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:
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()
When running the above function with the command line argument --help
it shows:
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
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:
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()
And when running the above function with the command line argument --help
it would show something similar to:
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}
I need to:
So I ask:
I have tried looking at the source for argparse, but as this modification feels pretty advanced I don´t know how to get going.
My answer doesn't attempt to extend argparse at all, but rather uses the available options of argparse as it is... Does this solve your situation?
import argparse
import textwrap
def main():
elements = ['a', 'b', 'c', 'd', 'e', 'f']
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog = textwrap.dedent('''\
sets in optional arguments:
ALFA\t\t{a,b,c,d,e,f}"
'''))
parser.add_argument(
'-i',
nargs='*',
choices=elements,
default=elements,
metavar="ALFA",
help='Space separated list of case sensitive element names.')
parser.add_argument(
'-e',
nargs='*',
choices=elements,
default=[],
metavar="ALFA",
help='Space separated list of case sensitive element names to '
'exclude from processing')
parser.parse_args()
usage: test.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}"
The nice thing about this approach is that you can freely name the metavar whatever you want for each flag, and you can manually format the epilog to reflect any format description as well. No subclassing needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With