From 595bce353fc65c44168df31a9cd3e29b9059b486 Mon Sep 17 00:00:00 2001 From: "steven.bethard" Date: Mon, 7 Dec 2009 00:56:45 +0000 Subject: Fix bug where --si=64 was deemed ambiguous when both --si and --size were in the parser. --- argparse.py | 7 +++++++ test/test_argparse.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/argparse.py b/argparse.py index 16a778f..8b96e62 100644 --- a/argparse.py +++ b/argparse.py @@ -2036,6 +2036,13 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): action = self._option_string_actions[arg_string] return action, arg_string, None + # if the option string before the "=" is present, return the action + if '=' in arg_string: + option_string, explicit_arg = arg_string.split('=', 1) + if option_string in self._option_string_actions: + action = self._option_string_actions[option_string] + return action, option_string, explicit_arg + # search through all possible prefixes of the option string # and all actions in the parser for possible interpretations option_tuples = self._get_option_tuples(arg_string) diff --git a/test/test_argparse.py b/test/test_argparse.py index 2356389..a7eb74a 100644 --- a/test/test_argparse.py +++ b/test/test_argparse.py @@ -401,6 +401,25 @@ class TestOptionalsDoubleDashPartialMatch(ParserTestCase): ] +class TestOptionalsDoubleDashPrefixMatch(ParserTestCase): + """Tests when one double-dash option string is a prefix of another""" + + argument_signatures = [ + Sig('--badger', action='store_true'), + Sig('--ba'), + ] + failures = ['--bar', '--b', '--ba', '--b=2', '--badge 5'] + successes = [ + ('', NS(badger=False, ba=None)), + ('--ba X', NS(badger=False, ba='X')), + ('--ba=X', NS(badger=False, ba='X')), + ('--bad', NS(badger=True, ba=None)), + ('--badg', NS(badger=True, ba=None)), + ('--badge', NS(badger=True, ba=None)), + ('--badger', NS(badger=True, ba=None)), + ] + + class TestOptionalsSingleDoubleDash(ParserTestCase): """Test an Optional with single- and double-dash option strings""" -- cgit v1.2.1