From 0094fd4e0868025702e843094ae8b735d0f69e0c Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Mon, 6 Mar 2017 16:34:05 -0800 Subject: [PATCH] revert subcommand change, align CliCommandLine versions --- src/dotnet/DotNetTopLevelCommandBase.cs | 72 ++++++++++++++++--------- src/dotnet/dotnet.csproj | 2 +- test/dotnet.Tests/dotnet.Tests.csproj | 2 +- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/dotnet/DotNetTopLevelCommandBase.cs b/src/dotnet/DotNetTopLevelCommandBase.cs index 89aa1a6e6..640d8dccc 100644 --- a/src/dotnet/DotNetTopLevelCommandBase.cs +++ b/src/dotnet/DotNetTopLevelCommandBase.cs @@ -3,9 +3,12 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Utils; +using Microsoft.DotNet.Tools; +using Microsoft.DotNet.Tools.Common; namespace Microsoft.DotNet.Cli { @@ -21,44 +24,61 @@ namespace Microsoft.DotNet.Cli { DebugHelper.HandleDebugSwitch(ref args); - var result = Parser.DotnetCommand[CommandName] - .Parse(args); - - Reporter.Verbose.WriteLine(result.Diagram()); - - var command = result[CommandName]; - - if (command.HasOption("help")) + CommandLineApplication command = new CommandLineApplication(throwOnUnexpectedArg: true) { - result.ShowHelp(); - return 0; - } + Name = $"dotnet {CommandName}", + FullName = FullCommandNameLocalized, + }; - if (result.Errors.Any()) + command.HelpOption("-h|--help"); + + command.Argument(ArgumentName, ArgumentDescriptionLocalized); + + foreach (var subCommandCreator in SubCommands) { - Reporter.Error.WriteLine(result.Errors.First().Message.Red()); - return 1; + var subCommand = subCommandCreator(); + command.AddCommand(subCommand); + + subCommand.OnExecute(() => { + try + { + if (!command.Arguments.Any()) + { + throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, ArgumentDescriptionLocalized); + } + + var projectOrDirectory = command.Arguments.First().Value; + if (string.IsNullOrEmpty(projectOrDirectory)) + { + projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()); + } + + return subCommand.Run(projectOrDirectory); + } + catch (GracefulException e) + { + Reporter.Error.WriteLine(e.Message.Red()); + subCommand.ShowHelp(); + return 1; + } + }); } - var subCommand = SubCommands - .Select(c => c()) - .FirstOrDefault(c => c.Name == command.AppliedOptions.First().Name); - - var fileOrDirectory = command.AppliedOptions - .First() - .Arguments - .FirstOrDefault(); - try { - return subCommand.Run(fileOrDirectory); + return command.Execute(args); } catch (GracefulException e) { Reporter.Error.WriteLine(e.Message.Red()); - subCommand.ShowHelp(); + command.ShowHelp(); + return 1; + } + catch (CommandParsingException e) + { + Reporter.Error.WriteLine(e.Message.Red()); return 1; } } } -} \ No newline at end of file +} diff --git a/src/dotnet/dotnet.csproj b/src/dotnet/dotnet.csproj index 3f0d29978..fb24f652e 100644 --- a/src/dotnet/dotnet.csproj +++ b/src/dotnet/dotnet.csproj @@ -40,7 +40,7 @@ - + diff --git a/test/dotnet.Tests/dotnet.Tests.csproj b/test/dotnet.Tests/dotnet.Tests.csproj index 6d62e4124..825c0a095 100644 --- a/test/dotnet.Tests/dotnet.Tests.csproj +++ b/test/dotnet.Tests/dotnet.Tests.csproj @@ -42,6 +42,6 @@ - +