Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18554
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'value,': 0.04; '(python': 0.05; 'args': 0.05; 'attribute': 0.07; 'none:': 0.07; 'subject:missing': 0.07; '(self,': 0.09; '__name__': 0.09; 'args,': 0.09; 'arguments,': 0.09; 'namespace': 0.09; 'none)': 0.09; 'optparse': 0.09; 'positional': 0.09; 'output': 0.10; 'am,': 0.12; 'stored': 0.13; "'__main__':": 0.16; '1),': 0.16; '[])': 0.16; 'argparse': 0.16; 'complains': 0.16; 'instantiate': 0.16; 'len': 0.16; 'namespace,': 0.16; 'parser,': 0.16; 'subject:capabilities': 0.16; '\xa0(i': 0.16; '\xa0def': 0.16; '\xa0print': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'arguments': 0.18; 'processed': 0.18; 'convert': 0.19; 'jan': 0.19; '(which': 0.19; 'appears': 0.19; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'trying': 0.21; 'detect': 0.21; 'header:In- Reply-To:1': 0.22; 'breaks': 0.23; 'documented': 0.23; 'formatting': 0.23; 'optional': 0.23; 'welcome.': 0.23; '\xa0if': 0.23; 'cc:2**0': 0.24; 'command': 0.24; 'received:74.125.82.174': 0.24; 'code': 0.25; 'tried': 0.27; 'beyond': 0.28; 'message- id:@mail.gmail.com': 0.28; 'unknown': 0.29; "skip:' 10": 0.29; 'cc:addr:python.org': 0.29; 'arguments.': 0.30; 'context,': 0.30; 'subject:?': 0.31; 'skip:( 20': 0.31; 'does': 0.32; 'values': 0.32; 'thu,': 0.32; 'instead': 0.33; 'object': 0.33; 'it.': 0.34; 'convince': 0.34; 'received:74.125.82': 0.35; 'google': 0.35; 'suggestions': 0.35; 'skip:" 20': 0.35; 'example,': 0.37; 'skip:" 10': 0.37; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'using': 0.38; 'skip:o 20': 0.38; 'skip:\xa0 10': 0.39; "couldn't": 0.39; 'help': 0.39; '8bit%:8': 0.40; 'custom': 0.61; 'your': 0.61; 'course,': 0.62; 'took': 0.63; 'here': 0.65; 'email addr:yahoo.com': 0.66; 'minutes': 0.66; '2012': 0.67; 'day': 0.69; 'with,': 0.73; 'to:addr:yahoo.com': 0.83; '2.7.1': 0.84; '__call__': 0.84; 'doable': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=ceXNUiLz4kj0IVVCgea6c7nty3nKlnQvRrkIUJ3zlDo=; b=F8tydDo9353QX7ZMUjz9z64VkVeuF7jQNvG7bEtbt17fyM3i9neBfIuF/QIQsV6Bps 5Oct2xVVablq1SpBkz3LIO0/7k9Hsjo6Dgw8aKGcIh2psqLF2aGBYFdVKEhLLG2zGEec u1v0VSWFfKfBIiJkLdgNL8ZzZukSm5n78zSME= |
| MIME-Version | 1.0 |
| In-Reply-To | <84bd6e5a-1849-43dd-a633-7233efee89c4@f11g2000yql.googlegroups.com> |
| References | <84bd6e5a-1849-43dd-a633-7233efee89c4@f11g2000yql.googlegroups.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Thu, 5 Jan 2012 11:14:33 -0700 |
| Subject | Re: argparse missing optparse capabilities? |
| To | "rurpy@yahoo.com" <rurpy@yahoo.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4455.1325787306.27778.python-list@python.org> (permalink) |
| Lines | 79 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1325787306 news.xs4all.nl 6947 [2001:888:2000:d::a6]:36257 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:18554 |
Show key headers only | View raw
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