argparse missing optparse capabilities?

rurpy at yahoo.com rurpy at yahoo.com
Thu Jan 5 19:30:29 EST 2012


On 01/05/2012 11:46 AM, Ian Kelly wrote:
> On Thu, Jan 5, 2012 at 11:14 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> On Thu, Jan 5, 2012 at 1:05 AM, rurpy at yahoo.com <rurpy at 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.
>[...]
>
> Sorry, I missed the second part of that.  You seem to be right, as far
> as I can tell from tinkering with it, all the positional arguments
> have to be in a single group.  If you have some positional arguments
> followed by an option followed by more positional arguments, and any
> of the arguments have a loose nargs quantifier ('?' or '*' or '+'),
> then you get an error.

OK, thanks for the second confirmation.  I was hoping there
was something I missed or some undocumented option to allow
intermixed optional and positional arguments with Argparse
but it appears not.

I notice that Optparse seems to intentionally provide this
capability since it offers a "disable_interspersed_args()"
method.  It is unfortunate that Argparse chose to not to
provide backward compatibility for this thus forcing some
users to continue using a deprecated module.



More information about the Python-list mailing list