Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use <a href="http://docs.python.org/dev/library/argparse.html#action" rel="nofollow">custom actions</a> for this:</p> <pre><code>import argparse import sys class VerifyNoBuild(argparse.Action): def __call__(self, parser, args, values, option_string=None): # print 'No: {n} {v} {o}'.format(n=args, v=values, o=option_string) if args.build is not None: parser.error( '--build should not be used with --from or --interval') setattr(args, self.dest, values) class VerifyOnlyBuild(argparse.Action): def __call__(self, parser, args, values, option_string=None): # print 'Only: {n} {v} {o}'.format(n=args, v=values, o=option_string) if getattr(args, 'from') is not None: parser.error('--from should not be used with --build') if getattr(args, 'interval') is not None: parser.error('--interval should not be used with --build') setattr(args, self.dest, values) parser = argparse.ArgumentParser(description='A Tool') subparsers = parser.add_subparsers(help='sub-command help') # create the parser for the 'download' command download_parser = subparsers.add_parser('download', help='download help') download_parser.add_argument('--interval', type=int, help='interval help', action=VerifyNoBuild) download_parser.add_argument('--from', type=int, action=VerifyNoBuild) download_parser.add_argument('--build', type=int, action=VerifyOnlyBuild) args = parser.parse_args('download --from 1234 --interval 60'.split()) print(args) # Namespace(build=None, from=1234, interval=60) args = parser.parse_args('download --build 1432'.split()) print(args) # Namespace(build=1432, from=None, interval=None) args = parser.parse_args('download --build 1432 --from 1234'.split()) print(args) # usage: test.py download [-h] [--interval INTERVAL] [--from FROM] [--build BUILD] # test.py download: error: --build should not be used with --from or --interval args = parser.parse_args('download --build 1432 --interval 60'.split()) print(args) # usage: test.py download [-h] [--interval INTERVAL] [--from FROM] [--build BUILD] # test.py download: error: --build should not be used with --from or --interval </code></pre> <p>But really, I think this is shorter and simpler:</p> <pre><code>def parse_options(): parser = argparse.ArgumentParser(description='A Tool') subparsers = parser.add_subparsers(help='sub-command help') #create the parser for the 'download' command download_parser = subparsers.add_parser('download', help='download help') download_parser.add_argument('--interval', type=int, help='interval help') download_parser.add_argument('--from', type=int) download_parser.add_argument('--build', type=int) opt=parser.parse_args() from_interval=[getattr(opt,key) is not None for key in ('from','interval')] if opt.build is not None: if any(from_interval): sys.exit('error!') elif not all(from_interval): sys.exit('error!') return opt </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload