trigger help display using HelpException
This commit is contained in:
parent
ea308e9b59
commit
46b799af01
6 changed files with 58 additions and 10 deletions
23
src/dotnet/HelpException.cs
Normal file
23
src/dotnet/HelpException.cs
Normal 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; }
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
src/dotnet/ParserExtensions.cs
Normal file
14
src/dotnet/ParserExtensions.cs
Normal 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());
|
||||
}
|
||||
}
|
|
@ -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() ?
|
||||
|
|
|
@ -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"];
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue