Skip to content

Commit 1f9bd44

Browse files
committed
have helpers indicate if they added a default arg desc, and use that to avoid duplciating it in ArgumentDefaultsHelpFormatter, fixes #177
1 parent a645a9a commit 1f9bd44

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: argparse.js

+12
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,13 @@ const ArgumentDefaultsHelpFormatter = _camelcase_alias(_callable(class ArgumentD
10801080

10811081
_get_help_string(action) {
10821082
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+
10831090
// LEGACY (v1 compatibility): additional check for defaultValue needed
10841091
if (!action.help.includes('%(default)') && !action.help.includes('%(defaultValue)')) {
10851092
if (action.default !== SUPPRESS) {
@@ -1265,6 +1272,7 @@ const Action = _camelcase_alias(_callable(class Action extends _AttributeHolder(
12651272
this.required = required
12661273
this.help = help
12671274
this.metavar = metavar
1275+
this.has_default_help_hint = false
12681276
}
12691277

12701278
_get_kwargs() {
@@ -1325,8 +1333,10 @@ const BooleanOptionalAction = _camelcase_alias(_callable(class BooleanOptionalAc
13251333
}
13261334
}
13271335

1336+
let did_appened_default_help_desc = false
13281337
if (help !== undefined && default_value !== undefined) {
13291338
help += ` (default: ${default_value})`
1339+
did_appened_default_help_desc = true
13301340
}
13311341

13321342
super({
@@ -1340,6 +1350,8 @@ const BooleanOptionalAction = _camelcase_alias(_callable(class BooleanOptionalAc
13401350
help,
13411351
metavar
13421352
})
1353+
1354+
this.has_default_help_hint = did_appened_default_help_desc
13431355
}
13441356

13451357
call(parser, namespace, values, option_string = undefined) {

Diff for: test/test_argparse.js

+23
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,29 @@ class ParserTestCase extends TestCase {
845845
}
846846
}).run()
847847

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(/\(default: /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+
848871
;(new class TestBooleanOptionalActionRequired extends ParserTestCase {
849872
/* Tests BooleanOptionalAction required */
850873

0 commit comments

Comments
 (0)