dotnet-installer/src/dotnet/commands/dotnet-complete/CompleteCommand.cs

73 lines
2 KiB
C#
Raw Normal View History

2017-03-02 19:36:51 -08:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Cli
2017-03-02 19:36:51 -08:00
{
public class CompleteCommand
{
public static int Run(string[] args)
{
return RunWithReporter(args, Reporter.Output);
}
public static int RunWithReporter(string [] args, IReporter reporter)
{
if (reporter == null)
{
throw new ArgumentNullException(nameof(reporter));
}
2017-03-07 07:10:12 -08:00
try
{
DebugHelper.HandleDebugSwitch(ref args);
2017-03-02 19:36:51 -08:00
2017-03-07 07:10:12 -08:00
// get the parser for the current subcommand
2017-03-08 16:02:24 -08:00
var parser = Parser.Instance;
2017-03-03 13:14:36 -08:00
2017-03-07 07:10:12 -08:00
// parse the arguments
2017-03-08 16:02:24 -08:00
var result = parser.ParseFrom("dotnet complete", args);
2017-03-02 19:36:51 -08:00
var complete = result["dotnet"]["complete"];
2017-03-02 19:36:51 -08:00
2017-03-07 07:10:12 -08:00
var suggestions = Suggestions(complete);
2017-03-02 19:36:51 -08:00
2017-03-07 07:10:12 -08:00
foreach (var suggestion in suggestions)
{
reporter.WriteLine(suggestion);
2017-03-07 07:10:12 -08:00
}
}
2017-03-22 15:47:54 -07:00
catch (Exception)
2017-03-02 19:36:51 -08:00
{
2017-03-22 13:51:45 -07:00
return 1;
2017-03-02 19:36:51 -08:00
}
return 0;
}
private static string[] Suggestions(AppliedOption complete)
2017-03-02 19:36:51 -08:00
{
var input = complete.Arguments.SingleOrDefault() ?? "";
var positionOption = complete.AppliedOptions.SingleOrDefault(a => a.Name == "position");
if (positionOption != null)
{
var position = positionOption.Value<int>();
if (position > input.Length)
{
input += " ";
}
}
var result = Parser.Instance.Parse(input);
2017-03-02 19:36:51 -08:00
return result.Suggestions()
.ToArray();
}
}
}