Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is pretty hard to achieve that (including a nice help message) while only using the standard argparse functions. You can however easily test it yourself after parsing the arguments. You can describe the extra requirements in the epilogue or so. Note that it is unusual to use numbers as options, I had to use dest='two', since args.2 is not valid syntax.</p> <pre><code>#!/usr/bin/env python import argparse parser = argparse.ArgumentParser( description='bla bla', epilog='Note: arguments -3 and -4 are required when -2 is missing') parser.add_argument('-2', dest='two', action='store_true') parser.add_argument('-3', dest='three') parser.add_argument('-4', dest='four') parser.add_argument('-5', dest='five') args = parser.parse_args() if not args.two and (args.three is None or args.four is None): parser.error('arguments -3 and -4 are required when -2 is missing') print 'Good:', args </code></pre> <p>With these results:</p> <pre><code>[~]: ./test.py -h usage: test.py [-h] [-2] [-3 THREE] [-4 FOUR] [-5 FIVE] bla bla optional arguments: -h, --help show this help message and exit -2 -3 THREE -4 FOUR -5 FIVE Note: arguments -3 and -4 are required when -2 is missing [~]: ./test.py -2 Good: Namespace(five=None, four=None, three=None, two=True) [~]: ./test.py -3 a -4 b Good: Namespace(five=None, four='b', three='a', two=False) [~]: ./test.py -3 a usage: test.py [-h] [-2] [-3 THREE] [-4 FOUR] [-5 FIVE] test.py: error: arguments -3 and -4 are required when -2 is missing [~]: ./test.py -2 -5 c Good: Namespace(five='c', four=None, three=None, two=True) [~]: ./test.py -2 -3 a Good: Namespace(five=None, four=None, three='a', two=True) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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