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;
|
|
|
|
|
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)
|
|
|
|
{
|
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-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;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|