Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate default arg descriptions when using ArgumentDefaultsHelpFormatter and BooleanOptionalAction #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions argparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,13 @@ const ArgumentDefaultsHelpFormatter = _camelcase_alias(_callable(class ArgumentD

_get_help_string(action) {
let help = action.help

// If the action already appended its own default argument description,
// then don't attempt to caculate an additional or new one.
if (action.has_default_help_hint === true) {
return help
}

// LEGACY (v1 compatibility): additional check for defaultValue needed
if (!action.help.includes('%(default)') && !action.help.includes('%(defaultValue)')) {
if (action.default !== SUPPRESS) {
Expand Down Expand Up @@ -1265,6 +1272,7 @@ const Action = _camelcase_alias(_callable(class Action extends _AttributeHolder(
this.required = required
this.help = help
this.metavar = metavar
this.has_default_help_hint = false
}

_get_kwargs() {
Expand Down Expand Up @@ -1325,8 +1333,10 @@ const BooleanOptionalAction = _camelcase_alias(_callable(class BooleanOptionalAc
}
}

let did_appened_default_help_desc = false
if (help !== undefined && default_value !== undefined) {
help += ` (default: ${default_value})`
did_appened_default_help_desc = true
}

super({
Expand All @@ -1340,6 +1350,8 @@ const BooleanOptionalAction = _camelcase_alias(_callable(class BooleanOptionalAc
help,
metavar
})

this.has_default_help_hint = did_appened_default_help_desc
}

call(parser, namespace, values, option_string = undefined) {
Expand Down
23 changes: 23 additions & 0 deletions test/test_argparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,29 @@ class ParserTestCase extends TestCase {
}
}).run()

;(new class TestBooleanOptionalActionHelpWithArgumentDefaultsHelpFormatter extends TestCase {
// Test that using BooleanOptionalAction with ArgumentDefaultsHelpFormatter
// doesn't result in duplicate '(default: <value>)' suffixes.

// See https://github.com/nodeca/argparse/issues/177

test_no_duplicate_default_desc() {
const parser = argparse.ArgumentParser({
formatter_class: argparse.ArgumentDefaultsHelpFormatter,
})
parser.add_argument('-e', '--an-example', {
help: 'Example',
action: argparse.BooleanOptionalAction,
default: true
})
const parser_help = parser.format_help()
const default_regex_rs = parser_help.match(/\(default: /g)
this.assertNotEqual(default_regex_rs, null)
const num_default_arg_descriptions = default_regex_rs.length
this.assertEqual(num_default_arg_descriptions, 1)
}
}).run()

;(new class TestBooleanOptionalActionRequired extends ParserTestCase {
/* Tests BooleanOptionalAction required */

Expand Down