Show help info when no or wrong arguments are given

Fixes #5383, fixes #5384
This commit is contained in:
Mikkel Nylander Bundgaard 2017-05-04 01:01:35 +02:00
parent f3a4b22457
commit 18d71a8471
8 changed files with 124 additions and 4 deletions

View file

@ -42,14 +42,19 @@ namespace Microsoft.DotNet.Cli
} }
catch (KeyNotFoundException) catch (KeyNotFoundException)
{ {
throw new GracefulException(CommonLocalizableStrings.RequiredCommandNotPassed); return ReportError(CommonLocalizableStrings.RequiredCommandNotPassed);
} }
catch (GracefulException e) catch (GracefulException e)
{ {
Reporter.Error.WriteLine(e.Message.Red()); return ReportError(e.Message);
ParseResult.ShowHelp();
return 1;
} }
} }
private int ReportError(string errorMessage)
{
Reporter.Error.WriteLine(errorMessage.Red());
ParseResult.ShowHelp();
return 1;
}
} }
} }

View file

@ -27,6 +27,21 @@ Options:
-f, --framework <FRAMEWORK> Add reference only when targeting a specific framework -f, --framework <FRAMEWORK> Add reference only when targeting a specific framework
"; ";
private const string AddCommandHelpText = @".NET Add Command
Usage: dotnet add [options] <PROJECT> [command]
Arguments:
<PROJECT> The project file to operate on. If a file is not specified, the command will search the current directory for one.
Options:
-h, --help Show help information
Commands:
package <PACKAGE_NAME> .NET Add Package reference Command
reference <args> .NET Add Project to Project reference Command
";
const string FrameworkNet451Arg = "-f net451"; const string FrameworkNet451Arg = "-f net451";
const string ConditionFrameworkNet451 = "== 'net451'"; const string ConditionFrameworkNet451 = "== 'net451'";
const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0"; const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0";
@ -103,6 +118,7 @@ Options:
.ExecuteWithCapturedOutput($"add {commandName}"); .ExecuteWithCapturedOutput($"add {commandName}");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().Be("Required command was not provided."); cmd.StdErr.Should().Be("Required command was not provided.");
cmd.StdOut.Should().BeVisuallyEquivalentTo(AddCommandHelpText);
} }
[Fact] [Fact]

View file

@ -24,6 +24,20 @@ Options:
-h, --help Show help information -h, --help Show help information
"; ";
private const string ListCommandHelpText = @".NET List Command
Usage: dotnet list [options] <PROJECT> [command]
Arguments:
<PROJECT> The project file to operate on. If a file is not specified, the command will search the current directory for one.
Options:
-h, --help Show help information
Commands:
reference .NET Core Project-to-Project dependency viewer
";
const string FrameworkNet451Arg = "-f net451"; const string FrameworkNet451Arg = "-f net451";
const string ConditionFrameworkNet451 = "== 'net451'"; const string ConditionFrameworkNet451 = "== 'net451'";
const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0"; const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0";
@ -48,6 +62,7 @@ Options:
.ExecuteWithCapturedOutput($"list {commandName}"); .ExecuteWithCapturedOutput($"list {commandName}");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().Be("Required command was not provided."); cmd.StdErr.Should().Be("Required command was not provided.");
cmd.StdOut.Should().BeVisuallyEquivalentTo(ListCommandHelpText);
} }
[Fact] [Fact]

View file

@ -23,6 +23,21 @@ Options:
-h, --help Show help information -h, --help Show help information
"; ";
private const string RemoveCommandHelpText = @".NET Remove Command
Usage: dotnet remove [options] <PROJECT> [command]
Arguments:
<PROJECT> The project file to operate on. If a file is not specified, the command will search the current directory for one.
Options:
-h, --help Show help information
Commands:
package <PACKAGE_NAME> .NET Remove Package reference Command.
reference <args> .NET Remove Project to Project reference Command
";
[Theory] [Theory]
[InlineData("--help")] [InlineData("--help")]
[InlineData("-h")] [InlineData("-h")]
@ -42,6 +57,7 @@ Options:
.ExecuteWithCapturedOutput($"remove {commandName}"); .ExecuteWithCapturedOutput($"remove {commandName}");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().Be("Required command was not provided."); cmd.StdErr.Should().Be("Required command was not provided.");
cmd.StdOut.Should().BeVisuallyEquivalentTo(RemoveCommandHelpText);
} }
[Fact] [Fact]

View file

@ -26,6 +26,21 @@ Options:
-f, --framework <FRAMEWORK> Remove reference only when targeting a specific framework -f, --framework <FRAMEWORK> Remove reference only when targeting a specific framework
"; ";
private const string RemoveCommandHelpText = @".NET Remove Command
Usage: dotnet remove [options] <PROJECT> [command]
Arguments:
<PROJECT> The project file to operate on. If a file is not specified, the command will search the current directory for one.
Options:
-h, --help Show help information
Commands:
package <PACKAGE_NAME> .NET Remove Package reference Command.
reference <args> .NET Remove Project to Project reference Command
";
const string FrameworkNet451Arg = "-f net451"; const string FrameworkNet451Arg = "-f net451";
const string ConditionFrameworkNet451 = "== 'net451'"; const string ConditionFrameworkNet451 = "== 'net451'";
const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0"; const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0";
@ -129,6 +144,7 @@ Options:
.ExecuteWithCapturedOutput($"remove {commandName}"); .ExecuteWithCapturedOutput($"remove {commandName}");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().Be("Required command was not provided."); cmd.StdErr.Should().Be("Required command was not provided.");
cmd.StdOut.Should().BeVisuallyEquivalentTo(RemoveCommandHelpText);
} }
[Fact] [Fact]

View file

@ -26,6 +26,23 @@ Arguments:
Options: Options:
-h, --help Show help information -h, --help Show help information
"; ";
private const string SlnCommandHelpText = @".NET modify solution file command
Usage: dotnet sln [options] <SLN_FILE> [command]
Arguments:
<SLN_FILE> Solution file to operate on. If not specified, the command will search the current directory for one.
Options:
-h, --help Show help information
Commands:
add <args> .NET Add project(s) to a solution file Command
list .NET List project(s) in a solution file Command
remove <args> .NET Remove project(s) from a solution file Command
";
private ITestOutputHelper _output; private ITestOutputHelper _output;
public GivenDotnetSlnAdd(ITestOutputHelper output) public GivenDotnetSlnAdd(ITestOutputHelper output)
@ -193,6 +210,7 @@ EndGlobal
.ExecuteWithCapturedOutput($"sln {commandName}"); .ExecuteWithCapturedOutput($"sln {commandName}");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().Be("Required command was not provided."); cmd.StdErr.Should().Be("Required command was not provided.");
cmd.StdOut.Should().BeVisuallyEquivalentTo(SlnCommandHelpText);
} }
[Fact] [Fact]

View file

@ -24,6 +24,22 @@ Options:
-h, --help Show help information -h, --help Show help information
"; ";
private const string SlnCommandHelpText = @".NET modify solution file command
Usage: dotnet sln [options] <SLN_FILE> [command]
Arguments:
<SLN_FILE> Solution file to operate on. If not specified, the command will search the current directory for one.
Options:
-h, --help Show help information
Commands:
add <args> .NET Add project(s) to a solution file Command
list .NET List project(s) in a solution file Command
remove <args> .NET Remove project(s) from a solution file Command
";
[Theory] [Theory]
[InlineData("--help")] [InlineData("--help")]
[InlineData("-h")] [InlineData("-h")]
@ -44,6 +60,7 @@ Options:
.ExecuteWithCapturedOutput($"sln {commandName}"); .ExecuteWithCapturedOutput($"sln {commandName}");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().Be("Required command was not provided."); cmd.StdErr.Should().Be("Required command was not provided.");
cmd.StdOut.Should().BeVisuallyEquivalentTo(SlnCommandHelpText);
} }
[Fact] [Fact]

View file

@ -25,6 +25,22 @@ Options:
-h, --help Show help information -h, --help Show help information
"; ";
private const string SlnCommandHelpText = @".NET modify solution file command
Usage: dotnet sln [options] <SLN_FILE> [command]
Arguments:
<SLN_FILE> Solution file to operate on. If not specified, the command will search the current directory for one.
Options:
-h, --help Show help information
Commands:
add <args> .NET Add project(s) to a solution file Command
list .NET List project(s) in a solution file Command
remove <args> .NET Remove project(s) from a solution file Command
";
private const string ExpectedSlnContentsAfterRemove = @" private const string ExpectedSlnContentsAfterRemove = @"
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
@ -187,6 +203,7 @@ EndGlobal
.ExecuteWithCapturedOutput($"sln {commandName}"); .ExecuteWithCapturedOutput($"sln {commandName}");
cmd.Should().Fail(); cmd.Should().Fail();
cmd.StdErr.Should().Be("Required command was not provided."); cmd.StdErr.Should().Be("Required command was not provided.");
cmd.StdOut.Should().BeVisuallyEquivalentTo(SlnCommandHelpText);
} }
[Theory] [Theory]