python - Don't show long options twice in print_help() from argparse -
i have following code:
parser = argparse.argumentparser(description='postfix queue administration tool', prog='pqa', usage='%(prog)s [-h] [-v,--version]') parser.add_argument('-l', '--list', action='store_true', help='shows full overview of queues') parser.add_argument('-q', '--queue', action='store', metavar='<queue>', dest='queue', help='show information <queue>') parser.add_argument('-d', '--domain', action='store', metavar='<domain>', dest='domain', help='show information specific <domain>') parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1') args = parser.parse_args() which gives me output this:
%./pqa usage: pqa [-h] [-v,--version] postfix queue administration tool optional arguments: -h, --help show message , exit -l, --list shows full overview of queues -q <queue>, --queue <queue> show information <queue> -d <domain>, --domain <domain> show information specific <domain> -v, --version show program's version number , exit i know how can 'group' commands have 2 versions (ie. long options) each show metavar.
this aesthetic issue on side, still fix this. have been reading manuals , texts on internet, either information isn't there or totally missing here :)
another solution, using custom descriptions
if set metavar='', line becomes:
-q , --queue show information <queue> here suppress regular lines, , replace them description lines 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 queues') g = parser.add_argument_group(title='information options', description='''-q, --queue <queue> show information <queue> -d, --domain <domain> show information specific <domain>''') g.add_argument('-q', '--queue', action='store', metavar='', dest='queue', help=argparse.suppress) g.add_argument('-d', '--domain', action='store', metavar='<domain>', 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 message , exit -l, --list shows full overview of queues -v, --version show program's version number , exit information options: -q, --queue <queue> show information <queue> -d, --domain <domain> show information specific <domain> or put information in regular description. using custom usage line.
Comments
Post a Comment