Replace dotnet help parser with CliCommandLineParser

CliCommandLineParserVersion 138 cannot use help as command and --help as
opinion at the same timem, update to 142
This commit is contained in:
William Li 2017-04-05 11:30:45 -07:00
parent cddffb08d2
commit a6d3012da1
9 changed files with 147 additions and 45 deletions

View file

@ -166,6 +166,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet.Tests", "test\dotnet
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-build.Tests", "test\dotnet-build.Tests\dotnet-build.Tests.csproj", "{BBB5A4C8-CD2D-4A6A-9159-0FEAF84B745E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-help.Tests", "test\dotnet-help.Tests\dotnet-help.Tests.csproj", "{8AA88E83-6A98-4AD6-86EB-2ED4F6EDA17F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-migrate.Tests", "test\dotnet-migrate.Tests\dotnet-migrate.Tests.csproj", "{726D2CB9-80E5-4496-9C86-910AC452C45E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-msbuild.Tests", "test\dotnet-msbuild.Tests\dotnet-msbuild.Tests.csproj", "{EF745C56-0350-4C42-AA22-86D592E1D8D5}"

View file

@ -16,7 +16,7 @@
<TemplateEngineTemplate2_0Version>1.0.0-beta2-20170410-189</TemplateEngineTemplate2_0Version>
<PlatformAbstractionsVersion>1.0.3</PlatformAbstractionsVersion>
<DependencyModelVersion>1.0.3</DependencyModelVersion>
<CliCommandLineParserVersion>0.1.0-alpha-140</CliCommandLineParserVersion>
<CliCommandLineParserVersion>0.1.0-alpha-142</CliCommandLineParserVersion>
</PropertyGroup>

View file

@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools.Help;
using static System.Environment;
using static Microsoft.DotNet.Cli.CommandLine.LocalizableStrings;
using LocalizableStrings = Microsoft.DotNet.Tools.Run.LocalizableStrings;
@ -47,6 +48,7 @@ namespace Microsoft.DotNet.Cli
ListCommandParser.List(),
NuGetCommandParser.NuGet(),
StoreCommandParser.Store(),
HelpCommandParser.Help(),
Create.Command("msbuild", ""),
Create.Command("vstest", ""),
CompleteCommandParser.Complete(),

View file

@ -1,68 +1,66 @@
// 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.Diagnostics;
using System.Reflection;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Cli;
using static HelpUsageText;
using Command = Microsoft.DotNet.Cli.CommandLine.Command;
using Parser = Microsoft.DotNet.Cli.Parser;
namespace Microsoft.DotNet.Tools.Help
{
public class HelpCommand
{
private readonly AppliedOption _appliedOption;
public HelpCommand(AppliedOption appliedOption)
{
_appliedOption = appliedOption;
}
public static int Run(string[] args)
{
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false);
app.Name = "dotnet help";
app.FullName = LocalizableStrings.AppFullName;
app.Description = LocalizableStrings.AppDescription;
DebugHelper.HandleDebugSwitch(ref args);
CommandArgument commandNameArgument = app.Argument($"<{LocalizableStrings.CommandArgumentName}>", LocalizableStrings.CommandArgumentDescription);
var parser = Parser.Instance;
var result = parser.ParseFrom("dotnet help", args);
var helpAppliedOption = result["dotnet"]["help"];
app.OnExecute(() =>
result.ShowHelpIfRequested();
HelpCommand cmd;
try
{
BuiltInCommandMetadata builtIn;
if (BuiltInCommandsCatalog.Commands.TryGetValue(commandNameArgument.Value, out builtIn))
cmd = new HelpCommand(helpAppliedOption);
}
catch (CommandCreationException e)
{
var process = ConfigureProcess(builtIn.DocLink);
process.Start();
process.WaitForExit();
return e.ExitCode;
}
if (helpAppliedOption.Arguments.Any())
{
return cmd.Execute();
}
else
{
Reporter.Error.WriteLine(String.Format(LocalizableStrings.CommandDoesNotExist, commandNameArgument.Value));
Reporter.Output.WriteLine(UsageText);
return 1;
}
return 0;
});
if (args.Length == 0)
{
PrintHelp();
return 0;
}
else
{
return app.Execute(args);
}
}
public static void PrintHelp()
{
PrintVersionHeader();
Reporter.Output.WriteLine(UsageText);
Reporter.Output.WriteLine(HelpUsageText.UsageText);
}
public static void PrintVersionHeader()
{
var versionString = string.IsNullOrEmpty(Product.Version) ?
string.Empty :
$" ({Product.Version})";
var versionString = string.IsNullOrEmpty(Product.Version) ? string.Empty : $" ({Product.Version})";
Reporter.Output.WriteLine(Product.LongName + versionString);
}
@ -99,5 +97,28 @@ namespace Microsoft.DotNet.Tools.Help
StartInfo = psInfo
};
}
public int Execute()
{
if (BuiltInCommandsCatalog.Commands.TryGetValue(
_appliedOption.Arguments.Single(),
out BuiltInCommandMetadata builtIn))
{
var process = ConfigureProcess(builtIn.DocLink);
process.Start();
process.WaitForExit();
return 0;
}
else
{
Reporter.Error.WriteLine(
string.Format(
LocalizableStrings.CommandDoesNotExist,
_appliedOption.Arguments.Single()));
Reporter.Output.WriteLine(HelpUsageText.UsageText);
return 1;
}
}
}
}

View file

@ -0,0 +1,21 @@
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
namespace Microsoft.DotNet.Tools.Help
{
internal static class HelpCommandParser
{
public static Command Help()
{
return Create.Command(
"help",
LocalizableStrings.AppFullName,
Accept.ZeroOrOneArgument()
.With(
LocalizableStrings.CommandArgumentDescription,
LocalizableStrings.CommandArgumentName),
CommonOptions.HelpOption());
}
}
}

View file

@ -42,3 +42,4 @@ Project modification commands:
msbuild {LocalizableStrings.MsBuildDefinition}
vstest {LocalizableStrings.VsTestDefinition}";
}

View file

@ -9,8 +9,17 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
{
public override CommandResult Execute(string args = "")
{
args = $"help {args}";
return base.Execute(args);
return base.Execute(AppendHelp(args));
}
public override CommandResult ExecuteWithCapturedOutput(string args = "")
{
return base.ExecuteWithCapturedOutput(AppendHelp(args));
}
private string AppendHelp(string args)
{
return args = $"help {args}";
}
}
}

View file

@ -67,6 +67,15 @@ Advanced Commands:
cmd.StdOut.Should().ContainVisuallySameFragment(HelpText);
}
[Fact]
public void WhenHelpCommandIsPassedToDotnetItPrintsUsage()
{
var cmd = new HelpCommand()
.ExecuteWithCapturedOutput();
cmd.Should().Pass();
cmd.StdOut.Should().ContainVisuallySameFragment(HelpText);
}
[Fact]
public void WhenInvalidCommandIsPassedToDotnetHelpItPrintsError()
{

View file

@ -0,0 +1,37 @@
// 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 Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
using FluentAssertions;
using HelpActual = Microsoft.DotNet.Tools.Help;
namespace Microsoft.DotNet.Help.Tests
{
public class GivenThatIWantToShowHelpForDotnetHelpCommand : TestBase
{
private const string HelpText =
@".NET CLI help utility
Usage: dotnet help [options] <COMMAND_NAME>
Arguments:
<COMMAND_NAME> CLI command for which to view more detailed help.
Options:
-h, --help Show help information";
[Theory]
[InlineData("--help")]
[InlineData("-h")]
[InlineData("-?")]
[InlineData("/?")]
public void WhenHelpOptionIsPassedToDotnetHelpCommandItPrintsUsage(string helpArg)
{
var cmd = new HelpCommand()
.ExecuteWithCapturedOutput($"{helpArg}");
cmd.Should().Pass();
cmd.StdOut.Should().ContainVisuallySameFragment(HelpText);
}
}
}