argparse: identify which subparser was used [duplicate]

A simpler solution is to add dest to the add_subparsers call. This is buried a bit further down in the documentation:

[…] If it is necessary to check the name of the subparser that was invoked, the dest keyword argument to the add_subparsers() call will work

In your example replace:

subparsers = parser.add_subparsers(help='commands')

with:

subparsers = parser.add_subparsers(help='commands', dest="command")

Now if you run:

print parser.parse_args(["all"])

you will get

Namespace(command='all')

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)