@@ -14,6 +14,8 @@ public CommandExecutor(ITypeRegistrar registrar)
14
14
15
15
public async Task < int > Execute ( IConfiguration configuration , IEnumerable < string > args )
16
16
{
17
+ CommandTreeParserResult parsedResult ;
18
+
17
19
if ( configuration == null )
18
20
{
19
21
throw new ArgumentNullException ( nameof ( configuration ) ) ;
@@ -33,22 +35,43 @@ public async Task<int> Execute(IConfiguration configuration, IEnumerable<string>
33
35
var firstArgument = arguments . FirstOrDefault ( ) ;
34
36
if ( firstArgument != null )
35
37
{
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?
38
39
if ( firstArgument . Equals ( "--version" , StringComparison . OrdinalIgnoreCase ) ||
39
40
firstArgument . Equals ( "-v" , StringComparison . OrdinalIgnoreCase ) )
40
41
{
41
42
if ( configuration . Settings . ApplicationVersion != null )
42
43
{
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
+ }
46
69
}
47
70
}
48
71
}
49
72
50
73
// Parse and map the model against the arguments.
51
- var parsedResult = ParseCommandLineArguments ( model , configuration . Settings , arguments ) ;
74
+ parsedResult = ParseCommandLineArguments ( model , configuration . Settings , arguments ) ;
52
75
53
76
// Register the arguments with the container.
54
77
_registrar . RegisterInstance ( typeof ( CommandTreeParserResult ) , parsedResult ) ;
0 commit comments