throw exception on parse errors

This commit is contained in:
Jon Sequeira 2017-03-13 13:29:03 -07:00
parent 11b7e7e449
commit 204d8594bf
15 changed files with 48 additions and 21 deletions

View file

@ -8,18 +8,21 @@ namespace Microsoft.DotNet.Cli.CommandLine
{
internal class CommandParsingException : Exception
{
private bool _isRequireSubCommandMissing;
private readonly bool _isRequireSubCommandMissing;
public CommandParsingException(string message) : base(message)
{
Data.Add("CLI_User_Displayed_Exception", true);
}
public CommandParsingException(
CommandLineApplication command,
string message,
bool isRequireSubCommandMissing = false)
: base(message)
: this(message)
{
Command = command;
_isRequireSubCommandMissing = isRequireSubCommandMissing;
Data.Add("CLI_User_Displayed_Exception", true);
}
public CommandLineApplication Command { get; }
@ -29,9 +32,9 @@ namespace Microsoft.DotNet.Cli.CommandLine
get
{
return _isRequireSubCommandMissing
? CommonLocalizableStrings.RequiredCommandNotPassed
: base.Message;
? CommonLocalizableStrings.RequiredCommandNotPassed
: base.Message;
}
}
}
}
}

View file

@ -65,6 +65,22 @@ namespace Microsoft.DotNet.Cli
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));
public static ArgumentsRule ExistingFilesOnly(
this ArgumentsRule rule) =>
rule.And(new ArgumentsRule(o =>
{
foreach (var filePath in o.Arguments)
{
if (!File.Exists(filePath) &&
!Directory.Exists(filePath))
{
return $"File not found: {filePath}";
}
}
return null;
}));
public static ArgumentsRule ExistingSlnFileOrDirectoryOnly(
this ArgumentsRule rule) =>
rule

View file

@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Cli
var result = parser.ParseFrom($"dotnet {CommandName}", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
var subcommandName = result.Command().Name;

View file

@ -12,8 +12,15 @@ namespace Microsoft.DotNet.Cli
public static void ShowHelp(this ParseResult parseResult) =>
Console.WriteLine(parseResult.Command().HelpView());
public static void ShowHelpIfRequested(this ParseResult parseResult)
public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult)
{
if (parseResult.Errors.Any())
{
throw new CommandParsingException(
string.Join(Environment.NewLine,
parseResult.Errors.Select(e => e.Message)));
}
if (parseResult.AppliedCommand().HasOption("help"))
{
// NOTE: this is a temporary stage in refactoring toward the ClicCommandLineParser being used at the CLI entry point.

View file

@ -8,6 +8,7 @@ using System.Net.Http;
using System.Threading;
using Microsoft.DotNet.Cli.CommandLine;
using Newtonsoft.Json.Linq;
using LocalizableStrings = Microsoft.DotNet.Tools.Add.PackageReference.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
@ -23,7 +24,7 @@ namespace Microsoft.DotNet.Cli
Create.Command(
"package",
".NET Add Package reference Command",
Accept.ExactlyOneArgument()
Accept.ExactlyOneArgument(errorMessage: o => LocalizableStrings.SpecifyExactlyOnePackageReference)
.WithSuggestionsFrom(QueryNuGet),
CommonOptions.HelpOption(),
Create.Option("-v|--version",

View file

@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Tools.Build
var result = parser.ParseFrom("dotnet build", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
var appliedBuildOptions = result["dotnet"]["build"];

View file

@ -29,7 +29,7 @@ namespace Microsoft.DotNet.Tools.Cache
var result = parser.ParseFrom("dotnet cache", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
var appliedBuildOptions = result["dotnet"]["cache"];

View file

@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Tools.Clean
var result = parser.ParseFrom("dotnet clean", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
var parsedClean = result["dotnet"]["clean"];

View file

@ -20,7 +20,7 @@ namespace Microsoft.DotNet.Tools.Migrate
var result = parser.ParseFrom("dotnet migrate", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
return result["dotnet"]["migrate"].Value<MigrateCommand>();
}

View file

@ -24,7 +24,7 @@ namespace Microsoft.DotNet.Tools.Pack
var result = parser.ParseFrom("dotnet pack", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
var parsedPack = result["dotnet"]["pack"];

View file

@ -27,7 +27,7 @@ namespace Microsoft.DotNet.Tools.Publish
var result = parser.ParseFrom("dotnet publish", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
msbuildArgs.Add("/t:Publish");

View file

@ -29,7 +29,7 @@ namespace Microsoft.DotNet.Tools.Restore
var result = parser.ParseFrom("dotnet restore", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
var parsedRestore = result["dotnet"]["restore"];

View file

@ -17,7 +17,7 @@ namespace Microsoft.DotNet.Tools.Run
var result = parser.ParseFrom("dotnet run", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
return result["dotnet"]["run"].Value<RunCommand>();
}

View file

@ -13,7 +13,8 @@ namespace Microsoft.DotNet.Cli
".NET modify solution file command",
Accept.ExactlyOneArgument()
.ExistingSlnFileOrDirectoryOnly()
.DefaultToCurrentDirectory(),
.DefaultToCurrentDirectory()
.With(name: "SLN_FILE" ),
CommonOptions.HelpOption(),
Create.Command("add",
".NET Add project(s) to a solution file Command",
@ -21,7 +22,6 @@ namespace Microsoft.DotNet.Cli
CommonOptions.HelpOption()),
Create.Command("list",
"List all projects in the solution.",
Accept.OneOrMoreArguments(),
CommonOptions.HelpOption()),
Create.Command("remove",
"Remove the specified project(s) from the solution. The project is not impacted.",

View file

@ -34,7 +34,7 @@ namespace Microsoft.DotNet.Tools.Test
var result = parser.ParseFrom("dotnet test", args);
result.ShowHelpIfRequested();
result.ShowHelpOrErrorIfAppropriate();
var parsedTest = result["dotnet"]["test"];