With python argaparse "choices" the default help looks like this:
>>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
positional arguments:
  {rock,paper,scissors}
Which works if it's obvious how to pick one, but not so great if each choice needs its own mini-help.
Is there any way to write one line help per choice in a clean way, something along these lines:
parser.add_argument("action",
                    choices=[
                        ["status", help="Shows current status of sys"],
                        ["load", help="Load data in DB"],
                        ["dump", help="Dump data to csv"],
                    ],
argparse doesn't support this format. Here is my solution. It isn't nice, but it works.
from argparse import ArgumentParser, RawTextHelpFormatter
choices_helper = { "status": "Shows current status of sys",
                   "load": "Load data in DB",
                   "dump": "Dump data to csv"}
parser = ArgumentParser(description='test', formatter_class=RawTextHelpFormatter)    
parser.add_argument("action",
                    choices=choices_helper,
                    help='\n'.join("{}: {}".format(key, value) for key, value in choices_helper.iteritems()))
Try to use Sub-commands(subparsers) is the better idea.
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