Don't show long options twice in print_help() from argparse

前端 未结 3 1335
一个人的身影
一个人的身影 2020-12-15 07:07

I have the following code:

parser = argparse.ArgumentParser(description=\'Postfix Queue Administration Tool\',
        prog=\'pqa\',
        usage=\'%(prog)s         


        
3条回答
  •  长情又很酷
    2020-12-15 07:41

    Another solution, using custom descriptions

    if you set the metavar='', the help line becomes:

    -q , --queue          Show information for 
    

    Here I suppress the regular help lines, and replace them with the description lines for a group:

    parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool',
            prog='pqa',
            usage='%(prog)s [-h] [-v,--version]',
            formatter_class=argparse.RawDescriptionHelpFormatter,
            )
    parser.add_argument('-l', '--list', action='store_true',
            help='Shows full overview of all queues')
    g = parser.add_argument_group(title='information options',
            description='''-q, --queue      Show information for 
    -d, --domain    Show information about a specific ''')
    g.add_argument('-q', '--queue', action='store', metavar='', dest='queue',
            help=argparse.SUPPRESS)
    g.add_argument('-d', '--domain', action='store', metavar='', dest='domain',
            help=argparse.SUPPRESS)
    parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
    parser.print_help()
    

    usage: pqa [-h] [-v,--version]
    
    Postfix Queue Administration Tool
    
    optional arguments:
      -h, --help     show this help message and exit
      -l, --list     Shows full overview of all queues
      -v, --version  show program's version number and exit
    
    information options:
      -q, --queue      Show information for 
      -d, --domain    Show information about a specific 
    

    Or you could put that information in the regular description. You already are using a custom usage line.

提交回复
热议问题