From 46b799af013f5b23675c95b603b650181a5a57a2 Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Thu, 9 Mar 2017 12:31:34 -0800 Subject: [PATCH] trigger help display using HelpException --- src/dotnet/HelpException.cs | 23 +++++++++++++++++++ src/dotnet/ParseResultExtensions.cs | 20 ++++++++-------- src/dotnet/ParserExtensions.cs | 14 +++++++++++ src/dotnet/Program.cs | 5 ++++ src/dotnet/commands/dotnet-publish/Program.cs | 2 ++ src/dotnet/commands/dotnet-restore/Program.cs | 4 +++- 6 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/dotnet/HelpException.cs create mode 100644 src/dotnet/ParserExtensions.cs diff --git a/src/dotnet/HelpException.cs b/src/dotnet/HelpException.cs new file mode 100644 index 000000000..be6427f06 --- /dev/null +++ b/src/dotnet/HelpException.cs @@ -0,0 +1,23 @@ +using System; +using Microsoft.DotNet.Cli.Utils; + +namespace Microsoft.DotNet.Cli +{ + /// + /// Allows control flow to be interrupted in order to display help in the console. + /// + [Obsolete("This is intended to facilitate refactoring during parser replacement and should not be used after that work is done.")] + public class HelpException : Exception + { + public HelpException( + string message, + bool isError = false) : base(message) + { + IsError = isError; + + Data.Add(ExceptionExtensions.CLI_User_Displayed_Exception, true); + } + + public bool IsError { get; } + } +} \ No newline at end of file diff --git a/src/dotnet/ParseResultExtensions.cs b/src/dotnet/ParseResultExtensions.cs index 894f494fb..f4ae8866a 100644 --- a/src/dotnet/ParseResultExtensions.cs +++ b/src/dotnet/ParseResultExtensions.cs @@ -7,18 +7,20 @@ using Microsoft.DotNet.Cli.CommandLine; namespace Microsoft.DotNet.Cli { - public static class ParserExtensions - { - public static ParseResult ParseFrom( - this CommandLine.Parser parser, - string context, - string[] args) => - parser.Parse(context.Split(' ').Concat(args).ToArray()); - } - public static class ParseResultExtensions { public static void ShowHelp(this ParseResult parseResult) => Console.WriteLine(parseResult.Command().HelpView()); + + public static void ShowHelpIfRequested(this ParseResult parseResult) + { + if (parseResult.HasOption("help")) + { + + // NOTE: this is a temporary stage in refactoring toward the ClicCommandLineParser being used at the CLI entry point. + + throw new HelpException(parseResult.Command().HelpView()); + } + } } } \ No newline at end of file diff --git a/src/dotnet/ParserExtensions.cs b/src/dotnet/ParserExtensions.cs new file mode 100644 index 000000000..fd378f0f3 --- /dev/null +++ b/src/dotnet/ParserExtensions.cs @@ -0,0 +1,14 @@ +using System.Linq; +using Microsoft.DotNet.Cli.CommandLine; + +namespace Microsoft.DotNet.Cli +{ + public static class ParserExtensions + { + public static ParseResult ParseFrom( + this CommandLine.Parser parser, + string context, + string[] args) => + parser.Parse(context.Split(' ').Concat(args).ToArray()); + } +} \ No newline at end of file diff --git a/src/dotnet/Program.cs b/src/dotnet/Program.cs index 57210c2e2..b47943ce8 100644 --- a/src/dotnet/Program.cs +++ b/src/dotnet/Program.cs @@ -77,6 +77,11 @@ namespace Microsoft.DotNet.Cli return ProcessArgs(args); } } + catch (HelpException e) + { + Reporter.Output.Write(e.Message); + return e.IsError ? 1 : 0; + } catch (Exception e) when (e.ShouldBeDisplayedAsError()) { Reporter.Error.WriteLine(CommandContext.IsVerbose() ? diff --git a/src/dotnet/commands/dotnet-publish/Program.cs b/src/dotnet/commands/dotnet-publish/Program.cs index 8d8c12a20..c04364f82 100644 --- a/src/dotnet/commands/dotnet-publish/Program.cs +++ b/src/dotnet/commands/dotnet-publish/Program.cs @@ -25,6 +25,8 @@ namespace Microsoft.DotNet.Tools.Publish var result = parser.ParseFrom("dotnet publish", args); + result.ShowHelpIfRequested(); + msbuildArgs.Add("/t:Publish"); var appliedPublishOption = result["dotnet"]["publish"]; diff --git a/src/dotnet/commands/dotnet-restore/Program.cs b/src/dotnet/commands/dotnet-restore/Program.cs index f31291dc4..178648938 100644 --- a/src/dotnet/commands/dotnet-restore/Program.cs +++ b/src/dotnet/commands/dotnet-restore/Program.cs @@ -29,6 +29,8 @@ namespace Microsoft.DotNet.Tools.Restore var result = parser.ParseFrom("dotnet restore", args); + result.ShowHelpIfRequested(); + Reporter.Output.WriteLine(result.Diagram()); var restore = result["dotnet"]["restore"]; @@ -60,7 +62,7 @@ namespace Microsoft.DotNet.Tools.Restore { return e.ExitCode; } - + return cmd.Execute(); } }