diff --git a/CliFx.Tests/CliApplicationTests.cs b/CliFx.Tests/CliApplicationTests.cs index 37309b5..12378b7 100644 --- a/CliFx.Tests/CliApplicationTests.cs +++ b/CliFx.Tests/CliApplicationTests.cs @@ -11,6 +11,8 @@ namespace CliFx.Tests [TestFixture] public partial class CliApplicationTests { + private const string TestVersionText = "v1.0"; + private static IEnumerable 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 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(); diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index b585bca..b2e80ad 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -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()) {