Fix not printing version when requested if used with stub default command

This commit is contained in:
Alexey Golub
2019-08-24 22:46:10 +03:00
parent a94b2296e1
commit b5d67ecf24
2 changed files with 25 additions and 8 deletions

View File

@@ -11,6 +11,8 @@ namespace CliFx.Tests
[TestFixture]
public partial class CliApplicationTests
{
private const string TestVersionText = "v1.0";
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
{
yield return new TestCaseData(
@@ -24,6 +26,18 @@ namespace CliFx.Tests
new[] {"cmd"},
"NamedCommand executed."
);
yield return new TestCaseData(
new[] {typeof(DefaultCommand)},
new[] {"--version"},
TestVersionText
);
yield return new TestCaseData(
new[] {typeof(NamedCommand)},
new[] {"--version"},
TestVersionText
);
}
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Smoke()
@@ -143,6 +157,7 @@ namespace CliFx.Tests
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.Build();
@@ -166,6 +181,7 @@ namespace CliFx.Tests
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.Build();
@@ -189,6 +205,7 @@ namespace CliFx.Tests
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.Build();

View File

@@ -92,6 +92,14 @@ namespace CliFx
// Get schemas for all available command types
var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_configuration.CommandTypes);
// Show version if version option was specified and command was not specified (only works on default command)
if (commandInput.IsVersionOptionSpecified() && !commandInput.IsCommandSpecified())
{
_console.Output.WriteLine(_metadata.VersionText);
return 0;
}
// Find command schema matching the name specified in the input
var targetCommandSchema = availableCommandSchemas.FindByName(commandInput.CommandName);
@@ -131,14 +139,6 @@ namespace CliFx
return isError ? -1 : 0;
}
// Show version if version option was specified and command was not specified (only works on default command)
if (commandInput.IsVersionOptionSpecified() && !commandInput.IsCommandSpecified())
{
_console.Output.WriteLine(_metadata.VersionText);
return 0;
}
// Show help if help option was specified
if (commandInput.IsHelpOptionSpecified())
{