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

65 lines
1.9 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.IO;
using System.Linq;
using System.Text;
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)
{
DebugHelper.HandleDebugSwitch(ref args);
2017-03-03 13:14:36 -08:00
// get the parser for the current subcommand
var completeCommandParser = Parser.DotnetCommand["complete"];
2017-03-03 13:14:36 -08:00
// parse the arguments
var result = completeCommandParser.Parse(args);
2017-03-02 19:36:51 -08:00
var complete = result["complete"];
var suggestions = Suggestions(complete);
2017-03-02 19:36:51 -08:00
#if DEBUG
var log = new StringBuilder();
log.AppendLine($"args: {string.Join(" ", args.Select(a => $"\"{a}\""))}");
log.AppendLine("diagram: " + result.Diagram());
2017-03-02 19:36:51 -08:00
File.WriteAllText("parse.log", log.ToString());
#endif
2017-03-02 19:36:51 -08:00
foreach (var suggestion in suggestions)
{
Console.WriteLine(suggestion);
}
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.DotnetCommand.Parse(input);
2017-03-02 19:36:51 -08:00
return result.Suggestions()
.ToArray();
}
}
}