📝 Python

argparse: Command-Line Arguments in Python

0
Author
04e5cc8b-58ac-4bdc-bdee-661bbb
📅
Published
08.05.2026
⏱️
Reading time
2 min
👁️
Views
59
🌱
Level
Beginner

When a script grows from a toy into a real tool, input() becomes inconvenient. Professional CLI programs accept arguments at launch — like git commit -m "fix" or python manage.py migrate. Python has the built-in argparse module for this.

The Problem with input()

# Inconvenient: you have to run it and wait for a prompt
python tool.py
> Enter command: issues
> Enter repository: my-repo
# Convenient: everything in one command
python tool.py issues my-repo
python tool.py issues my-repo --state closed

The second form can be scheduled in cron, called from an automation script, or invoked by another program.

Basic Syntax

import argparse

parser = argparse.ArgumentParser(description="Program description")

# Positional argument (required):
parser.add_argument("repo", help="Repository name")

# Optional argument (with --):
parser.add_argument("--state", default="open", help="Issue state")

args = parser.parse_args()
print(args.repo)    # value from the command line
print(args.state)   # "open" or the provided value

Running:

python tool.py my-repo               # args.repo = "my-repo", args.state = "open"
python tool.py my-repo --state all   # args.state = "all"

Argument Types

# String (default):
parser.add_argument("--query")

# Integer:
parser.add_argument("--days", type=int, default=7)

# True/False flag:
parser.add_argument("--execute", action="store_true")
# python tool.py           → args.execute = False
# python tool.py --execute → args.execute = True

# Restricted set of values:
parser.add_argument("--state", choices=["open", "closed", "all"], default="open")

Subcommands (subparsers)

For complex CLIs with multiple commands, use subcommands — just like git:

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command", help="Command")

# "issues" subcommand:
issues_p = subparsers.add_parser("issues", help="List issues")
issues_p.add_argument("repo")
issues_p.add_argument("--state", choices=["open","closed","all"], default="open")

# "create" subcommand:
create_p = subparsers.add_parser("create", help="Create an issue")
create_p.add_argument("repo")
create_p.add_argument("--title", required=True)
create_p.add_argument("--body", default="")

args = parser.parse_args()

if args.command == "issues":
    # args.repo, args.state are available
    ...
elif args.command == "create":
    # args.repo, args.title, args.body are available
    ...
elif args.command is None:
    parser.print_help()

Running:

python tool.py issues my-repo
python tool.py issues my-repo --state closed
python tool.py create my-repo --title "Bug fix"
python tool.py --help          # auto-generated help
python tool.py issues --help   # subcommand help

Automatic Help

argparse generates --help for free:

$ python tool.py --help
usage: tool.py [-h] {issues,create,close,repos,search} ...

GitHub Automation Tool

positional arguments:
  {issues,create,close,repos,search}
    issues    List repository issues
    create    Create an issue
    ...

options:
  -h, --help  show this help message and exit

required=True and nargs

# Required optional argument:
parser.add_argument("--title", required=True)

# Accepts multiple values:
parser.add_argument("--labels", nargs="+")  # one or more
# python tool.py --labels bug enhancement → ["bug", "enhancement"]

Summary

argparse turns a script into a real CLI tool:
- All parameters are passed at launch, not via input()
- Automatic --help
- Type validation (type=int) and allowed value validation (choices=)
- --execute flags to control behavior
- Subcommands for grouping related commands

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

Setting Up Your Environment: Python, pip, and VS …

Before writing code locally, you need to set up three tools: Python, pip, and VS...

📅 04.06.2026 👁️ 16
📝

The datetime Module: Working with Dates and Times

datetime is Python's standard module for working with dates and times. It's part of the...

📅 08.05.2026 👁️ 66
📝

.env Files and Environment Variables: Keeping Sec…

Imagine you wrote a program with an API key hardcoded in the source and pushed...

📅 08.05.2026 👁️ 75

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!