Skip to main content

How to Add Subcommands to a Python Command-Line Tool with argparse

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
commands = parser.add_subparsers(dest="<attribute>", required=True)
<command_parser> = commands.add_parser("<name>", help="<description>")
PartWhat 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=TrueMakes 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​

tool.py
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")

The tool.py script in a macOS-style code window: an ArgumentParser named tool, add_subparsers with dest="command" and required=True, a hello command with a name argument, a version command, and an if that greets the name or prints 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​

Terminal
uv run python tool.py hello Ada
Output
Hello, Ada!
Terminal
uv run python tool.py version
Output
tool 1.0

The terminal after running tool.py hello Ada, which printed Hello, Ada!, and tool.py version, which printed tool 1.0.

Step 3: Read the Help​

Terminal
uv run python tool.py --help
Output
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

The terminal after tool.py --help: the usage line with the -h option and the hello and version commands, the description, both commands with their help lines, and the -h option.

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​

Terminal
uv run python tool.py
Output
usage: tool [-h] {hello,version} ...
tool: error: the following arguments are required: command

The terminal after running tool.py with no command: the usage line and 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​

MistakeWhat happensDo this instead
Leaving out destThe command's name isn't stored, so the code can't tell which command ranSet dest="command"
Putting the main parser's options after the commandWith a --verbose option on the main parser, tool hello Ada --verbose stopped with tool: error: unrecognized arguments: --verbosePut 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:

Output
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​