Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18554
| References | <84bd6e5a-1849-43dd-a633-7233efee89c4@f11g2000yql.googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-01-05 11:14 -0700 |
| Subject | Re: argparse missing optparse capabilities? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4455.1325787306.27778.python-list@python.org> (permalink) |
On Thu, Jan 5, 2012 at 1:05 AM, rurpy@yahoo.com <rurpy@yahoo.com> wrote:
> I have optparse code that parses a command line containing
> intermixed positional and optional arguments, where the optional
> arguments set the context for the following positional arguments.
> For example,
>
> myprogram.py arg1 -c33 arg2 arg3 -c44 arg4
>
> 'arg1' is processed in a default context, 'args2' and 'arg3' in
> context '33', and 'arg4' in context '44'.
>
> I am trying to do the same using argparse but it appears to be
> not doable in a documented way.
>
> Here is the working optparse code (which took 30 minutes to write
> using just the optparse docs):
>
> import optparse
> def append_with_pos (option, opt_str, value, parser):
> if getattr (parser.values, option.dest, None) is None:
> setattr (parser.values, option.dest, [])
> getattr (parser.values, option.dest).append ((value, len
> (parser.largs)))
> def opt_parse():
> p = optparse.OptionParser()
> p.add_option ("-c", type=int,
> action='callback', callback=append_with_pos)
> opts, args = p.parse_args()
> return args, opts
> if __name__ == '__main__':
> args, opts = opt_parse()
> print args, opts
>
> Output from the command line above:
> ['arg1', 'arg2', 'arg3', 'arg4'] {'c': [(33, 1), (44, 3)]}
> The -c values are stored as (value, arglist_position) tuples.
>
> Here is an attempt to convert to argparse using the guidelines
> in the argparse docs:
>
> import argparse
> class AppendWithPos (argparse.Action):
> def __call__ (self, parser, namespace, values,
> option_string=None):
> if getattr (namespace, self.dest, None) is None:
> setattr (namespace, self.dest, [])
> getattr (namespace, self.dest).extend ((values, len
> (parser.largs)))
> def arg_parse():
> p = argparse.ArgumentParser (description='description')
> p.add_argument ('src', nargs='*')
> p.add_argument ('-c', type=int, action=AppendWithPos)
> opts = p.parse_args()
> return opts
> if __name__ == '__main__':
> opts = arg_parse()
> print opts
>
> This fails with,
> AttributeError: 'ArgumentParser' object has no attribute 'largs'
> and of course, the argparse.parser is not documented beyond how
> to instantiate it. Even were that not a problem, argparse complains
> about "unrecognised arguments" for any positional arguments that
> occur after an optional one. I've been farting with this code for
> a day now.
>
> Any suggestions on how I can convince argparse to do what optparse
> does easily will be very welcome. (I tried parse_known_args() but
> that breaks help and requires me to detect truly unknown arguments.)
>
> (Python 2.7.1 if it matters and apologies if Google mangles
> the formatting of this post.)
You have the namespace object in your custom action. Instead of
"len(parser.largs)", couldn't you just do "len(namespace.src)"?
Cheers,
Ian
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
argparse missing optparse capabilities? "rurpy@yahoo.com" <rurpy@yahoo.com> - 2012-01-05 00:05 -0800
Re: argparse missing optparse capabilities? "rurpy@yahoo.com" <rurpy@yahoo.com> - 2012-01-05 00:26 -0800
Re: argparse missing optparse capabilities? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-05 10:19 +0100
Re: argparse missing optparse capabilities? "rurpy@yahoo.com" <rurpy@yahoo.com> - 2012-01-05 09:36 -0800
Re: argparse missing optparse capabilities? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-05 11:14 -0700
Re: argparse missing optparse capabilities? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-05 11:46 -0700
Re: argparse missing optparse capabilities? "rurpy@yahoo.com" <rurpy@yahoo.com> - 2012-01-05 16:30 -0800
csiph-web