File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -1080,6 +1080,13 @@ const ArgumentDefaultsHelpFormatter = _camelcase_alias(_callable(class ArgumentD
1080
1080
1081
1081
_get_help_string ( action ) {
1082
1082
let help = action . help
1083
+
1084
+ // If the action already appended its own default argument description,
1085
+ // then don't attempt to caculate an additional or new one.
1086
+ if ( action . has_default_help_hint === true ) {
1087
+ return help
1088
+ }
1089
+
1083
1090
// LEGACY (v1 compatibility): additional check for defaultValue needed
1084
1091
if ( ! action . help . includes ( '%(default)' ) && ! action . help . includes ( '%(defaultValue)' ) ) {
1085
1092
if ( action . default !== SUPPRESS ) {
@@ -1265,6 +1272,7 @@ const Action = _camelcase_alias(_callable(class Action extends _AttributeHolder(
1265
1272
this . required = required
1266
1273
this . help = help
1267
1274
this . metavar = metavar
1275
+ this . has_default_help_hint = false
1268
1276
}
1269
1277
1270
1278
_get_kwargs ( ) {
@@ -1325,8 +1333,10 @@ const BooleanOptionalAction = _camelcase_alias(_callable(class BooleanOptionalAc
1325
1333
}
1326
1334
}
1327
1335
1336
+ let did_appened_default_help_desc = false
1328
1337
if ( help !== undefined && default_value !== undefined ) {
1329
1338
help += ` (default: ${ default_value } )`
1339
+ did_appened_default_help_desc = true
1330
1340
}
1331
1341
1332
1342
super ( {
@@ -1340,6 +1350,8 @@ const BooleanOptionalAction = _camelcase_alias(_callable(class BooleanOptionalAc
1340
1350
help,
1341
1351
metavar
1342
1352
} )
1353
+
1354
+ this . has_default_help_hint = did_appened_default_help_desc
1343
1355
}
1344
1356
1345
1357
call ( parser , namespace , values , option_string = undefined ) {
Original file line number Diff line number Diff line change @@ -845,6 +845,29 @@ class ParserTestCase extends TestCase {
845
845
}
846
846
} ) . run ( )
847
847
848
+ ; ( new class TestBooleanOptionalActionHelpWithArgumentDefaultsHelpFormatter extends TestCase {
849
+ // Test that using BooleanOptionalAction with ArgumentDefaultsHelpFormatter
850
+ // doesn't result in duplicate '(default: <value>)' suffixes.
851
+
852
+ // See https://github.com/nodeca/argparse/issues/177
853
+
854
+ test_no_duplicate_default_desc ( ) {
855
+ const parser = argparse . ArgumentParser ( {
856
+ formatter_class : argparse . ArgumentDefaultsHelpFormatter ,
857
+ } )
858
+ parser . add_argument ( '-e' , '--an-example' , {
859
+ help : 'Example' ,
860
+ action : argparse . BooleanOptionalAction ,
861
+ default : true
862
+ } )
863
+ const parser_help = parser . format_help ( )
864
+ const default_regex_rs = parser_help . match ( / \( d e f a u l t : / g)
865
+ this . assertNotEqual ( default_regex_rs , null )
866
+ const num_default_arg_descriptions = default_regex_rs . length
867
+ this . assertEqual ( num_default_arg_descriptions , 1 )
868
+ }
869
+ } ) . run ( )
870
+
848
871
; ( new class TestBooleanOptionalActionRequired extends ParserTestCase {
849
872
/* Tests BooleanOptionalAction required */
850
873
You can’t perform that action at this time.
0 commit comments