trigger help display using HelpException

This commit is contained in:
Jon Sequeira 2017-03-09 12:31:34 -08:00
parent ea308e9b59
commit 46b799af01
6 changed files with 58 additions and 10 deletions

View file

@ -0,0 +1,23 @@
using System;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Cli
{
///
/// <summary>Allows control flow to be interrupted in order to display help in the console.</summary>
///
[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; }
}
}

View file

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

View file

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

View file

@ -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() ?

View file

@ -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"];

View file

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