Skip to content

gh-95100: Replace - in a positional name with _ in dest. #95103

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

Open
wants to merge 3 commits into
base: main
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
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ def _get_positional_kwargs(self, dest, **kwargs):
kwargs['required'] = True

# return the keyword arguments with no option strings
return dict(kwargs, dest=dest, option_strings=[])
return dict(kwargs, dest=dest.replace("-", "_"), option_strings=[])

def _get_optional_kwargs(self, *args, **kwargs):
# determine short and long option strings
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,22 @@ class TestDisallowLongAbbreviationAllowsShortGroupingPrefix(ParserTestCase):
# Positional tests
# ================


class TestPositionalsDest(ParserTestCase):
"""Test a Positional containing - is converted to _ in the resulting namepsace"""

argument_signatures = [Sig('foo-bar')]

failures = []

successes = [

('abc', NS(foo_bar='abc')),

('def', NS(foo_bar='def'))

]

class TestPositionalsNargsNone(ParserTestCase):
"""Test a Positional that doesn't specify nargs"""

Expand Down