diff -r 21d5e038120e Lib/argparse.py --- a/Lib/argparse.py Tue Mar 06 15:27:31 2012 +0100 +++ b/Lib/argparse.py Tue Mar 06 12:01:39 2012 -0300 @@ -852,7 +852,27 @@ help=help, metavar=metavar) + # has this action been called yet? + self.called = False + def __call__(self, parser, namespace, values, option_string=None): + # if this action has nargs = "*" and is greedy, we need to override + # the default value (if any) that comes with the namespace in the first + # call, and append new values on successive calls + if self.nargs == ZERO_OR_MORE and parser.greedy_star and self.called: + previous = getattr(namespace, self.dest, None) + + if previous is None: + previous = [] + elif not isinstance(previous, list): + previous = [previous] + + if isinstance(values, list): + values = previous + values + else: + values = previous + [values] + + self.called = True setattr(namespace, self.dest, values) @@ -1586,7 +1606,8 @@ fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', - add_help=True): + add_help=True, + greedy_star=False): if version is not None: import warnings @@ -1613,6 +1634,7 @@ self.formatter_class = formatter_class self.fromfile_prefix_chars = fromfile_prefix_chars self.add_help = add_help + self.greedy_star = greedy_star add_group = self.add_argument_group self._positionals = add_group(_('positional arguments')) @@ -1920,7 +1942,17 @@ # slice off the Positionals that we just parsed and return the # index at which the Positionals' string args stopped - positionals[:] = positionals[len(arg_counts):] + # keep positionals that take zero or more arguments if they are + # greedy so we can reuse them + if not self.greedy_star: + positionals[:] = positionals[len(arg_counts):] + else: + positionals[:len(arg_counts)] = [ + p + for p in positionals[:len(arg_counts)] + if p.nargs == ZERO_OR_MORE + ] + return start_index # consume Positionals and Optionals alternately, until we have