Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51913 > unrolled thread

[argparse] mutually exclusive group with 2 sets of options

Started byFrancois Lafont <francois.lafont@nospam.invalid>
First post2013-08-04 04:10 +0200
Last post2013-08-05 22:15 +0000
Articles 11 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  [argparse] mutually exclusive group with 2 sets of options Francois Lafont <francois.lafont@nospam.invalid> - 2013-08-04 04:10 +0200
    Re: [argparse] mutually exclusive group with 2 sets of options Francois Lafont <francois.lafont@nospam.invalid> - 2013-08-05 04:05 +0200
      Re: [argparse] mutually exclusive group with 2 sets of options Joshua Landau <joshua@landau.ws> - 2013-08-05 04:53 +0100
    Re: [argparse] mutually exclusive group with 2 sets of options Miki Tebeka <miki.tebeka@gmail.com> - 2013-08-05 07:11 -0700
      RE: [argparse] mutually exclusive group with 2 sets of options "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-08-05 17:11 +0000
      Re: [argparse] mutually exclusive group with 2 sets of options Francois Lafont <francois.lafont@nospam.invalid> - 2013-08-06 00:13 +0200
    Re: [argparse] mutually exclusive group with 2 sets of options Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2013-08-05 22:01 +0200
      Re: [argparse] mutually exclusive group with 2 sets of options Francois Lafont <francois.lafont@nospam.invalid> - 2013-08-06 00:34 +0200
        Re: [argparse] mutually exclusive group with 2 sets of options Francois Lafont <francois.lafont@nospam.invalid> - 2013-08-07 01:18 +0200
          Re: [argparse] mutually exclusive group with 2 sets of options Francois Lafont <francois.lafont@nospam.invalid> - 2013-08-07 01:36 +0200
    RE: [argparse] mutually exclusive group with 2 sets of options "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-08-05 22:15 +0000

#51913 — [argparse] mutually exclusive group with 2 sets of options

FromFrancois Lafont <francois.lafont@nospam.invalid>
Date2013-08-04 04:10 +0200
Subject[argparse] mutually exclusive group with 2 sets of options
Message-ID<51fdb81b$0$2423$426a74cc@news.free.fr>
Hi,

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.


-- 
François Lafont

[toc] | [next] | [standalone]


#51946

FromFrancois Lafont <francois.lafont@nospam.invalid>
Date2013-08-05 04:05 +0200
Message-ID<51ff0881$0$2218$426a74cc@news.free.fr>
In reply to#51913
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.

-- 
François Lafont

[toc] | [prev] | [next] | [standalone]


#51947

FromJoshua Landau <joshua@landau.ws>
Date2013-08-05 04:53 +0100
Message-ID<mailman.198.1375674871.1251.python-list@python.org>
In reply to#51946

[Multipart message — attachments visible in raw view] — view raw

On 5 August 2013 03:05, Francois Lafont <francois.lafont@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.

[toc] | [prev] | [next] | [standalone]


#51961

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2013-08-05 07:11 -0700
Message-ID<6db08670-52e8-4ca2-aff4-82d611a29da6@googlegroups.com>
In reply to#51913
> 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.
You can probably do something similar using sub commands (http://docs.python.org/2/library/argparse.html#sub-commands).

[toc] | [prev] | [next] | [standalone]


#51973

From"Joseph L. Casale" <jcasale@activenetwerx.com>
Date2013-08-05 17:11 +0000
Message-ID<mailman.215.1375722699.1251.python-list@python.org>
In reply to#51961
> You can probably do something similar using sub commands
> (http://docs.python.org/2/library/argparse.html#sub-commands).

The problem here is that argparse does not pass the subparser into the
parsed args and shared args between subparsers need to be declared
each time. Come execution time, when you have shared args you end
up testing for various incantations of the invoked code, you're better
off omitting subparsers and performing conditional tests after parsing
for incompatible combinations.

It's a waste of typing to write out a mode followed by params to achieve
the ops goal, I've hit the same limitation. Certainly a weakness of argparse
in my opinion. It's also a tough sell to force a user to install a package just
for the cli if you go with docopt. I'd love to see argparse expand or docopt
get included...

jlc

[toc] | [prev] | [next] | [standalone]


#51981

FromFrancois Lafont <francois.lafont@nospam.invalid>
Date2013-08-06 00:13 +0200
Message-ID<5200237c$0$2103$426a74cc@news.free.fr>
In reply to#51961
Le 05/08/2013 16:11, Miki Tebeka a écrit :

> You can probably do something similar using sub commands (http://docs.python.org/2/library/argparse.html#sub-commands).

Yes, but this is not the same syntax. I want this syntax :

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

I don't want this syntax:

my-script (subcommad-a -b VALUE-B | subcommand-c -d VALUE-D)

In fact, in this post, I have simplified my question to put the stress just on my problem. In the real life, my script already uses the subcommands (and no problem with that). But I need to have mutually exclusive groups with 2 *sets* of options for compatibility reasons with another programs.

-- 
François Lafont

[toc] | [prev] | [next] | [standalone]


#51977

FromRafael Durán Castañeda <rafadurancastaneda@gmail.com>
Date2013-08-05 22:01 +0200
Message-ID<mailman.217.1375732889.1251.python-list@python.org>
In reply to#51913
El 04/08/13 04:10, Francois Lafont escribió:
> Hi,
>
> 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 think you are looking for exclusive groups:

http://docs.python.org/2.7/library/argparse.html#argparse.add_mutually_exclusive_group

HTH

[toc] | [prev] | [next] | [standalone]


#51985

FromFrancois Lafont <francois.lafont@nospam.invalid>
Date2013-08-06 00:34 +0200
Message-ID<52002868$0$2551$426a74cc@news.free.fr>
In reply to#51977
Le 05/08/2013 22:01, Rafael Durán Castañeda a écrit :

> I think you are looking for exclusive groups:
> 
> http://docs.python.org/2.7/library/argparse.html#argparse.add_mutually_exclusive_group

Yes... but no. The doc explains you can do this:

my-script (-b VALUE-B | -d VALUE-D) 

ie mutally exclusive group with *just* *one* option incompatible with another one.

But, apparently, you can't do that:

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

ie mutually exclusive group with one *set* of option*s* incompatible with another *set* of option*s*. This is why I have posted my message. I have read the documentation before to post it. ;-)

I know docopt but I prefer argparse. My script have subcommands and some of them have common options, so I appreciate the parser objects and the inheritance between parser objects (with parents parameter of argparse.ArgumentParser class).

-- 
François Lafont

[toc] | [prev] | [next] | [standalone]


#52083

FromFrancois Lafont <francois.lafont@nospam.invalid>
Date2013-08-07 01:18 +0200
Message-ID<5201843a$0$2051$426a34cc@news.free.fr>
In reply to#51985
Hi,

On relfection, it's clear that:

1. the "(-a -b VALUE-B | -c -d VALUE-D)" syntax is not
implemented by the argparse module;

2. and get this syntax with "argparse + hacking" is not very clean.

So, finally I'll use the docopt module version 0.6.1.
For the inheritance of common options, I'll used something like
that (even if I prefer the oriented object side of the argparse
module):



[the main file]
#-----------------------------------
import importlib
import sys
from docopt import docopt

help_message ="""
Usage:
  my-script (-h | --help)
  my-script <command> [<args>...]

Options:
  -h, --help
        Show this help message and exit.

Available commands:
  command1  Description1...
  command2  Description2...

See 'my-script <command> -h' for more information on a specific command.
"""

if __name__ == '__main__':

    args = docopt(help_message, options_first=True)
    command = args['<command>']

    try:
        m = importlib.import_module('mymodule.' + command)
    except ImportError:
        print("Sorry, the %s command doesn't exist. See 'my-script -h'.") % (command,)
        sys.exit(1)

    args = docopt(m.help_message, options_first=False)
    m.run(args)
#-----------------------------------



[a file for each specific command, mymodule/command1.py etc.]
#-----------------------------------
import mymodule.common as common

help_message = """
Usage:
  my-script subcommand1 (-h | --help)
  my-script subcommand1 %s -w <warning> -c <critical>

Options:
%s
  -w <warning>, --warning=<warning>
        Some help.
  -c <critical>, --critical=<critical>
        Some help.
""" % (common.common_syntax, common.common_help,)


def run(args):
    pass
#-----------------------------------



[and a file for the common syntax, mymodule/common.py]
#-----------------------------------
common_syntax = """-H <host-address> -t <timeout>
      (--v2c -C <community> | -l <login> -x <passwd> -X <privpass> -L <protocols>)"""

common_help = """  -h, --help
        Show this help message and exit.
  -H <host-address>, --host=<host-address>
        Some help.
  -t <timeout>, --timeout=<timeout>
        Some help.
  --v2c
        Some help.
  -C <community>, --community=<community>
        Set the community password for SNMP V2c.
   # etc.
   # etc.
"""
#-----------------------------------

Thank you all.

-- 
François Lafont

[toc] | [prev] | [next] | [standalone]


#52085

FromFrancois Lafont <francois.lafont@nospam.invalid>
Date2013-08-07 01:36 +0200
Message-ID<5201888a$0$2224$426a34cc@news.free.fr>
In reply to#52083
Le 07/08/2013 01:18, Francois Lafont a écrit :

> For the inheritance of common options, I'll used something like
> that (even if I prefer the oriented object side of the argparse
> module):

But I admit that this is a very simple and intelligent module. ;-)

-- 
François Lafont

[toc] | [prev] | [next] | [standalone]


#51982

From"Joseph L. Casale" <jcasale@activenetwerx.com>
Date2013-08-05 22:15 +0000
Message-ID<mailman.219.1375741018.1251.python-list@python.org>
In reply to#51913
> I think you are looking for exclusive groups:
> 
> http://docs.python.org/2.7/library/argparse.html#argparse.add_mutually_excl
> usive_group

No. That links first doc line in that method shows the very point we are all discussing:

"Create a mutually exclusive group. argparse will make sure that only one
of the arguments in the mutually exclusive group was present on the
command line:"

Op requires more than one per group, this method "make sure that only one
of the arguments" is accepted.

jlc

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web