include HelpText in CommandParsingError, write it to stdout

This commit is contained in:
Jon Sequeira 2017-03-14 12:26:24 -07:00
parent 22fb17422f
commit 35732fc07d
4 changed files with 20 additions and 7 deletions

View file

@ -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

View file

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

View file

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

View file

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