How to Add Subcommands to a Python Command-Line Tool with argparse
Tools like git do several jobs, and the first word after the tool's name says which: git commit, git status. Python's argparse builds the same thing with subcommands. Each command gets its own arguments and its own help. This guide builds a tool with two commands on Python 3.15.2.
Syntax
commands = parser.add_subparsers(dest="<attribute>", required=True)
<command_parser> = commands.add_parser("<name>", help="<description>")
| Part | What it means |
|---|---|
parser.add_subparsers() | Tells the main parser that the first argument chooses a command |
dest="<attribute>" | The attribute that holds the chosen command's name, such as args.command |
required=True | Makes choosing a command compulsory |
commands.add_parser("<name>", ...) | Adds one command and returns its own parser, which takes add_argument() like the main one |
help="<description>" | The line shown next to the command in the tool's help |
The argparse documentation explains: "The add_subparsers() method is normally called with no arguments and returns a special action object. This object has a single method, add_parser(), which takes a command name and any ArgumentParser constructor arguments, and returns an ArgumentParser object that can be modified as usual." About dest, it says that "by default None and no value is stored", and required is "by default False", which is why both are set here.
The Example
A tool called tool with two commands: hello, which greets a name, and version, which prints the tool's version.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import argparse
parser = argparse.ArgumentParser(prog="tool", description="A tool with two commands.")
commands = parser.add_subparsers(dest="command", required=True)
hello = commands.add_parser("hello", help="say hello to someone")
hello.add_argument("name", help="who to greet")
commands.add_parser("version", help="show the version")
args = parser.parse_args()
if args.command == "hello":
print(f"Hello, {args.name}!")
else:
print("tool 1.0")

Only hello gets an argument: name belongs to the hello parser, so version doesn't take it. After parse_args(), args.command says which command ran.
Step 2: Run Both Commands
uv run python tool.py hello Ada
Hello, Ada!
uv run python tool.py version
tool 1.0

Step 3: Read the Help
uv run python tool.py --help
usage: tool [-h] {hello,version} ...
A tool with two commands.
positional arguments:
{hello,version}
hello say hello to someone
version show the version
options:
-h, --help show this help message and exit

argparse wrote the help from the help= texts: {hello,version} lists the commands, and each has its line.
Step 4: Run It Without a Command
uv run python tool.py
usage: tool [-h] {hello,version} ...
tool: error: the following arguments are required: command

required=True made argparse stop with an error that names the missing argument, command, the name given in dest. All the commands on this page work unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out dest | The command's name isn't stored, so the code can't tell which command ran | Set dest="command" |
| Putting the main parser's options after the command | With a --verbose option on the main parser, tool hello Ada --verbose stopped with tool: error: unrecognized arguments: --verbose | Put them before the command: tool --verbose hello Ada worked |
FAQ
Does each command have its own help?
Yes. uv run python tool.py hello --help showed only the hello command's arguments:
usage: tool hello [-h] name
positional arguments:
name who to greet
options:
-h, --help show this help message and exit
How do I run different code for each command?
Check args.command, as the if in the script does. For many commands, a dictionary from each command's name to a function keeps the if short.
Conclusion
add_subparsers(dest="command", required=True) gave the tool two commands, each with its own parser: hello Ada greeted Ada, version printed the version, and running it with no command stopped with a clear error.
Additional Resources
- add_subparsers() — subcommands in the argparse documentation.
- argparse tutorial — the official introduction.
