Skip to content

Sorting case-sensitively and alphabetically argparse's help output when using parents #145975

@jljsijus

Description

@jljsijus

Feature or enhancement

Proposal:

import argparse

common_opts = argparse.ArgumentParser(add_help = False)
common_opts.add_argument("-F", "--foo")
common_opts.add_argument("-t", "--test")


specific_opts = argparse.ArgumentParser(
        parents = [common_opts],
        )
specific_opts.add_argument("-c", "--rfc")
specific_opts.add_argument("-f", "--foobar")
specific_opts.add_argument("-T", "--other-test")
specific_args = specific_opts.parse_args()

If I execute argparse_parents.py -h I get:

usage: argparse_parents.py [-h] [-F FOO] [-t TEST] [-c RFC] [-f FOOBAR]
                           [-T OTHER_TEST]

options:
  -h, --help            show this help message and exit
  -F, --foo FOO
  -t, --test TEST
  -c, --rfc RFC
  -f, --foobar FOOBAR
  -T, --other-test OTHER_TEST

Where I would like it to be:

usage: argparse-parents.py [-h] [-c RFC] [-f FOOBAR] [-F FOO] [-t TEST]
                           [-T OTHER_TEST]

options:
  -h, --help            show this help message and exit
  -c, --rfc RFC
  -f, --foobar FOOBAR
  -F, --foo FOO
  -t, --test TEST
  -T, --other-test OTHER_TEST

How can I sort alphabetically the output of argparse's -h option by their POSIX options?

To me, this is a bug and a feature request at the same time as this question is two-folds:

  • Sorting upper case opts after lower case opts

  • Best/Recommended way to access the help usage

    If you use parentsyou have no control on the way the option are added and modifying the help output is the only way to have a coherent help output.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

https://discuss.python.org/t/sorting-case-sensitively-and-alphabetically-argparses-help-output-by-posix-style-options/106448

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions