diff options
author | steven.bethard <devnull@localhost> | 2009-12-06 23:21:48 +0000 |
---|---|---|
committer | steven.bethard <devnull@localhost> | 2009-12-06 23:21:48 +0000 |
commit | d8a9e95d1809e508aa8720b26e98510ef414aa0e (patch) | |
tree | 7cf1702382868ee22034f2e6ac5fbc57b058ae9d | |
parent | b2ea34ebaac78374545e3167761acb3976807382 (diff) | |
download | argparse-d8a9e95d1809e508aa8720b26e98510ef414aa0e.tar.gz |
Add support for nargs='...', which allows emulation of command lines like "python [options] file [arguments]".
-rw-r--r-- | argparse.py | 22 | ||||
-rw-r--r-- | test/test_argparse.py | 13 |
2 files changed, 30 insertions, 5 deletions
diff --git a/argparse.py b/argparse.py index f29b6e0..16a778f 100644 --- a/argparse.py +++ b/argparse.py @@ -137,7 +137,8 @@ SUPPRESS = '==SUPPRESS==' OPTIONAL = '?' ZERO_OR_MORE = '*' ONE_OR_MORE = '+' -PARSER = '==PARSER==' +PARSER = 'A...' +REMAINDER = '...' # ============================= # Utility functions and classes @@ -606,7 +607,9 @@ class HelpFormatter(object): result = '[%s [%s ...]]' % get_metavar(2) elif action.nargs == ONE_OR_MORE: result = '%s [%s ...]' % get_metavar(2) - elif action.nargs is PARSER: + elif action.nargs == REMAINDER: + result = '...' + elif action.nargs == PARSER: result = '%s ...' % get_metavar(1) else: formats = ['%s' for _ in range(action.nargs)] @@ -1255,6 +1258,7 @@ class _ActionsContainer(object): return action.default return self._defaults.get(dest, None) + # ======================= # Adding argument actions # ======================= @@ -2129,8 +2133,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): elif nargs == ONE_OR_MORE: nargs_pattern = '(-*A[A-]*)' + # allow any number of options or arguments + elif nargs == REMAINDER: + nargs_pattern = '([-AO]*)' + # allow one argument followed by any number of options or arguments - elif nargs is PARSER: + elif nargs == PARSER: nargs_pattern = '(-*A[-AO]*)' # all others should be integers @@ -2150,7 +2158,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): # ======================== def _get_values(self, action, arg_strings): # for everything but PARSER args, strip out '--' - if action.nargs is not PARSER: + if action.nargs not in [PARSER, REMAINDER]: arg_strings = [s for s in arg_strings if s != '--'] # optional argument produces a default when not present @@ -2179,8 +2187,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): value = self._get_value(action, arg_string) self._check_value(action, value) + # REMAINDER arguments convert all values, checking none + elif action.nargs == REMAINDER: + value = [self._get_value(action, v) for v in arg_strings] + # PARSER arguments convert all values, but check only the first - elif action.nargs is PARSER: + elif action.nargs == PARSER: value = [self._get_value(action, v) for v in arg_strings] self._check_value(action, value[0]) diff --git a/test/test_argparse.py b/test/test_argparse.py index fadc1af..2356389 100644 --- a/test/test_argparse.py +++ b/test/test_argparse.py @@ -1173,6 +1173,19 @@ class TestNargsZeroOrMore(ParserTestCase): ] +class TestNargsRemainder(ParserTestCase): + """Tests specifying a positional with nargs=REMAINDER""" + + argument_signatures = [Sig('x'), Sig('y', nargs='...'), Sig('-z')] + failures = ['', '-z', '-z Z'] + successes = [ + ('X', NS(x='X', y=[], z=None)), + ('-z Z X', NS(x='X', y=[], z='Z')), + ('X A B -z Z', NS(x='X', y=['A', 'B', '-z', 'Z'], z=None)), + ('X Y --foo', NS(x='X', y=['Y', '--foo'], z=None)), + ] + + class TestOptionLike(ParserTestCase): """Tests options that may or may not be arguments""" |