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;
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
namespace Microsoft.DotNet.Cli
|
2017-03-02 19:36:51 -08:00
|
|
|
{
|
|
|
|
public class CompleteCommand
|
|
|
|
{
|
|
|
|
public static int Run(string[] args)
|
|
|
|
{
|
2018-05-16 14:29:53 -07:00
|
|
|
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
|
|
|
|
2017-03-09 09:14:55 -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)
|
|
|
|
{
|
2018-05-16 14:29:53 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
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 += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:40:18 -08:00
|
|
|
var result = Parser.Instance.Parse(input);
|
2017-03-02 19:36:51 -08:00
|
|
|
|
|
|
|
return result.Suggestions()
|
|
|
|
.ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|