[argparse] mutually exclusive group with 2 sets of options

Joshua Landau joshua at landau.ws
Sun Aug 4 23:53:43 EDT 2013


On 5 August 2013 03:05, Francois Lafont <francois.lafont at nospam.invalid>wrote:

> Hello,
>
> Up. ;-)
>
> Le 04/08/2013 04:10, Francois Lafont a écrit :
>
> > Is it possible with argparse to have this syntax for a script?
> >
> > my-script (-a -b VALUE-B | -c -d VALUE-D)
> >
> > I would like to do this with the argparse module.
> >
> > Thanks in advance.
>
> I have found this post:
>
> https://groups.google.com/forum/#!searchin/argparse-users/exclusive/argparse-users/-o6GOwhCjbQ/m-PfL4OxLAIJ
>
> It was in 2011 and at that time, it was impossible to have the syntax
> above. I have the impression that it's impossible now yet. Am I wrong?
>
> If it's impossible yet, I could try a hack. With some checks, I think I
> could have the "(-a -b VALUE-B | -c -d VALUE-D)" behavior but I would like
> this syntax appear in the help output too and I have no idea to do it.


If possible you could try docopt. That should be able to do it quite easily.

You could write literally:

    My-Script

    Usage:
      my-script (-a -b VALUE-B | -c -d VALUE-D)

    Options:
      -b VALUE-B  Description
      -d VALUE-D  Description

as the grammar, but:

    My-Script

    Usage:
      my-script -a -b VALUE-B
      my-script -c -d VALUE-D

    Options:
      -b VALUE-B  Description
      -d VALUE-D  Description

would be preferred.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130805/a2adf44a/attachment.html>


More information about the Python-list mailing list