From 35732fc07dc267882d9938d5933e2c54464d1edd Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Tue, 14 Mar 2017 12:26:24 -0700 Subject: [PATCH] include HelpText in CommandParsingError, write it to stdout --- src/dotnet/CommandLine/CommandParsingException.cs | 7 ++++++- src/dotnet/ParseResultExtensions.cs | 5 +++-- src/dotnet/Program.cs | 13 ++++++++++--- src/dotnet/commands/dotnet-sln/SlnCommandParser.cs | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/dotnet/CommandLine/CommandParsingException.cs b/src/dotnet/CommandLine/CommandParsingException.cs index 606db5564..79e19e522 100644 --- a/src/dotnet/CommandLine/CommandParsingException.cs +++ b/src/dotnet/CommandLine/CommandParsingException.cs @@ -10,8 +10,11 @@ namespace Microsoft.DotNet.Cli.CommandLine { private readonly bool _isRequireSubCommandMissing; - public CommandParsingException(string message) : base(message) + public CommandParsingException( + string message, + string helpText = null) : base(message) { + HelpText = helpText ?? ""; Data.Add("CLI_User_Displayed_Exception", true); } @@ -27,6 +30,8 @@ namespace Microsoft.DotNet.Cli.CommandLine public CommandLineApplication Command { get; } + public string HelpText { get; } = ""; + public override string Message { get diff --git a/src/dotnet/ParseResultExtensions.cs b/src/dotnet/ParseResultExtensions.cs index 9fa549dc9..82932de0b 100644 --- a/src/dotnet/ParseResultExtensions.cs +++ b/src/dotnet/ParseResultExtensions.cs @@ -27,8 +27,9 @@ namespace Microsoft.DotNet.Cli if (parseResult.Errors.Any()) { throw new CommandParsingException( - string.Join(Environment.NewLine, - parseResult.Errors.Select(e => e.Message))); + message: string.Join(Environment.NewLine, + parseResult.Errors.Select(e => e.Message)), + helpText: parseResult?.Command()?.HelpView()); } } } diff --git a/src/dotnet/Program.cs b/src/dotnet/Program.cs index 983f6507f..9af50b56e 100644 --- a/src/dotnet/Program.cs +++ b/src/dotnet/Program.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Configurer; using Microsoft.DotNet.PlatformAbstractions; @@ -84,9 +85,15 @@ namespace Microsoft.DotNet.Cli } catch (Exception e) when (e.ShouldBeDisplayedAsError()) { - Reporter.Error.WriteLine(CommandContext.IsVerbose() ? - e.ToString().Red().Bold() : - e.Message.Red().Bold()); + Reporter.Error.WriteLine(CommandContext.IsVerbose() + ? e.ToString().Red().Bold() + : e.Message.Red().Bold()); + + var commandParsingException = e as CommandParsingException; + if (commandParsingException != null) + { + Reporter.Output.WriteLine(commandParsingException.HelpText); + } return 1; } diff --git a/src/dotnet/commands/dotnet-sln/SlnCommandParser.cs b/src/dotnet/commands/dotnet-sln/SlnCommandParser.cs index 5a38fe68b..ce4865921 100644 --- a/src/dotnet/commands/dotnet-sln/SlnCommandParser.cs +++ b/src/dotnet/commands/dotnet-sln/SlnCommandParser.cs @@ -20,7 +20,7 @@ namespace Microsoft.DotNet.Cli CommonOptions.HelpOption(), Create.Command("add", ".NET Add project(s) to a solution file Command", - Accept.OneOrMoreArguments() + Accept.OneOrMoreArguments(o => CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd) .With(name: "args", description: LocalizableStrings.AddSubcommandHelpText), CommonOptions.HelpOption()),