Search code examples
pythonargparse

How can I remove three-dots at the end of usage line in argparse


Python argparse keep putting space and three-dots ( ...) at the end of usage: line, example: usage: program.sh [-h] command [<options>...] .... Would it be possible to remove them?

Example code:

def helper():
    parser = argparse.ArgumentParser(
        "program.py",              
    )
    subparsers = parser.add_subparsers(dest="command", metavar="command [<options>...]")
    driver = subparsers.add_parser(
        "driver", help="Example script")
    driver.add_argument("--bn", type=int, default=0, help="Block number to start fetch blocks from")
    return parser

Output:

$ ./program.sh --help
usage: program.sh [-h] command [<options>...] ...

Solution

  • Direct answer: you could write your own usage summary:

    parser = argparse.ArgumentParser(
        "program.py",                  
        usage="usage: %(prog)s [-h] command [<options>...]",
    )
    

    However, there is something that does not make sense to me.

    The ... is caused by using subparsers. There is usually one subparser per command, for example: 3 commands, i.e. 3 subparsers:

    Usage:
    myprog cmd1 [options and args specific for cmd1]
    myprog cmd2 [different options and args for cmd2]
    myprog cmd3 [yet another set of options and args]
    

    And that can be hardly summarized in one line except with:

    Usage: myprog command ...
    

    unless you are not using any options and args for the commands, which means there is nothing to parse.

    So, if you want to get rid of the trailing ... and still have a valid usage synopsis, you probably do not need subparsers at all.