@@ -15,12 +15,6 @@ namespace System.CommandLine.Tests
15
15
{
16
16
public partial class ParserTests
17
17
{
18
- private T GetValue < T > ( ParseResult parseResult , Option < T > option )
19
- => parseResult . GetValue ( option ) ;
20
-
21
- private T GetValue < T > ( ParseResult parseResult , Argument < T > argument )
22
- => parseResult . GetValue ( argument ) ;
23
-
24
18
[ Fact ]
25
19
public void An_option_can_be_checked_by_object_instance ( )
26
20
{
@@ -816,7 +810,7 @@ public void Commands_can_have_default_argument_values()
816
810
817
811
ParseResult result = command . Parse ( "command" ) ;
818
812
819
- GetValue ( result , argument )
813
+ result . GetValue ( argument )
820
814
. Should ( )
821
815
. Be ( "default" ) ;
822
816
@@ -861,7 +855,7 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_c
861
855
ParseResult result = command . Parse ( "command" ) ;
862
856
863
857
result . GetResult ( option ) . Should ( ) . NotBeNull ( ) ;
864
- GetValue ( result , option ) . Should ( ) . Be ( "the-default" ) ;
858
+ result . GetValue ( option ) . Should ( ) . Be ( "the-default" ) ;
865
859
}
866
860
867
861
[ Fact ]
@@ -926,6 +920,48 @@ public void When_an_argument_with_a_default_value_is_not_matched_then_there_are_
926
920
. BeEmpty ( ) ;
927
921
}
928
922
923
+ [ Fact ]
924
+ public void When_an_argument_with_a_default_value_is_matched_then_the_option_result_is_implicit ( )
925
+ {
926
+ var argument = new Argument < string > ( "the-arg" )
927
+ {
928
+ DefaultValueFactory = _ => "the-default"
929
+ } ;
930
+
931
+ var command = new Command ( "command" )
932
+ {
933
+ argument
934
+ } ;
935
+
936
+ var result = command . Parse ( "command the-explicit-value" ) ;
937
+
938
+ result . GetResult ( argument )
939
+ . Implicit
940
+ . Should ( )
941
+ . BeFalse ( ) ;
942
+ }
943
+
944
+ [ Fact ]
945
+ public void When_an_argument_with_a_default_value_is_not_matched_then_the_option_result_is_implicit ( )
946
+ {
947
+ var argument = new Argument < string > ( "the-arg" )
948
+ {
949
+ DefaultValueFactory = _ => "the-default"
950
+ } ;
951
+
952
+ var command = new Command ( "command" )
953
+ {
954
+ argument
955
+ } ;
956
+
957
+ var result = command . Parse ( "command" ) ;
958
+
959
+ result . GetResult ( argument )
960
+ . Implicit
961
+ . Should ( )
962
+ . BeTrue ( ) ;
963
+ }
964
+
929
965
[ Fact ]
930
966
public void Command_default_argument_value_does_not_override_parsed_value ( )
931
967
{
@@ -941,7 +977,7 @@ public void Command_default_argument_value_does_not_override_parsed_value()
941
977
942
978
var result = command . Parse ( "the-directory" ) ;
943
979
944
- GetValue ( result , argument )
980
+ result . GetValue ( argument )
945
981
. Name
946
982
. Should ( )
947
983
. Be ( "the-directory" ) ;
@@ -1125,7 +1161,7 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_op
1125
1161
1126
1162
var result = command . Parse ( input ) ;
1127
1163
1128
- GetValue ( result , optionX ) . Should ( ) . Be ( "-y" ) ;
1164
+ result . GetValue ( optionX ) . Should ( ) . Be ( "-y" ) ;
1129
1165
}
1130
1166
1131
1167
[ Fact ]
@@ -1144,9 +1180,9 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_bu
1144
1180
1145
1181
var result = command . Parse ( "-a -bc" ) ;
1146
1182
1147
- GetValue ( result , optionA ) . Should ( ) . Be ( "-bc" ) ;
1148
- GetValue ( result , optionB ) . Should ( ) . BeFalse ( ) ;
1149
- GetValue ( result , optionC ) . Should ( ) . BeFalse ( ) ;
1183
+ result . GetValue ( optionA ) . Should ( ) . Be ( "-bc" ) ;
1184
+ result . GetValue ( optionB ) . Should ( ) . BeFalse ( ) ;
1185
+ result . GetValue ( optionC ) . Should ( ) . BeFalse ( ) ;
1150
1186
}
1151
1187
1152
1188
[ Fact ]
@@ -1161,7 +1197,7 @@ public void Option_arguments_can_match_subcommands()
1161
1197
1162
1198
var result = root . Parse ( "-a subcommand" ) ;
1163
1199
1164
- GetValue ( result , optionA ) . Should ( ) . Be ( "subcommand" ) ;
1200
+ result . GetValue ( optionA ) . Should ( ) . Be ( "subcommand" ) ;
1165
1201
result . CommandResult . Command . Should ( ) . BeSameAs ( root ) ;
1166
1202
}
1167
1203
@@ -1182,7 +1218,7 @@ public void Arguments_can_match_subcommands()
1182
1218
1183
1219
result . CommandResult . Command . Should ( ) . BeSameAs ( subcommand ) ;
1184
1220
1185
- GetValue ( result , argument )
1221
+ result . GetValue ( argument )
1186
1222
. Should ( )
1187
1223
. BeEquivalentSequenceTo ( "one" , "two" , "three" , "subcommand" , "four" ) ;
1188
1224
@@ -1207,7 +1243,7 @@ public void Option_arguments_can_match_the_aliases_of_sibling_options_when_non_s
1207
1243
var result = command . Parse ( input ) ;
1208
1244
1209
1245
result . Errors . Should ( ) . BeEmpty ( ) ;
1210
- GetValue ( result , optionX ) . Should ( ) . Be ( "-y" ) ;
1246
+ result . GetValue ( optionX ) . Should ( ) . Be ( "-y" ) ;
1211
1247
}
1212
1248
1213
1249
[ Fact ]
@@ -1222,7 +1258,7 @@ public void Single_option_arguments_that_match_option_aliases_are_parsed_correct
1222
1258
1223
1259
var result = command . Parse ( "-x -x" ) ;
1224
1260
1225
- GetValue ( result , optionX ) . Should ( ) . Be ( "-x" ) ;
1261
+ result . GetValue ( optionX ) . Should ( ) . Be ( "-x" ) ;
1226
1262
}
1227
1263
1228
1264
[ Theory ]
@@ -1249,8 +1285,8 @@ public void Boolean_options_are_not_greedy(string commandLine)
1249
1285
1250
1286
result . Errors . Should ( ) . BeEmpty ( ) ;
1251
1287
1252
- GetValue ( result , optX ) . Should ( ) . BeTrue ( ) ;
1253
- GetValue ( result , optY ) . Should ( ) . BeTrue ( ) ;
1288
+ result . GetValue ( optX ) . Should ( ) . BeTrue ( ) ;
1289
+ result . GetValue ( optY ) . Should ( ) . BeTrue ( ) ;
1254
1290
}
1255
1291
1256
1292
[ Fact ]
@@ -1267,8 +1303,8 @@ public void Multiple_option_arguments_that_match_multiple_arity_option_aliases_a
1267
1303
1268
1304
var result = command . Parse ( "-x -x -x -y -y -x -y -y -y -x -x -y" ) ;
1269
1305
1270
- GetValue ( result , optionX ) . Should ( ) . BeEquivalentTo ( new [ ] { "-x" , "-y" , "-y" } ) ;
1271
- GetValue ( result , optionY ) . Should ( ) . BeEquivalentTo ( new [ ] { "-x" , "-y" , "-x" } ) ;
1306
+ result . GetValue ( optionX ) . Should ( ) . BeEquivalentTo ( new [ ] { "-x" , "-y" , "-y" } ) ;
1307
+ result . GetValue ( optionY ) . Should ( ) . BeEquivalentTo ( new [ ] { "-x" , "-y" , "-x" } ) ;
1272
1308
}
1273
1309
1274
1310
[ Fact ]
@@ -1285,7 +1321,7 @@ public void Bundled_option_arguments_that_match_option_aliases_are_parsed_correc
1285
1321
1286
1322
var result = command . Parse ( "-yxx" ) ;
1287
1323
1288
- GetValue ( result , optionX ) . Should ( ) . Be ( "x" ) ;
1324
+ result . GetValue ( optionX ) . Should ( ) . Be ( "x" ) ;
1289
1325
}
1290
1326
1291
1327
[ Fact ]
@@ -1302,8 +1338,8 @@ public void Argument_name_is_not_matched_as_a_token()
1302
1338
1303
1339
var result = command . Parse ( "name one two three" ) ;
1304
1340
1305
- GetValue ( result , nameArg ) . Should ( ) . Be ( "name" ) ;
1306
- GetValue ( result , columnsArg ) . Should ( ) . BeEquivalentTo ( "one" , "two" , "three" ) ;
1341
+ result . GetValue ( nameArg ) . Should ( ) . Be ( "name" ) ;
1342
+ result . GetValue ( columnsArg ) . Should ( ) . BeEquivalentTo ( "one" , "two" , "three" ) ;
1307
1343
}
1308
1344
1309
1345
[ Fact ]
@@ -1328,7 +1364,7 @@ public void Boolean_options_with_no_argument_specified_do_not_match_subsequent_a
1328
1364
1329
1365
var result = command . Parse ( "-v an-argument" ) ;
1330
1366
1331
- GetValue ( result , option ) . Should ( ) . BeTrue ( ) ;
1367
+ result . GetValue ( option ) . Should ( ) . BeTrue ( ) ;
1332
1368
}
1333
1369
1334
1370
[ Fact ]
@@ -1345,8 +1381,8 @@ public void When_a_command_line_has_unmatched_tokens_they_are_not_applied_to_sub
1345
1381
1346
1382
var result = command . Parse ( "-x 23 unmatched-token -y 42" ) ;
1347
1383
1348
- GetValue ( result , optionX ) . Should ( ) . Be ( "23" ) ;
1349
- GetValue ( result , optionY ) . Should ( ) . Be ( "42" ) ;
1384
+ result . GetValue ( optionX ) . Should ( ) . Be ( "23" ) ;
1385
+ result . GetValue ( optionY ) . Should ( ) . Be ( "42" ) ;
1350
1386
result . UnmatchedTokens . Should ( ) . BeEquivalentTo ( "unmatched-token" ) ;
1351
1387
}
1352
1388
@@ -1648,7 +1684,7 @@ public void Parsed_value_of_empty_string_arg_is_an_empty_string(string arg1, str
1648
1684
1649
1685
var result = rootCommand . Parse ( new [ ] { arg1 , arg2 } ) ;
1650
1686
1651
- GetValue ( result , option ) . Should ( ) . BeEmpty ( ) ;
1687
+ result . GetValue ( option ) . Should ( ) . BeEmpty ( ) ;
1652
1688
}
1653
1689
}
1654
1690
}
0 commit comments