move parse result validation to CommandBase to allow custom messages

This commit is contained in:
Jon Sequeira 2017-03-19 14:30:43 -07:00
parent e0ca794ed5
commit 43c13f2f53
16 changed files with 75 additions and 31 deletions

View file

@ -1,10 +1,22 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Cli.CommandLine;
namespace Microsoft.DotNet.Cli namespace Microsoft.DotNet.Cli
{ {
public abstract class CommandBase public abstract class CommandBase
{ {
protected CommandBase(ParseResult parseResult)
{
ShowHelpOrErrorIfAppropriate(parseResult);
}
protected virtual void ShowHelpOrErrorIfAppropriate(ParseResult parseResult)
{
parseResult.ShowHelpOrErrorIfAppropriate();
}
public abstract int Execute(); public abstract int Execute();
} }
} }

View file

@ -16,6 +16,8 @@ namespace Microsoft.DotNet.Cli
protected abstract string FullCommandNameLocalized { get; } protected abstract string FullCommandNameLocalized { get; }
protected abstract string ArgumentName { get; } protected abstract string ArgumentName { get; }
protected abstract string ArgumentDescriptionLocalized { get; } protected abstract string ArgumentDescriptionLocalized { get; }
protected ParseResult ParseResult { get; private set; }
internal abstract Dictionary<string, Func<AppliedOption, CommandBase>> SubCommands { get; } internal abstract Dictionary<string, Func<AppliedOption, CommandBase>> SubCommands { get; }
public int RunCommand(string[] args) public int RunCommand(string[] args)
@ -24,33 +26,26 @@ namespace Microsoft.DotNet.Cli
var parser = Parser.Instance; var parser = Parser.Instance;
var result = parser.ParseFrom($"dotnet {CommandName}", args); ParseResult = parser.ParseFrom($"dotnet {CommandName}", args);
result.ShowHelpOrErrorIfAppropriate(); var subcommandName = ParseResult.Command().Name;
var subcommandName = result.Command().Name;
try try
{ {
var create = SubCommands[subcommandName]; var create = SubCommands[subcommandName];
var command = create(result["dotnet"][CommandName]); var command = create(ParseResult["dotnet"][CommandName]);
return command.Execute(); return command.Execute();
} }
catch (KeyNotFoundException e) catch (KeyNotFoundException)
{ {
throw new GracefulException(CommonLocalizableStrings.RequiredCommandNotPassed); throw new GracefulException(CommonLocalizableStrings.RequiredCommandNotPassed);
} }
catch (GracefulException e) catch (GracefulException e)
{ {
Reporter.Error.WriteLine(e.Message.Red()); Reporter.Error.WriteLine(e.Message.Red());
result.ShowHelp(); ParseResult.ShowHelp();
return 1;
}
catch (CommandParsingException e)
{
Reporter.Error.WriteLine(e.Message.Red());
return 1; return 1;
} }
} }

View file

@ -25,12 +25,14 @@ namespace Microsoft.DotNet.Tools.Add
["reference"] = ["reference"] =
add => new AddProjectToProjectReferenceCommand( add => new AddProjectToProjectReferenceCommand(
add["reference"], add["reference"],
add.Value<string>()), add.Value<string>(),
ParseResult),
["package"] = ["package"] =
add => new AddPackageReferenceCommand( add => new AddPackageReferenceCommand(
add["package"], add["package"],
add.Value<string>()) add.Value<string>(),
ParseResult)
}; };
public static int Run(string[] args) public static int Run(string[] args)

View file

@ -22,7 +22,8 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
public AddPackageReferenceCommand( public AddPackageReferenceCommand(
AppliedOption appliedCommand, AppliedOption appliedCommand,
string fileOrDirectory) string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {
@ -38,6 +39,16 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
_packageId = appliedCommand.Value<string>(); _packageId = appliedCommand.Value<string>();
} }
protected override void ShowHelpOrErrorIfAppropriate(ParseResult parseResult)
{
if (parseResult.UnmatchedTokens.Any())
{
throw new GracefulException(LocalizableStrings.SpecifyExactlyOnePackageReference);
}
base.ShowHelpOrErrorIfAppropriate(parseResult);
}
public override int Execute() public override int Execute()
{ {
var projectFilePath = string.Empty; var projectFilePath = string.Empty;

View file

@ -22,7 +22,8 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
public AddProjectToProjectReferenceCommand( public AddProjectToProjectReferenceCommand(
AppliedOption appliedCommand, AppliedOption appliedCommand,
string fileOrDirectory) string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {

View file

@ -20,7 +20,12 @@ namespace Microsoft.DotNet.Tools.List
internal override Dictionary<string, Func<AppliedOption, CommandBase>> SubCommands => internal override Dictionary<string, Func<AppliedOption, CommandBase>> SubCommands =>
new Dictionary<string, Func<AppliedOption, CommandBase>> new Dictionary<string, Func<AppliedOption, CommandBase>>
{ {
{ "reference", o => new ListProjectToProjectReferencesCommand(o) } {
"reference",
o => new ListProjectToProjectReferencesCommand(
o,
ParseResult)
}
}; };
public static int Run(string[] args) public static int Run(string[] args)

View file

@ -14,7 +14,9 @@ namespace Microsoft.DotNet.Tools.List.ProjectToProjectReferences
{ {
private readonly string _fileOrDirectory; private readonly string _fileOrDirectory;
public ListProjectToProjectReferencesCommand(AppliedOption appliedCommand) public ListProjectToProjectReferencesCommand(
AppliedOption appliedCommand,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {

View file

@ -24,12 +24,14 @@ namespace Microsoft.DotNet.Tools.Remove
["reference"] = ["reference"] =
remove => new RemoveProjectToProjectReferenceCommand( remove => new RemoveProjectToProjectReferenceCommand(
remove["reference"], remove["reference"],
remove.Value<string>()), remove.Value<string>(),
ParseResult),
["package"] = ["package"] =
remove => new RemovePackageReferenceCommand( remove => new RemovePackageReferenceCommand(
remove["package"], remove["package"],
remove.Value<string>()) remove.Value<string>(),
ParseResult)
}; };
public static int Run(string[] args) public static int Run(string[] args)

View file

@ -18,7 +18,8 @@ namespace Microsoft.DotNet.Tools.Remove.PackageReference
public RemovePackageReferenceCommand( public RemovePackageReferenceCommand(
AppliedOption appliedCommand, AppliedOption appliedCommand,
string fileOrDirectory) string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {

View file

@ -17,7 +17,8 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
public RemoveProjectToProjectReferenceCommand( public RemoveProjectToProjectReferenceCommand(
AppliedOption appliedCommand, AppliedOption appliedCommand,
string fileOrDirectory) string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {

View file

@ -25,18 +25,21 @@ namespace Microsoft.DotNet.Tools.Sln
["add"] = ["add"] =
sln => new AddProjectToSolutionCommand( sln => new AddProjectToSolutionCommand(
sln["add"], sln["add"],
sln.Value<string>()), sln.Value<string>(),
ParseResult),
["list"] = ["list"] =
sln => new ListProjectsInSolutionCommand( sln => new ListProjectsInSolutionCommand(
sln["list"], sln["list"],
sln.Value<string>()), sln.Value<string>(),
ParseResult),
["remove"] = ["remove"] =
sln => sln =>
new RemoveProjectFromSolutionCommand( new RemoveProjectFromSolutionCommand(
sln["remove"], sln["remove"],
sln.Value<string>()) sln.Value<string>(),
ParseResult)
}; };
public static int Run(string[] args) public static int Run(string[] args)

View file

@ -17,7 +17,10 @@ namespace Microsoft.DotNet.Tools.Sln.Add
private readonly AppliedOption _appliedCommand; private readonly AppliedOption _appliedCommand;
private readonly string _fileOrDirectory; private readonly string _fileOrDirectory;
public AddProjectToSolutionCommand(AppliedOption appliedCommand, string fileOrDirectory) public AddProjectToSolutionCommand(
AppliedOption appliedCommand,
string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {

View file

@ -15,7 +15,10 @@ namespace Microsoft.DotNet.Tools.Sln.List
{ {
private readonly string _fileOrDirectory; private readonly string _fileOrDirectory;
public ListProjectsInSolutionCommand(AppliedOption appliedCommand, string fileOrDirectory) public ListProjectsInSolutionCommand(
AppliedOption appliedCommand,
string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {

View file

@ -17,7 +17,10 @@ namespace Microsoft.DotNet.Tools.Sln.Remove
private readonly AppliedOption _appliedCommand; private readonly AppliedOption _appliedCommand;
private readonly string _fileOrDirectory; private readonly string _fileOrDirectory;
public RemoveProjectFromSolutionCommand(AppliedOption appliedCommand, string fileOrDirectory) public RemoveProjectFromSolutionCommand(
AppliedOption appliedCommand,
string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{ {
if (appliedCommand == null) if (appliedCommand == null)
{ {

View file

@ -40,7 +40,7 @@
<PackageReference Include="Microsoft.Win32.Registry" Version="4.3.0" /> <PackageReference Include="Microsoft.Win32.Registry" Version="4.3.0" />
<PackageReference Include="Microsoft.Build" Version="$(CLI_MSBuild_Version)" /> <PackageReference Include="Microsoft.Build" Version="$(CLI_MSBuild_Version)" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="$(PlatformAbstractionsVersion)" /> <PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="$(PlatformAbstractionsVersion)" />
<PackageReference Include="Microsoft.DotNet.Cli.CommandLine" Version="0.1.0-alpha-111" /> <PackageReference Include="Microsoft.DotNet.Cli.CommandLine" Version="0.1.0-alpha-115" />
<PackageReference Include="Microsoft.TemplateEngine.Abstractions" Version="$(TemplateEngineVersion)" /> <PackageReference Include="Microsoft.TemplateEngine.Abstractions" Version="$(TemplateEngineVersion)" />
<PackageReference Include="Microsoft.TemplateEngine.Cli" Version="$(TemplateEngineVersion)" /> <PackageReference Include="Microsoft.TemplateEngine.Cli" Version="$(TemplateEngineVersion)" />
<PackageReference Include="Microsoft.TemplateEngine.Orchestrator.RunnableProjects" Version="$(TemplateEngineVersion)" /> <PackageReference Include="Microsoft.TemplateEngine.Orchestrator.RunnableProjects" Version="$(TemplateEngineVersion)" />

View file

@ -42,6 +42,6 @@
<PackageReference Include="xunit" Version="2.2.0-beta4-build3444" /> <PackageReference Include="xunit" Version="2.2.0-beta4-build3444" />
<PackageReference Include="xunit.netcore.extensions" Version="1.0.0-prerelease-00206" /> <PackageReference Include="xunit.netcore.extensions" Version="1.0.0-prerelease-00206" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="$(PlatformAbstractionsVersion)" /> <PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="$(PlatformAbstractionsVersion)" />
<PackageReference Include="Microsoft.DotNet.Cli.CommandLine" Version="0.1.0-alpha-111" /> <PackageReference Include="Microsoft.DotNet.Cli.CommandLine" Version="0.1.0-alpha-115" />
</ItemGroup> </ItemGroup>
</Project> </Project>