File tree Expand file tree Collapse file tree 3 files changed +68
-4
lines changed
System.CommandLine/Parsing Expand file tree Collapse file tree 3 files changed +68
-4
lines changed Original file line number Diff line number Diff line change @@ -702,6 +702,22 @@ public void A_command_with_subcommands_is_invalid_to_invoke_if_it_has_no_handler
702702 e . SymbolResult . Symbol . Name . Equals ( "inner" ) ) ;
703703 }
704704
705+ [ Fact ]
706+ public void A_root_command_is_invalid_if_it_has_no_handler ( )
707+ {
708+ var rootCommand = new RootCommand ( ) ;
709+ var inner = new Command ( "inner" ) ;
710+ rootCommand . Add ( inner ) ;
711+
712+ var result = rootCommand . Parse ( "" ) ;
713+
714+ result . Errors
715+ . Should ( )
716+ . ContainSingle (
717+ e => e . Message . Equals ( ValidationMessages . Instance . RequiredCommandWasNotProvided ( ) ) &&
718+ e . SymbolResult . Symbol == rootCommand ) ;
719+ }
720+
705721 [ Fact ]
706722 public void A_command_with_subcommands_is_valid_to_invoke_if_it_has_a_handler ( )
707723 {
Original file line number Diff line number Diff line change 1+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
2+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
5+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
6+
7+ using System . CommandLine . Builder ;
8+ using System . CommandLine . Parsing ;
9+ using FluentAssertions ;
10+ using Xunit ;
11+
12+ namespace System . CommandLine . Tests
13+ {
14+ public class UseParseErrorReportingTests
15+ {
16+ [ Fact ] // https://github.com/dotnet/command-line-api/issues/817
17+ public void Parse_error_reporting_reports_error_when_help_is_used_and_required_subcommand_is_missing ( )
18+ {
19+ var root = new RootCommand
20+ {
21+ new Command ( "inner" )
22+ } ;
23+
24+ var parser = new CommandLineBuilder ( root )
25+ . UseParseErrorReporting ( )
26+ . UseHelp ( )
27+ . Build ( ) ;
28+
29+ var parseResult = parser . Parse ( "" ) ;
30+
31+ parseResult . Errors . Should ( ) . NotBeEmpty ( ) ;
32+
33+ var result = parser . Invoke ( "" ) ;
34+
35+ result . Should ( ) . Be ( 1 ) ;
36+ }
37+ }
38+ }
Original file line number Diff line number Diff line change @@ -226,10 +226,20 @@ private void ValidateCommandResult()
226226
227227 private void ValidateCommandHandler ( )
228228 {
229- if ( _innermostCommandResult . Command is Command cmd &&
230- cmd . Handler == null &&
231- cmd . Children . OfType < ICommand > ( ) . Any ( ) &&
232- ! cmd . Options . OfType < HelpOption > ( ) . Any ( ) )
229+ if ( ! ( _innermostCommandResult . Command is Command cmd ) ||
230+ cmd . Handler != null )
231+ {
232+ return ;
233+ }
234+
235+ if ( ! cmd . Children . OfType < ICommand > ( ) . Any ( ) )
236+ {
237+ return ;
238+ }
239+
240+ if ( ! _innermostCommandResult
241+ . Children
242+ . Select ( o => o . Symbol is HelpOption ) . Any ( ) )
233243 {
234244 _errors . Insert ( 0 ,
235245 new ParseError (
You can’t perform that action at this time.
0 commit comments