I'm using a subparser/subcommand that has an alias.
I'm using the dest
option for the subparser to store the name of the subcommand so I can get it later.
Currently if the subcommand's name is reallyLongName
and the alias is r
(say) then the dest
option stores either reallyLongName
or r
exactly - whatever I typed in gets stored. This is annoying because I now have to check for the name of the command or any of its aliases in order to identify the command.
Is there a way to get argparse to store the subcommand's name in the dest
field in some sort of single, canonical text string?
For example, given the following code:
import argparseparser = argparse.ArgumentParser()subparsers = parser.add_subparsers(dest='command', help='sub-command help')parser_ag = subparsers.add_parser( 'mySubcommand',aliases=['m'],help='Subcommand help')print(parser.parse_args('mySubcommand'.split()))print(parser.parse_args('m'.split()))
the following output is produced:
Namespace(command='mySubcommand')
Namespace(command='m')
Desired result: command
has a single, canonical value for both, for example:
Namespace(command='mySubcommand')
Namespace(command='mySubcommand')