3

Argparse displays message about the list of choices, like in this example:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--styles', choices=long_list_of_styles)

As I'm passing a long list, help message does not look good, in fact it looks confusing and it shadows other arguments by its presence, because of all those choices being printed.

Is there any way to tell Argparser not to print arguments choices?

vedar
  • 483
  • 8
  • 15
  • 1
    This was already asked before. You have to use a custom help formatter as shown in the linked question. – Bakuriu Jul 17 '16 at 13:57
  • Thanks I did not get any results with Google. However both solutions in linked answer are too complicated. I'll just remove choices. – vedar Jul 17 '16 at 14:08
  • My suggestion: don't use `choices` and manually check if the option given is a choice. Provide an option like `--help-styles` that will print the list of names of available styles, so that users can still access the list of supported choices. – Bakuriu Jul 17 '16 at 14:33

1 Answers1

9

The proposed duplicate, Python's argparse choices constrained printing presents a relatively complicated solution - a custom HelpFormatter. Or a custom type.

A higher vote question/answers is Python argparse: Lots of choices results in ugly help output

You'll find more with a [argparse] choices search.

The simplest solution is to set the metavar parameter. None displays nothing in the choices slot, but you probably want a short word

In [8]: styles=['one','two','three','four']
In [10]: parser = argparse.ArgumentParser()
In [11]: parser.add_argument('--styles', metavar='STYLE', choices=styles,
    ...:    help='list of choices: {%(choices)s}')
Out[11]: _StoreAction(option_strings=['--styles'], dest='styles', nargs=None, const=None, default=None, type=None, choices=['one', 'two', 'three', 'four'], help='list of choices: {%(choices)s}', metavar='STYLE')

In [12]: parser.print_help()
usage: ipython3 [-h] [--styles STYLE]

optional arguments:
  -h, --help      show this help message and exit
  --styles STYLE  list of choices: {one, two, three, four}

I included %(choices)s in the help to list them there. You could of course put your own summary there. A long list fits better there than in usage.

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Given that you have found this answer on an other duplicate you should have reopened the question *and closed it as a duplicate of the other one* instead of re-duplicating that again... – Bakuriu Jul 17 '16 at 20:41