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.
// 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
{
public abstract class CommandBase
{
protected CommandBase(ParseResult parseResult)
{
ShowHelpOrErrorIfAppropriate(parseResult);
}
protected virtual void ShowHelpOrErrorIfAppropriate(ParseResult parseResult)
{
parseResult.ShowHelpOrErrorIfAppropriate();
}
public abstract int Execute();
}
}
}

View file

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

View file

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

View file

@ -22,7 +22,8 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
public AddPackageReferenceCommand(
AppliedOption appliedCommand,
string fileOrDirectory)
string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{
if (appliedCommand == null)
{
@ -38,6 +39,16 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
_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()
{
var projectFilePath = string.Empty;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -40,7 +40,7 @@
<PackageReference Include="Microsoft.Win32.Registry" Version="4.3.0" />
<PackageReference Include="Microsoft.Build" Version="$(CLI_MSBuild_Version)" />
<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.Cli" 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.netcore.extensions" Version="1.0.0-prerelease-00206" />
<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>
</Project>