0

I'm building a command line argparser for my program and I try to give much detail in the -h option

I have the following code:

import argparse
legal_actions = ['act1', 'act2', 'act3']
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(help='available commands')

    parser_cmd = subparsers.add_parser("cmd")

    parser_cmd.add_argument("-a", "--action", type=str, metavar="", choices=legal_actions, required=True,
                            help='list of actions: {%(choices)s}')
    parser_cmd.add_argument("nargs", type=str, nargs='*',
                            help="the rest of the arguments required to perform an action")
    parser_cmd.set_defaults(func=cmd_handler)

python prog.py cmd -h will result the following prints in the command line

usage: cmd [-h] -a  [nargs [nargs ...]]

positional arguments:
  nargs           the rest of the arguments required to perform an action

optional arguments:
  -h, --help      show this help message and exit
  -a , --action   list of actions: {act1, act2, act3}

Every action requires a different number of arguments, so I want to add something that will describe the actions (from the list of actions) like:

actions availble:
    act1: requires 2 arguments (arg1, arg2)
    act2: requires 0 arguments ()
    act3: requires 1 arguments (arg1)

And I want it to have any link with the above "optional arguments", so it'll be easy to see the "acts" are under the -a option

Eran Moshe
  • 3,062
  • 2
  • 22
  • 41

1 Answers1

1

If you want to add more information, you can use the epilog-parameter:

from argparse import RawDescriptionHelpFormatter  # This is used to enable newlines in epilogs and descriptions(\n)
from argparse import ArgumentParser

description = 'Some description of program'
epilog = 'actions availble:\n\t'
epilog += 'act1: requires 2 arguments (arg1, arg2)\n\t'
epilog += 'act2: requires 0 arguments ()\n\t'
epilog += 'act3: requires 1 arguments (arg1)'

parser = argparse.ArgumentParser(description=description, epilog=epilog, 
                                formatter_class=RawTextHelpFormatter)

This will print out

actions availble:
    act1: requires 2 arguments (arg1, arg2)
    act2: requires 0 arguments ()
    act3: requires 1 arguments (arg1)

At the end of help output. The epilog-parameter can also be included in the add_parser() when using add_subparsers():

This object has a single method, add_parser(), which takes a command name and any ArgumentParser constructor arguments, and returns an ArgumentParser object that can be modified as usual.

NOTE: the default formatter will ignore newlines, so take a look at Python argparse: How to insert newline in the help text? where this is addressed, which describe how to replace the formatter á la:

ArgumentParser(..., formatter_class=RawDescriptionHelpFormatter)

Read more about the epilog-parameter in docs.

Thomas Fauskanger
  • 2,536
  • 1
  • 27
  • 42
  • `parser_cmd = subparsers.add_parser("cmd", epilog=epilog)` Will do the trick. Thanks! – Eran Moshe Feb 13 '18 at 15:57
  • Yes, the `add_parser()` method will accept any arguments that the `ArgumentParser` initializer would. Glad it could be of use :) – Thomas Fauskanger Feb 13 '18 at 16:00
  • damn. epilog completely ignore the '\n' and '\t' characters – Eran Moshe Feb 13 '18 at 16:00
  • Ah, yes, that might be an issue. Take a look at https://stackoverflow.com/questions/3853722/python-argparse-how-to-insert-newline-in-the-help-text which might solve the newline issue – Thomas Fauskanger Feb 13 '18 at 16:01
  • I've updated the answer to reflect your issue, does this help? – Thomas Fauskanger Feb 13 '18 at 16:05
  • 1
    `parser_cmd = subparsers.add_parser("cmd", epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter, help="run xgb_server from cmd line with cmd arguments")`. Notice that it has to be in parser_cmd, and in order to control description and epilogs, you have to use `argparse.RawDescriptionHelpFormatter` – Eran Moshe Feb 13 '18 at 16:26