[Tutor] Groups of mutually exclusive options

Alan Gauld alan.gauld at btinternet.com
Tue Apr 22 01:46:16 CEST 2014


On 21/04/14 22:05, rail shafigulin wrote:
> Does anybody know if there is a way to specify groups of mutually
> exclusive options using argparse module?
>

Sorry, I didn't follow your example. Can you explain what you mean in 
English? What would be the outcome if you succeeded?
What could the user do and not do?

> What I need is a way to specify groups of mutually exclusive options.
>   In other words
>
>>>>parser  =  argparse.ArgumentParser(prog='PROG')
>>>>group1  =  parser.add_argument_group()
>>>> group2 = parser.add_argument_group()
>>>>group1.add_argument('--foo',  action='store_true')
>>>>group1.add_argument('--bar1',  action='store_false')
>>>> group2.add_argument('--foo2', action = 'store_true')
>>>> group2.add_argument('--bar2', action = 'store_false')
>>>> mutually_exclusive_group = parser.add_mutually_exclusive_argument_group()
>>>> mutually_exclusive_group.add_group(group1)
>>>> mutually_exclusive_group.add_group(group2)
>>>>parser.parse_args(['--foo1',  '--bar1', '--bar2'])
> usage: PROG [-h] [--foo1, --bar1] | [--foo2, --bar2]
> PROG: error: argument --foo1 or bar1 not allowed with argument --foo2 or bar2


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list