diff --git a/Lib/argparse.py b/Lib/argparse.py index 02e98bbf920cf1..4be276811cbe61 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -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 diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 425b6bb3e0b4ee..97ae6f612df798 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -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"""