Skip to content
Advertisement

Get selected subcommand with argparse

When I use subcommands with python argparse, I can get the selected arguments.

JavaScript

So args doesn’t contain 'foo'. Simply writing sys.argv[1] doesn’t work because of the possible global args. How can I get the subcommand itself?

Advertisement

Answer

The very bottom of the Python docs on argparse sub-commands explains how to do this:

JavaScript

You can also use the set_defaults() method referenced just above the example I found.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement