I'm using Python's argparse module to create a CLI for my application. I've made a subparsers
variable to store the parsers for each command, but when I can't find a way to change the title of the subparsers
without modifying parser
's (the main ArgumentParser
's) internal variables.
Original Code
parser = ArgumentParser(prog="pacstall", formatter_class=CustomHelpFormatter)
subparsers = parser.add_subparsers(dest="command")
parser._subparsers.title = "commands" # type: ignore[union-attr]
parser._optionals.title = "options"
Result
Edited Code
parser = ArgumentParser(prog="pacstall", formatter_class=CustomHelpFormatter)
subparsers = parser.add_subparsers(title="commands", dest="command")
parser._optionals.title = "options"
Result
As you can see the order of the options
and commands
are switched if I make that change. Also I have no idea how to modify the title
of the _optionals
to "options"
without modifying parser._optionals.title
.
Here is my full parser file.