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 EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-build.Tests", "test\dotnet-build.Tests\dotnet-build.Tests.csproj", "{BBB5A4C8-CD2D-4A6A-9159-0FEAF84B745E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-build.Tests", "test\dotnet-build.Tests\dotnet-build.Tests.csproj", "{BBB5A4C8-CD2D-4A6A-9159-0FEAF84B745E}"
EndProject 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}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-migrate.Tests", "test\dotnet-migrate.Tests\dotnet-migrate.Tests.csproj", "{726D2CB9-80E5-4496-9C86-910AC452C45E}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-msbuild.Tests", "test\dotnet-msbuild.Tests\dotnet-msbuild.Tests.csproj", "{EF745C56-0350-4C42-AA22-86D592E1D8D5}" 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,8 +16,8 @@
<TemplateEngineTemplate2_0Version>1.0.0-beta2-20170410-189</TemplateEngineTemplate2_0Version> <TemplateEngineTemplate2_0Version>1.0.0-beta2-20170410-189</TemplateEngineTemplate2_0Version>
<PlatformAbstractionsVersion>1.0.3</PlatformAbstractionsVersion> <PlatformAbstractionsVersion>1.0.3</PlatformAbstractionsVersion>
<DependencyModelVersion>1.0.3</DependencyModelVersion> <DependencyModelVersion>1.0.3</DependencyModelVersion>
<CliCommandLineParserVersion>0.1.0-alpha-140</CliCommandLineParserVersion> <CliCommandLineParserVersion>0.1.0-alpha-142</CliCommandLineParserVersion>
</PropertyGroup> </PropertyGroup>
<!-- infrastructure and test only dependencies --> <!-- infrastructure and test only dependencies -->

View file

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

View file

@ -1,68 +1,66 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // 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. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Cli; using Command = Microsoft.DotNet.Cli.CommandLine.Command;
using static HelpUsageText; using Parser = Microsoft.DotNet.Cli.Parser;
namespace Microsoft.DotNet.Tools.Help namespace Microsoft.DotNet.Tools.Help
{ {
public class HelpCommand public class HelpCommand
{ {
private readonly AppliedOption _appliedOption;
public HelpCommand(AppliedOption appliedOption)
{
_appliedOption = appliedOption;
}
public static int Run(string[] args) public static int Run(string[] args)
{ {
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false); DebugHelper.HandleDebugSwitch(ref args);
app.Name = "dotnet help";
app.FullName = LocalizableStrings.AppFullName;
app.Description = LocalizableStrings.AppDescription;
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; cmd = new HelpCommand(helpAppliedOption);
if (BuiltInCommandsCatalog.Commands.TryGetValue(commandNameArgument.Value, out builtIn)) }
{ catch (CommandCreationException e)
var process = ConfigureProcess(builtIn.DocLink);
process.Start();
process.WaitForExit();
}
else
{
Reporter.Error.WriteLine(String.Format(LocalizableStrings.CommandDoesNotExist, commandNameArgument.Value));
Reporter.Output.WriteLine(UsageText);
return 1;
}
return 0;
});
if (args.Length == 0)
{ {
PrintHelp(); return e.ExitCode;
return 0; }
if (helpAppliedOption.Arguments.Any())
{
return cmd.Execute();
} }
else else
{ {
return app.Execute(args); PrintHelp();
return 0;
} }
} }
public static void PrintHelp() public static void PrintHelp()
{ {
PrintVersionHeader(); PrintVersionHeader();
Reporter.Output.WriteLine(UsageText); Reporter.Output.WriteLine(HelpUsageText.UsageText);
} }
public static void PrintVersionHeader() public static void PrintVersionHeader()
{ {
var versionString = string.IsNullOrEmpty(Product.Version) ? var versionString = string.IsNullOrEmpty(Product.Version) ? string.Empty : $" ({Product.Version})";
string.Empty :
$" ({Product.Version})";
Reporter.Output.WriteLine(Product.LongName + versionString); Reporter.Output.WriteLine(Product.LongName + versionString);
} }
@ -93,11 +91,34 @@ namespace Microsoft.DotNet.Tools.Help
Arguments = docUrl Arguments = docUrl
}; };
} }
return new Process return new Process
{ {
StartInfo = psInfo 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

@ -3,7 +3,7 @@ using Microsoft.DotNet.Tools.Help;
internal static class HelpUsageText internal static class HelpUsageText
{ {
public static readonly string UsageText = public static readonly string UsageText =
$@"{LocalizableStrings.Usage}: dotnet [host-options] [command] [arguments] [common-options] $@"{LocalizableStrings.Usage}: dotnet [host-options] [command] [arguments] [common-options]
{LocalizableStrings.Arguments}: {LocalizableStrings.Arguments}:
[command] {LocalizableStrings.CommandDefinition} [command] {LocalizableStrings.CommandDefinition}
@ -13,7 +13,7 @@ internal static class HelpUsageText
{LocalizableStrings.CommonOptions}: {LocalizableStrings.CommonOptions}:
-v|--verbose {LocalizableStrings.VerboseDefinition} -v|--verbose {LocalizableStrings.VerboseDefinition}
-h|--help {LocalizableStrings.HelpDefinition} -h|--help {LocalizableStrings.HelpDefinition}
{LocalizableStrings.HostOptions}: {LocalizableStrings.HostOptions}:
-d|--diagnostics {LocalizableStrings.DiagnosticsDefinition} -d|--diagnostics {LocalizableStrings.DiagnosticsDefinition}
@ -41,4 +41,5 @@ Project modification commands:
nuget {LocalizableStrings.NugetDefinition} nuget {LocalizableStrings.NugetDefinition}
msbuild {LocalizableStrings.MsBuildDefinition} msbuild {LocalizableStrings.MsBuildDefinition}
vstest {LocalizableStrings.VsTestDefinition}"; vstest {LocalizableStrings.VsTestDefinition}";
} }

View file

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

View file

@ -25,7 +25,7 @@ Arguments:
Common options: Common options:
-v|--verbose Enable verbose output -v|--verbose Enable verbose output
-h|--help Show help -h|--help Show help
Host options (passed before the command): Host options (passed before the command):
-d|--diagnostics Enable diagnostic output -d|--diagnostics Enable diagnostic output
@ -67,12 +67,21 @@ Advanced Commands:
cmd.StdOut.Should().ContainVisuallySameFragment(HelpText); cmd.StdOut.Should().ContainVisuallySameFragment(HelpText);
} }
[Fact]
public void WhenHelpCommandIsPassedToDotnetItPrintsUsage()
{
var cmd = new HelpCommand()
.ExecuteWithCapturedOutput();
cmd.Should().Pass();
cmd.StdOut.Should().ContainVisuallySameFragment(HelpText);
}
[Fact] [Fact]
public void WhenInvalidCommandIsPassedToDotnetHelpItPrintsError() public void WhenInvalidCommandIsPassedToDotnetHelpItPrintsError()
{ {
var cmd = new DotnetCommand() var cmd = new DotnetCommand()
.ExecuteWithCapturedOutput("help invalid"); .ExecuteWithCapturedOutput("help invalid");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().ContainVisuallySameFragment($"Specified command 'invalid' is not a valid CLI command. Please specify a valid CLI commands. For more information, run dotnet help."); cmd.StdErr.Should().ContainVisuallySameFragment($"Specified command 'invalid' is not a valid CLI command. Please specify a valid CLI commands. For more information, run dotnet help.");
cmd.StdOut.Should().ContainVisuallySameFragment(HelpText); cmd.StdOut.Should().ContainVisuallySameFragment(HelpText);
@ -92,7 +101,7 @@ Advanced Commands:
var proc = HelpActual.HelpCommand.ConfigureProcess("https://aka.ms/dotnet-build"); var proc = HelpActual.HelpCommand.ConfigureProcess("https://aka.ms/dotnet-build");
Assert.Equal("xdg-open", proc.StartInfo.FileName); Assert.Equal("xdg-open", proc.StartInfo.FileName);
Assert.Equal("https://aka.ms/dotnet-build", proc.StartInfo.Arguments); Assert.Equal("https://aka.ms/dotnet-build", proc.StartInfo.Arguments);
} }
[MacOsOnlyFact] [MacOsOnlyFact]
public void WhenRunOnMacOsDotnetHelpCommandShouldContainProperProcessInformation() public void WhenRunOnMacOsDotnetHelpCommandShouldContainProperProcessInformation()

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