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')