Command Line Interface
Argument Parsing
Scripting
Programming Tips
Code Processing

How can I read and process (parse) command line arguments?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Reading command-line arguments has two layers. First, your program receives raw tokens from the shell. Second, you decide what those tokens mean: flags, option values, positional inputs, defaults, and validation rules. The raw access is simple. The parsing logic is where most CLI bugs come from.

Start with the Raw Argument Vector

Every CLI program gets an ordered list of strings. In Python, that list is sys.argv.

python
import sys

print(sys.argv)

If you run:

bash
python app.py --count 3 input.txt

then sys.argv will contain something like:

python
["app.py", "--count", "3", "input.txt"]

That raw list is enough for tiny scripts, but manually indexing into it becomes brittle fast.

Use a Parser for Anything Non-Trivial

Once you have optional flags, repeated values, help text, or input validation, use a real parser. In Python, the standard choice is argparse.

python
1import argparse
2
3
4def build_parser() -> argparse.ArgumentParser:
5    parser = argparse.ArgumentParser(description="Resize files in a directory")
6    parser.add_argument("path", help="directory to scan")
7    parser.add_argument("--count", type=int, default=10, help="max files to process")
8    parser.add_argument("--verbose", action="store_true", help="print detailed progress")
9    return parser
10
11
12args = build_parser().parse_args()
13print(args.path, args.count, args.verbose)

This gives you several useful features immediately:

  • typed conversion
  • built-in --help
  • default values
  • automatic error messages for missing or malformed arguments

That is why parser libraries are usually a better long-term choice than manual string inspection.

Separate Positionals from Options

A strong CLI design distinguishes clearly between:

  • positional arguments, such as an input file or command name
  • named options, such as --count 10
  • boolean flags, such as --verbose

For example:

python
1import argparse
2
3parser = argparse.ArgumentParser(description="Copy a file")
4parser.add_argument("source")
5parser.add_argument("destination")
6parser.add_argument("--overwrite", action="store_true")
7
8args = parser.parse_args()
9print(args)

That is much easier to reason about than a list of ambiguous unnamed tokens.

Validate Early, Not Deep Inside the Program

Argument parsing is also the right place to reject invalid input. If a value must be an integer greater than zero or a file must exist, validate it as close to the parser as possible.

python
1import argparse
2from pathlib import Path
3
4
5def positive_int(value: str) -> int:
6    parsed = int(value)
7    if parsed <= 0:
8        raise argparse.ArgumentTypeError("value must be positive")
9    return parsed
10
11
12parser = argparse.ArgumentParser()
13parser.add_argument("input_file", type=Path)
14parser.add_argument("--workers", type=positive_int, default=4)
15
16args = parser.parse_args()
17print(args.input_file, args.workers)

This keeps the rest of the program cleaner because bad input is rejected before application logic starts.

Subcommands Make Multi-Action Tools Manageable

If your CLI does more than one thing, use subcommands instead of piling more flags onto one mode.

python
1import argparse
2
3parser = argparse.ArgumentParser()
4subparsers = parser.add_subparsers(dest="command", required=True)
5
6serve = subparsers.add_parser("serve")
7serve.add_argument("--port", type=int, default=8080)
8
9init = subparsers.add_parser("init")
10init.add_argument("project_name")
11
12args = parser.parse_args()
13print(args)

That structure scales much better for tools that need commands like init, serve, build, or deploy.

Think About Shell Behavior Too

Not every parsing bug comes from your code. Shell quoting matters. If a user passes a path with spaces, the shell may split it before your program sees it unless the argument is quoted properly.

That means debugging argument parsing sometimes starts by printing the raw argv and confirming what the shell actually passed.

Common Pitfalls

  • Parsing sys.argv manually long after the CLI has grown beyond a trivial script.
  • Mixing positionals and options in ways that make the interface ambiguous.
  • Deferring validation until business logic, which produces confusing failures.
  • Ignoring shell quoting and assuming the raw command line reaches the program unchanged.
  • Treating argv[0] as a user argument instead of the program name.

Summary

  • Command-line parsing starts with a raw list of strings, usually argv.
  • For real tools, use a parser such as argparse instead of manual indexing.
  • Separate positionals, options, and boolean flags clearly.
  • Validate argument values as early as possible.
  • Use subcommands when the CLI supports multiple distinct actions.

Course illustration
Course illustration

All Rights Reserved.