Skip to content

Commit e9f9f56

Browse files
FrankRay78patriksvensson
authored andcommitted
Check if the command has a version option on its setting class
1 parent cefb51d commit e9f9f56

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/Spectre.Console.Cli/Internal/CommandExecutor.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public CommandExecutor(ITypeRegistrar registrar)
1414

1515
public async Task<int> Execute(IConfiguration configuration, IEnumerable<string> args)
1616
{
17+
CommandTreeParserResult parsedResult;
18+
1719
if (configuration == null)
1820
{
1921
throw new ArgumentNullException(nameof(configuration));
@@ -33,22 +35,43 @@ public async Task<int> Execute(IConfiguration configuration, IEnumerable<string>
3335
var firstArgument = arguments.FirstOrDefault();
3436
if (firstArgument != null)
3537
{
36-
// Asking for version? Kind of a hack, but it's alright.
37-
// We should probably make this a bit better in the future.
38+
// Asking for version?
3839
if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) ||
3940
firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase))
4041
{
4142
if (configuration.Settings.ApplicationVersion != null)
4243
{
43-
var console = configuration.Settings.Console.GetConsole();
44-
console.MarkupLine(configuration.Settings.ApplicationVersion);
45-
return 0;
44+
// We need to check if the command has a version option on its setting class.
45+
// Do this by first parsing the command line args and checking the remaining args.
46+
try
47+
{
48+
// Parse and map the model against the arguments.
49+
parsedResult = ParseCommandLineArguments(model, configuration.Settings, arguments);
50+
}
51+
catch (Exception)
52+
{
53+
// Something went wrong with parsing the command line arguments,
54+
// however we know the first argument is a version option.
55+
var console = configuration.Settings.Console.GetConsole();
56+
console.MarkupLine(configuration.Settings.ApplicationVersion);
57+
return 0;
58+
}
59+
60+
// Check the parsed remaining args for the version options.
61+
if ((firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase) && parsedResult.Remaining.Parsed.Contains("-v")) ||
62+
(firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) && parsedResult.Remaining.Parsed.Contains("--version")))
63+
{
64+
// The version option is not a member of the command settings.
65+
var console = configuration.Settings.Console.GetConsole();
66+
console.MarkupLine(configuration.Settings.ApplicationVersion);
67+
return 0;
68+
}
4669
}
4770
}
4871
}
4972

5073
// Parse and map the model against the arguments.
51-
var parsedResult = ParseCommandLineArguments(model, configuration.Settings, arguments);
74+
parsedResult = ParseCommandLineArguments(model, configuration.Settings, arguments);
5275

5376
// Register the arguments with the container.
5477
_registrar.RegisterInstance(typeof(CommandTreeParserResult), parsedResult);

0 commit comments

Comments
 (0)