argparse -- mutually exclusive sets of arguments?

Discussion in 'Python' started by Roy Smith, Nov 23, 2012.

  1. Roy Smith

    Roy Smith Guest

    My command either takes two positional arguments (in which case, both
    are required):

    $ command foo bar

    or the name of a config file (in which case, the positional arguments
    are forbidden):

    $ command --config file

    How can I represent this with argparse; add_mutually_exclusive_group()
    isn't quite the right thing. It could specify that foo and --config are
    mutually exclusive, but not (as far as I can see) the more complicated
    logic described above.
    Roy Smith, Nov 23, 2012
    #1
    1. Advertising

  2. Roy Smith

    Terry Reedy Guest

    On 11/23/2012 1:46 PM, Roy Smith wrote:
    > My command either takes two positional arguments (in which case, both
    > are required):
    >
    > $ command foo bar
    >
    > or the name of a config file (in which case, the positional arguments
    > are forbidden):
    >
    > $ command --config file
    >
    > How can I represent this with argparse; add_mutually_exclusive_group()
    > isn't quite the right thing. It could specify that foo and --config are
    > mutually exclusive, but not (as far as I can see) the more complicated
    > logic described above.


    Make the two positional arguments be one duple?
    Or tell argparse that all three are optional and handle the 'more
    complicated logic' in your own code after argparse returns.


    --
    Terry Jan Reedy
    Terry Reedy, Nov 23, 2012
    #2
    1. Advertising

  3. Roy Smith

    Ian Kelly Guest

    On Fri, Nov 23, 2012 at 11:46 AM, Roy Smith <> wrote:
    > My command either takes two positional arguments (in which case, both
    > are required):
    >
    > $ command foo bar
    >
    > or the name of a config file (in which case, the positional arguments
    > are forbidden):
    >
    > $ command --config file
    >
    > How can I represent this with argparse; add_mutually_exclusive_group()
    > isn't quite the right thing. It could specify that foo and --config are
    > mutually exclusive, but not (as far as I can see) the more complicated
    > logic described above.


    I don't think you could even do the former. An argument must be
    optional in order to be mutually exclusive with anything. This works,
    however:

    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--config', type=file)
    group.add_argument('--foobar', nargs=2, metavar=('FOO', 'BAR'))
    print parser.parse_args()

    Downsides are that the resulting interface is a little more formal and
    a little less friendly, and unless you customize the action you'll
    wind up with a 2-element 'foobar' arg instead of separate 'foo' and
    'bar' args.
    Ian Kelly, Nov 24, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin

    Mutually exclusive

    ALuPin, Sep 7, 2004, in forum: VHDL
    Replies:
    2
    Views:
    689
    Mike Treseler
    Sep 7, 2004
  2. js
    Replies:
    2
    Views:
    876
  3. Stimp
    Replies:
    2
    Views:
    793
    Stimp
    Oct 24, 2005
  4. Adam Funk
    Replies:
    11
    Views:
    309
    Adam Funk
    Dec 31, 2011
  5. Francois Lafont
    Replies:
    10
    Views:
    164
    Francois Lafont
    Aug 7, 2013
Loading...

Share This Page