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: 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 Date: Thu, 5 Jan 2012 11:14:33 -0700 Subject: Re: argparse missing optparse capabilities? To: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, Jan 5, 2012 at 1:05 AM, 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, > > =A0myprogram.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): > > =A0import optparse > =A0def append_with_pos (option, opt_str, value, parser): > =A0 =A0 =A0 =A0if getattr (parser.values, option.dest, None) is None: > =A0 =A0 =A0 =A0 =A0 =A0setattr (parser.values, option.dest, []) > =A0 =A0 =A0 =A0getattr (parser.values, option.dest).append ((value, len > (parser.largs))) > =A0def opt_parse(): > =A0 =A0 =A0 =A0p =3D optparse.OptionParser() > =A0 =A0 =A0 =A0p.add_option ("-c", type=3Dint, > =A0 =A0 =A0 =A0 =A0 =A0action=3D'callback', callback=3Dappend_with_pos) > =A0 =A0 =A0 =A0opts, args =3D p.parse_args() > =A0 =A0 =A0 =A0return args, opts > =A0if __name__ =3D=3D '__main__': > =A0 =A0 =A0 =A0args, opts =3D opt_parse() > =A0 =A0 =A0 =A0print args, opts > > Output from the command line above: > =A0['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: > > =A0import argparse > =A0class AppendWithPos (argparse.Action): > =A0 =A0def __call__ (self, parser, namespace, values, > option_string=3DNone): > =A0 =A0 =A0 =A0if getattr (namespace, self.dest, None) is None: > =A0 =A0 =A0 =A0 =A0 =A0setattr (namespace, self.dest, []) > =A0 =A0 =A0 =A0getattr (namespace, self.dest).extend ((values, len > (parser.largs))) > =A0def arg_parse(): > =A0 =A0 =A0 =A0p =3D argparse.ArgumentParser (description=3D'description'= ) > =A0 =A0 =A0 =A0p.add_argument ('src', nargs=3D'*') > =A0 =A0 =A0 =A0p.add_argument ('-c', type=3Dint, action=3DAppendWithPos) > =A0 =A0 =A0 =A0opts =3D p.parse_args() > =A0 =A0 =A0 =A0return opts > =A0if __name__ =3D=3D '__main__': > =A0 =A0 =A0 =A0opts =3D arg_parse() > =A0 =A0 =A0 =A0print opts > > This fails with, > =A0AttributeError: 'ArgumentParser' object has no attribute 'largs' > and of course, the argparse.parser is not documented beyond how > to instantiate it. =A0Even were that not a problem, argparse complains > about "unrecognised arguments" for any positional arguments that > occur after an optional one. =A0I'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. =A0(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