Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating better help from argparse when nargs='*'

Like many command line tools, mine accepts optional filenames. Argparse seems to support this via nargs='*', which is working for me as expected:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
        'files',
        help='file(s) to parse instead of stdin',
        nargs='*')

parser.parse_args()

However, the help output is bizarre:

$ ./help.py -h
usage: help.py [-h] [files [files ...]]

How can I avoid the nested optional and repeated parameter name? The repetition adds no information beyond [files ...], which is the traditional way optional parameter lists are indicated on Unix:

$ grep --help
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
        [-e pattern] [-f file] [--binary-files=value] [--color=when]
        [--context[=num]] [--directories=action] [--label] [--line-buffered]
        [--null] [pattern] [file ...]

$ ls --help
Usage:
  exa [options] [files...]

$ vim --help
Usage:
  nvim [options] [file ...]      Edit file(s)

Any help is appreciated. I'm trying argparse because using it seems to be a Python best practice, but this help output is a dealbreaker for me.

like image 649
Jeff Schwab Avatar asked Mar 01 '23 23:03

Jeff Schwab


1 Answers

This was fixed in Python 3.9, see https://bugs.python.org/issue38438 and commit a0ed99bc that fixed it.

Your code produces the usage message you expect if run on 3.9:

Python 3.9.0 (default, Oct 12 2020, 02:44:01) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('files', help='file(s) to parse instead of stdin', nargs='*')
_StoreAction(option_strings=[], dest='files', nargs='*', const=None, default=None, type=None, choices=None, help='file(s) to parse instead of stdin', metavar=None)
>>> parser.print_help()
usage: [-h] [files ...]
like image 52
Boris Avatar answered Mar 11 '23 01:03

Boris