File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
System.CommandLine.Tests/Binding
System.CommandLine/Parsing Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 88using System . IO ;
99using System . Linq ;
1010using System . Net ;
11+ using System . Threading ;
1112using Xunit ;
1213
1314namespace System . CommandLine . Tests . Binding
@@ -195,6 +196,30 @@ public void Bool_parses_as_true_when_the_option_has_been_applied(string commandL
195196 . BeTrue ( ) ;
196197 }
197198
199+ [ Fact ] // https://github.com/dotnet/command-line-api/issues/2210
200+ public void Nullable_bool_with_unparseable_argument_does_not_throw ( )
201+ {
202+ CliRootCommand rootCommand = new ( ) ;
203+ CliOption < bool ? > option = new ( "--test" ) ;
204+ rootCommand . Options . Add ( option ) ;
205+ var result = rootCommand . Parse ( "--test ouch" ) ;
206+
207+ result . Invoking ( r => r . GetValue ( option ) )
208+ . Should ( ) . NotThrow ( ) ;
209+ }
210+
211+ [ Fact ] // https://github.com/dotnet/command-line-api/issues/2210
212+ public void Bool_with_unparseable_argument_does_not_throw ( )
213+ {
214+ CliRootCommand rootCommand = new ( ) ;
215+ CliOption < bool > option = new ( "--test" ) ;
216+ rootCommand . Options . Add ( option ) ;
217+ var result = rootCommand . Parse ( "--test ouch" ) ;
218+
219+ result . Invoking ( r => r . GetValue ( option ) )
220+ . Should ( ) . NotThrow ( ) ;
221+ }
222+
198223 [ Theory ]
199224 [ InlineData ( "the-command -x" ) ]
200225 [ InlineData ( "the-command -x true" ) ]
Original file line number Diff line number Diff line change @@ -246,9 +246,10 @@ private void ParseOptionArguments(OptionResult optionResult)
246246 break ;
247247 }
248248 }
249- else if ( argument . ValueType == typeof ( bool ) &&
249+ else if ( ( argument . ValueType == typeof ( bool ) || argument . ValueType == typeof ( bool ? ) ) &&
250250 ! bool . TryParse ( CurrentToken . Value , out _ ) )
251251 {
252+ // Don't greedily consume the following token for bool. The presence of the option token (i.e. a flag) is sufficient.
252253 break ;
253254 }
254255
You can’t perform that action at this time.
0 commit comments