File tree Expand file tree Collapse file tree 4 files changed +31
-16
lines changed Expand file tree Collapse file tree 4 files changed +31
-16
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ public async Task When_the_version_option_is_specified_then_invocation_is_short_
37
37
{
38
38
var wasCalled = false ;
39
39
var rootCommand = new RootCommand ( ) ;
40
- rootCommand . SetAction ( ( _ ) => wasCalled = true ) ;
40
+ rootCommand . SetAction ( _ => wasCalled = true ) ;
41
41
42
42
var output = new StringWriter ( ) ;
43
43
@@ -46,6 +46,21 @@ public async Task When_the_version_option_is_specified_then_invocation_is_short_
46
46
wasCalled . Should ( ) . BeFalse ( ) ;
47
47
}
48
48
49
+ [ Fact ] // https://github.com/dotnet/command-line-api/issues/2628
50
+ public void When_the_version_option_is_specified_then_there_are_no_parse_errors_due_to_unspecified_subcommand ( )
51
+ {
52
+ Command subcommand = new ( "subcommand" ) ;
53
+ RootCommand root = new ( )
54
+ {
55
+ subcommand
56
+ } ;
57
+ subcommand . SetAction ( _ => 0 ) ;
58
+
59
+ var parseResult = root . Parse ( "--version" ) ;
60
+
61
+ parseResult . Errors . Should ( ) . BeEmpty ( ) ;
62
+ }
63
+
49
64
[ Fact ]
50
65
public async Task Version_option_appears_in_help ( )
51
66
{
Original file line number Diff line number Diff line change 2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System . CommandLine . Parsing ;
5
- using System . Linq ;
6
5
7
6
namespace System . CommandLine
8
7
{
Original file line number Diff line number Diff line change @@ -43,17 +43,10 @@ internal CommandResult(
43
43
internal override bool UseDefaultValueFor ( ArgumentResult argumentResult )
44
44
=> argumentResult . Argument . HasDefaultValue && argumentResult . Tokens . Count == 0 ;
45
45
46
- /// <param name="completeValidation">Only the inner most command goes through complete validation.</param>
47
- internal void Validate ( bool completeValidation )
46
+ internal void Validate ( bool isInnermostCommand )
48
47
{
49
- if ( completeValidation )
48
+ if ( isInnermostCommand )
50
49
{
51
- if ( Command . Action is null && Command . HasSubcommands )
52
- {
53
- SymbolResultTree . InsertFirstError (
54
- new ParseError ( LocalizationResources . RequiredCommandWasNotProvided ( ) , this ) ) ;
55
- }
56
-
57
50
if ( Command . HasValidators )
58
51
{
59
52
int errorCountBefore = SymbolResultTree . ErrorCount ;
@@ -71,12 +64,12 @@ internal void Validate(bool completeValidation)
71
64
72
65
if ( Command . HasOptions )
73
66
{
74
- ValidateOptionsAndAddDefaultResults ( completeValidation ) ;
67
+ ValidateOptionsAndAddDefaultResults ( isInnermostCommand ) ;
75
68
}
76
69
77
70
if ( Command . HasArguments )
78
71
{
79
- ValidateArgumentsAndAddDefaultResults ( completeValidation ) ;
72
+ ValidateArgumentsAndAddDefaultResults ( isInnermostCommand ) ;
80
73
}
81
74
}
82
75
Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ internal ParseResult Parse()
63
63
64
64
ValidateAndAddDefaultResults ( ) ;
65
65
66
+
66
67
if ( _isHelpRequested )
67
68
{
68
69
_symbolResultTree . Errors ? . Clear ( ) ;
@@ -373,17 +374,24 @@ private void AddCurrentTokenToUnmatched()
373
374
374
375
private void ValidateAndAddDefaultResults ( )
375
376
{
376
- // Only the inner most command goes through complete validation,
377
+ // Only the innermost command goes through complete validation,
377
378
// for other commands only a subset of options is checked.
378
- _innermostCommandResult . Validate ( completeValidation : true ) ;
379
+ _innermostCommandResult . Validate ( isInnermostCommand : true ) ;
379
380
380
381
CommandResult ? currentResult = _innermostCommandResult . Parent as CommandResult ;
381
382
while ( currentResult is not null )
382
383
{
383
- currentResult . Validate ( completeValidation : false ) ;
384
+ currentResult . Validate ( isInnermostCommand : false ) ;
384
385
385
386
currentResult = currentResult . Parent as CommandResult ;
386
387
}
388
+
389
+ if ( _primaryAction is null &&
390
+ _innermostCommandResult is { Command : { Action : null , HasSubcommands : true } } )
391
+ {
392
+ _symbolResultTree . InsertFirstError (
393
+ new ParseError ( LocalizationResources . RequiredCommandWasNotProvided ( ) , _innermostCommandResult ) ) ;
394
+ }
387
395
}
388
396
}
389
397
}
You can’t perform that action at this time.
0 commit comments