summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2009-04-20 00:41:33 +0000
committersteven.bethard <devnull@localhost>2009-04-20 00:41:33 +0000
commit4ef95d551085a5fffcc5a417e8ab6379e98e4e51 (patch)
tree1afce9b2ebf11d8774db23c2869c1ece9df2db21
parent5008b7c677c2ed5289ef3628d310fda403e370ed (diff)
downloadargparse-4ef95d551085a5fffcc5a417e8ab6379e98e4e51.tar.gz
Add support for empty string arguments and arguments that contain spaces.
-rw-r--r--argparse.py8
-rw-r--r--test/test_argparse.py22
2 files changed, 28 insertions, 2 deletions
diff --git a/argparse.py b/argparse.py
index 83fac14..bb641e2 100644
--- a/argparse.py
+++ b/argparse.py
@@ -1785,6 +1785,14 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
return result
def _parse_optional(self, arg_string):
+ # if it's an empty string, it was meant to be a positional
+ if not arg_string:
+ return None
+
+ # if it contains a space, it was meant to be a positional
+ if ' ' in arg_string:
+ return None
+
# if it doesn't start with a prefix, it was meant to be positional
if not arg_string[0] in self.prefix_chars:
return None
diff --git a/test/test_argparse.py b/test/test_argparse.py
index ba1a3ba..dd66239 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -203,8 +203,9 @@ class ParserTesterMetaclass(type):
def test_successes(self, tester):
parser = self._get_parser(tester)
- for args_str, expected_ns in tester.successes:
- args = args_str.split()
+ for args, expected_ns in tester.successes:
+ if isinstance(args, str):
+ args = args.split()
result_ns = self._parse_args(parser, args)
tester.assertEqual(expected_ns, result_ns)
@@ -1045,6 +1046,23 @@ class TestOptionalsNumericAndPositionals(ParserTestCase):
]
+class TestEmptyAndSpaceContainingArguments(ParserTestCase):
+
+ argument_signatures = [
+ Sig('x', nargs='?'),
+ Sig('-y'),
+ ]
+ failures = ['-y']
+ successes = [
+ ([''], NS(x='', y=None)),
+ (['a badger'], NS(x='a badger', y=None)),
+ (['-a badger'], NS(x='-a badger', y=None)),
+ (['-y', ''], NS(x=None, y='')),
+ (['-y', 'a badger'], NS(x=None, y='a badger')),
+ (['-y', '-a badger'], NS(x=None, y='-a badger')),
+ ]
+
+
class TestNargsZeroOrMore(ParserTestCase):
"""Tests specifying an args for an Optional that accepts zero or more"""