Note that there are some explanatory texts on larger screens.

plurals
  1. POExtend argparse to write set names in the help text for optional argument choices and define those sets once at the end
    text
    copied!<h1>Example of the problem</h1> <p>If I have a list of valid option strings which is shared between several arguments, the list is written in multiple places in the help string. Making it harder to read:</p> <pre><code>def main(): elements = ['a', 'b', 'c', 'd', 'e', 'f'] parser = argparse.ArgumentParser() parser.add_argument( '-i', nargs='*', choices=elements, default=elements, help='Space separated list of case sensitive element names.') parser.add_argument( '-e', nargs='*', choices=elements, default=[], help='Space separated list of case sensitive element names to ' 'exclude from processing') parser.parse_args() </code></pre> <p>When running the above function with the command line argument <code>--help</code> it shows:</p> <pre><code>usage: arguments.py [-h] [-i [{a,b,c,d,e,f} [{a,b,c,d,e,f} ...]]] [-e [{a,b,c,d,e,f} [{a,b,c,d,e,f} ...]]] optional arguments: -h, --help show this help message and exit -i [{a,b,c,d,e,f} [{a,b,c,d,e,f} ...]] Space separated list of case sensitive element names. -e [{a,b,c,d,e,f} [{a,b,c,d,e,f} ...]] Space separated list of case sensitive element names to exclude from processing </code></pre> <hr> <h1>What would be nice</h1> <p>It would be nice if one could define an option list name, and in the help output write the option list name in multiple places and define it last of all. In theory it would work like this:</p> <pre><code>def main_optionlist(): elements = ['a', 'b', 'c', 'd', 'e', 'f'] # Two instances of OptionList are equal if and only if they # have the same name (ALFA in this case) ol = OptionList('ALFA', elements) parser = argparse.ArgumentParser() parser.add_argument( '-i', nargs='*', choices=ol, default=ol, help='Space separated list of case sensitive element names.') parser.add_argument( '-e', nargs='*', choices=ol, default=[], help='Space separated list of case sensitive element names to ' 'exclude from processing') parser.parse_args() </code></pre> <p>And when running the above function with the command line argument <code>--help</code> it would show something similar to:</p> <pre><code>usage: arguments.py [-h] [-i [ALFA [ALFA ...]]] [-e [ALFA [ALFA ...]]] optional arguments: -h, --help show this help message and exit -i [ALFA [ALFA ...]] Space separated list of case sensitive element names. -e [ALFA [ALFA ...]] Space separated list of case sensitive element names to exclude from processing sets in optional arguments: ALFA {a,b,c,d,e,f} </code></pre> <hr> <h1>Question</h1> <p>I need to:</p> <ul> <li>Replace the {'l', 'i', 's', 't', 's'} shown with the option name, in the optional arguments.</li> <li>At the end of the help text show a section explaining which elements each option name consists of.</li> </ul> <p>So I ask:</p> <ol> <li>Is this possible using argparse?</li> <li>Which classes would I have to inherit from and which methods would I need to override?</li> </ol> <p>I have tried looking at the source for argparse, but as this modification feels pretty advanced I don´t know how to get going.</p>
 

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