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

71 lines
2.1 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)
{
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
var parser = Parser.Instance["dotnet"]["complete"];
2017-03-03 13:14:36 -08:00
2017-03-07 07:10:12 -08:00
// parse the arguments
var result = parser.Parse(args);
2017-03-02 19:36:51 -08:00
2017-03-07 07:10:12 -08:00
var complete = result["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
var log = new StringBuilder();
log.AppendLine($"args: {string.Join(" ", args.Select(a => $"\"{a}\""))}");
log.AppendLine("diagram: " + result.Diagram());
File.WriteAllText("parse.log", log.ToString());
2017-03-02 19:36:51 -08:00
2017-03-07 07:10:12 -08:00
foreach (var suggestion in suggestions)
{
Console.WriteLine(suggestion);
}
}
catch (Exception e)
2017-03-02 19:36:51 -08:00
{
2017-03-07 07:10:12 -08:00
File.WriteAllText("dotnet completion exception.log", e.ToString());
throw;
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();
}
}
}