1

When using argparse in python 3.6, I've noticed that if I supply the 'choices' attr when adding one of the arguments, then in the help menu's section for that argument, the list of choices themselves show up rather than the name of the argument.

Using this stackoverflow answer Python argparse: Lots of choices results in ugly help output, I found to use the 'metavar' and 'numargs' attrs to try and clean up the help menu. While this gets rid of the ugly choices being displayed as a hash, the name of the option still does not display at all in help.

Example...

This:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("works", type=int,
                    help="Shows up properly")
parser.add_argument("nope", type=int, choices=[1,2,3],
                    help="foobared")
parser.add_argument("nope2", type=int, choices=[1,2,3], nargs='?', metavar='',
                    help="still foobared")
parser.add_argument("why", type=int,
                    help="help me")
parser.parse_args()

Results in this help output:

$ python myparser.py --help
usage: myparser.py [-h] works {1,2,3}  why

positional arguments:
  works       Shows up properly
  {1,2,3}     foobared
              still foobared
  why         help me

optional arguments:
  -h, --help  show this help message and exit

This has to be simple... Can someone please advise me where I'm going wrong?

ffConundrums
  • 765
  • 9
  • 24
  • 1
    The user doesn't need to know the name of the option; the list of legal values is useful. By default, the name of the option is used as the metavariable that is displayed for arguments with too many legal values to display. The `help` argument is where you provide a *description* of what the list of values (or metavariable) means. – chepner Feb 21 '18 at 19:11
  • 1
    Sorry, maybe my whole thought process is off. I want them to be able to go optionA=something optionB=something someFlaghere (or whatever would be the proper syntax in python) - Is this possible? In the help, I would like to be able to display clearly what optionA, optionB, and someFlaghere mean – ffConundrums Feb 21 '18 at 19:12
  • 1
    ah - I see - so the answer is to supply the name of the option in the 'metavar' attribute. that works. Thanks! – ffConundrums Feb 21 '18 at 19:14
  • 1
    I would actually prefer to see `{1,2,3}`. `myparser.py [-h] works nope nope2 why` would imply that the second positional argument could be an arbitrary string (within the constraints of the defined `type`), rather than showing that it *has* to be either `1`, `2`, or `3`. The metavariable is used when you can't enumerate the legal values, whether because there are an infinite number of them, or simply too many to fit comfortably. – chepner Feb 21 '18 at 19:18
  • i see, so these are positional, meaning they have to give it in this order. I didn't want positional constraints but wanted/thought they were named... I see now that to make them identified by arg name rather than position, I need to pre-fix with '--' , i.e., parser.add_argument('--why', ). Thank you so much - I didn't realize this. Indeed, the original help menu display makes more sense now. – ffConundrums Feb 21 '18 at 19:24

1 Answers1

0

Although you answered the question yourself, I'd like to highlight the answer here to make other people easily find it. You can use the metavar (https://docs.python.org/3/library/argparse.html#metavar) key word in order to say to an argument parser which is the name of the argument and it will be displayed if you type --help. By default if you don't specify anything except name or flags (https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument) it is going to show that name just because it doesn't have any other mean to display it.

Michał Słodki
  • 168
  • 1
  • 2
  • 8