throw exception on parse errors
This commit is contained in:
parent
11b7e7e449
commit
204d8594bf
15 changed files with 48 additions and 21 deletions
|
@ -8,18 +8,21 @@ namespace Microsoft.DotNet.Cli.CommandLine
|
||||||
{
|
{
|
||||||
internal class CommandParsingException : Exception
|
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(
|
public CommandParsingException(
|
||||||
CommandLineApplication command,
|
CommandLineApplication command,
|
||||||
string message,
|
string message,
|
||||||
bool isRequireSubCommandMissing = false)
|
bool isRequireSubCommandMissing = false)
|
||||||
: base(message)
|
: this(message)
|
||||||
{
|
{
|
||||||
Command = command;
|
Command = command;
|
||||||
_isRequireSubCommandMissing = isRequireSubCommandMissing;
|
_isRequireSubCommandMissing = isRequireSubCommandMissing;
|
||||||
|
|
||||||
Data.Add("CLI_User_Displayed_Exception", true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandLineApplication Command { get; }
|
public CommandLineApplication Command { get; }
|
||||||
|
@ -29,8 +32,8 @@ namespace Microsoft.DotNet.Cli.CommandLine
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return _isRequireSubCommandMissing
|
return _isRequireSubCommandMissing
|
||||||
? CommonLocalizableStrings.RequiredCommandNotPassed
|
? CommonLocalizableStrings.RequiredCommandNotPassed
|
||||||
: base.Message;
|
: base.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,22 @@ namespace Microsoft.DotNet.Cli
|
||||||
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
|
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
|
||||||
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));
|
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(
|
public static ArgumentsRule ExistingSlnFileOrDirectoryOnly(
|
||||||
this ArgumentsRule rule) =>
|
this ArgumentsRule rule) =>
|
||||||
rule
|
rule
|
||||||
|
|
|
@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Cli
|
||||||
|
|
||||||
var result = parser.ParseFrom($"dotnet {CommandName}", args);
|
var result = parser.ParseFrom($"dotnet {CommandName}", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
var subcommandName = result.Command().Name;
|
var subcommandName = result.Command().Name;
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,15 @@ namespace Microsoft.DotNet.Cli
|
||||||
public static void ShowHelp(this ParseResult parseResult) =>
|
public static void ShowHelp(this ParseResult parseResult) =>
|
||||||
Console.WriteLine(parseResult.Command().HelpView());
|
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"))
|
if (parseResult.AppliedCommand().HasOption("help"))
|
||||||
{
|
{
|
||||||
// NOTE: this is a temporary stage in refactoring toward the ClicCommandLineParser being used at the CLI entry point.
|
// NOTE: this is a temporary stage in refactoring toward the ClicCommandLineParser being used at the CLI entry point.
|
||||||
|
|
|
@ -8,6 +8,7 @@ using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Microsoft.DotNet.Cli.CommandLine;
|
using Microsoft.DotNet.Cli.CommandLine;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using LocalizableStrings = Microsoft.DotNet.Tools.Add.PackageReference.LocalizableStrings;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Cli
|
namespace Microsoft.DotNet.Cli
|
||||||
{
|
{
|
||||||
|
@ -23,7 +24,7 @@ namespace Microsoft.DotNet.Cli
|
||||||
Create.Command(
|
Create.Command(
|
||||||
"package",
|
"package",
|
||||||
".NET Add Package reference Command",
|
".NET Add Package reference Command",
|
||||||
Accept.ExactlyOneArgument()
|
Accept.ExactlyOneArgument(errorMessage: o => LocalizableStrings.SpecifyExactlyOnePackageReference)
|
||||||
.WithSuggestionsFrom(QueryNuGet),
|
.WithSuggestionsFrom(QueryNuGet),
|
||||||
CommonOptions.HelpOption(),
|
CommonOptions.HelpOption(),
|
||||||
Create.Option("-v|--version",
|
Create.Option("-v|--version",
|
||||||
|
|
|
@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Tools.Build
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet build", args);
|
var result = parser.ParseFrom("dotnet build", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
var appliedBuildOptions = result["dotnet"]["build"];
|
var appliedBuildOptions = result["dotnet"]["build"];
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ namespace Microsoft.DotNet.Tools.Cache
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet cache", args);
|
var result = parser.ParseFrom("dotnet cache", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
var appliedBuildOptions = result["dotnet"]["cache"];
|
var appliedBuildOptions = result["dotnet"]["cache"];
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Tools.Clean
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet clean", args);
|
var result = parser.ParseFrom("dotnet clean", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
var parsedClean = result["dotnet"]["clean"];
|
var parsedClean = result["dotnet"]["clean"];
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace Microsoft.DotNet.Tools.Migrate
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet migrate", args);
|
var result = parser.ParseFrom("dotnet migrate", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
return result["dotnet"]["migrate"].Value<MigrateCommand>();
|
return result["dotnet"]["migrate"].Value<MigrateCommand>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace Microsoft.DotNet.Tools.Pack
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet pack", args);
|
var result = parser.ParseFrom("dotnet pack", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
var parsedPack = result["dotnet"]["pack"];
|
var parsedPack = result["dotnet"]["pack"];
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ namespace Microsoft.DotNet.Tools.Publish
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet publish", args);
|
var result = parser.ParseFrom("dotnet publish", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
msbuildArgs.Add("/t:Publish");
|
msbuildArgs.Add("/t:Publish");
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ namespace Microsoft.DotNet.Tools.Restore
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet restore", args);
|
var result = parser.ParseFrom("dotnet restore", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
var parsedRestore = result["dotnet"]["restore"];
|
var parsedRestore = result["dotnet"]["restore"];
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ namespace Microsoft.DotNet.Tools.Run
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet run", args);
|
var result = parser.ParseFrom("dotnet run", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
return result["dotnet"]["run"].Value<RunCommand>();
|
return result["dotnet"]["run"].Value<RunCommand>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,8 @@ namespace Microsoft.DotNet.Cli
|
||||||
".NET modify solution file command",
|
".NET modify solution file command",
|
||||||
Accept.ExactlyOneArgument()
|
Accept.ExactlyOneArgument()
|
||||||
.ExistingSlnFileOrDirectoryOnly()
|
.ExistingSlnFileOrDirectoryOnly()
|
||||||
.DefaultToCurrentDirectory(),
|
.DefaultToCurrentDirectory()
|
||||||
|
.With(name: "SLN_FILE" ),
|
||||||
CommonOptions.HelpOption(),
|
CommonOptions.HelpOption(),
|
||||||
Create.Command("add",
|
Create.Command("add",
|
||||||
".NET Add project(s) to a solution file Command",
|
".NET Add project(s) to a solution file Command",
|
||||||
|
@ -21,7 +22,6 @@ namespace Microsoft.DotNet.Cli
|
||||||
CommonOptions.HelpOption()),
|
CommonOptions.HelpOption()),
|
||||||
Create.Command("list",
|
Create.Command("list",
|
||||||
"List all projects in the solution.",
|
"List all projects in the solution.",
|
||||||
Accept.OneOrMoreArguments(),
|
|
||||||
CommonOptions.HelpOption()),
|
CommonOptions.HelpOption()),
|
||||||
Create.Command("remove",
|
Create.Command("remove",
|
||||||
"Remove the specified project(s) from the solution. The project is not impacted.",
|
"Remove the specified project(s) from the solution. The project is not impacted.",
|
||||||
|
|
|
@ -34,7 +34,7 @@ namespace Microsoft.DotNet.Tools.Test
|
||||||
|
|
||||||
var result = parser.ParseFrom("dotnet test", args);
|
var result = parser.ParseFrom("dotnet test", args);
|
||||||
|
|
||||||
result.ShowHelpIfRequested();
|
result.ShowHelpOrErrorIfAppropriate();
|
||||||
|
|
||||||
var parsedTest = result["dotnet"]["test"];
|
var parsedTest = result["dotnet"]["test"];
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue