throw exceptions for command not found

This commit is contained in:
Jon Sequeira 2017-03-13 20:06:59 -07:00
parent ace4fe49f6
commit 2c96d8e8b9
4 changed files with 11 additions and 13 deletions

View file

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
namespace Microsoft.DotNet.Cli namespace Microsoft.DotNet.Cli
{ {
@ -29,14 +30,18 @@ namespace Microsoft.DotNet.Cli
var subcommandName = result.Command().Name; var subcommandName = result.Command().Name;
var create = SubCommands[subcommandName];
var command = create(result["dotnet"][CommandName]);
try try
{ {
var create = SubCommands[subcommandName];
var command = create(result["dotnet"][CommandName]);
return command.Execute(); return command.Execute();
} }
catch (KeyNotFoundException e)
{
throw new GracefulException(CommonLocalizableStrings.RequiredCommandNotPassed);
}
catch (GracefulException e) catch (GracefulException e)
{ {
Reporter.Error.WriteLine(e.Message.Red()); Reporter.Error.WriteLine(e.Message.Red());

View file

@ -9,15 +9,9 @@ namespace Microsoft.DotNet.Cli
[Obsolete("This is intended to facilitate refactoring during parser replacement and should not be used after that work is done.")] [Obsolete("This is intended to facilitate refactoring during parser replacement and should not be used after that work is done.")]
public class HelpException : Exception public class HelpException : Exception
{ {
public HelpException( public HelpException(string message) : base(message)
string message,
bool isError = false) : base(message)
{ {
IsError = isError;
Data.Add(ExceptionExtensions.CLI_User_Displayed_Exception, true); Data.Add(ExceptionExtensions.CLI_User_Displayed_Exception, true);
} }
public bool IsError { get; }
} }
} }

View file

@ -80,7 +80,7 @@ namespace Microsoft.DotNet.Cli
catch (HelpException e) catch (HelpException e)
{ {
Reporter.Output.Write(e.Message); Reporter.Output.Write(e.Message);
return e.IsError ? 1 : 0; return 0;
} }
catch (Exception e) when (e.ShouldBeDisplayedAsError()) catch (Exception e) when (e.ShouldBeDisplayedAsError())
{ {

View file

@ -12,7 +12,6 @@ namespace Microsoft.DotNet.Cli
"sln", "sln",
".NET modify solution file command", ".NET modify solution file command",
Accept.ExactlyOneArgument() Accept.ExactlyOneArgument()
.ExistingSlnFileOrDirectoryOnly()
.DefaultToCurrentDirectory() .DefaultToCurrentDirectory()
.With(name: "SLN_FILE"), .With(name: "SLN_FILE"),
CommonOptions.HelpOption(), CommonOptions.HelpOption(),