How do I access 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
Command-line arguments are the values passed to a program when it starts, and they are one of the oldest ways to control software behavior without editing source code. Every mainstream language exposes them, usually through an argv-style API, but the more important skill is understanding how to parse, validate, and use them safely.
The Basic Model: argc and argv
Conceptually, programs receive:
- the executable or script name
- zero or more argument strings after that
In Python, those values are exposed through sys.argv.
If you run:
then sys.argv will contain the script name followed by "hello" and "42".
In C, the same idea appears as argc and argv in main:
Different languages expose the values differently, but the mental model is the same.
Once you understand that model, porting between languages becomes much easier. You stop memorizing APIs and start recognizing the same structure: program name first, then zero or more user-supplied strings that the program must interpret.
Parsing Is More Important Than Accessing
Simply reading raw arguments is only the first step. Real programs usually need to interpret flags, options, and values such as:
- '
--input data.csv' - '
--verbose' - '
--port 8080'
For anything beyond a quick demo, use the language's argument-parsing tools instead of indexing strings manually. In Python, argparse is the standard choice.
This gives you validation, defaults, and help text automatically. That is far better than writing fragile sys.argv[1] logic everywhere.
Handle Missing and Invalid Values
A common beginner mistake is assuming the needed argument is always present. Raw indexing such as sys.argv[1] will raise an error if the program is run without enough arguments.
Even when an argument exists, it is still just a string until you validate or convert it. If a user passes abc where a port number is expected, the program should fail clearly rather than continue with broken assumptions.
Good command-line handling therefore includes:
- checking required arguments
- converting types deliberately
- producing useful error messages
- documenting expected usage
That discipline matters more than the specific language API you happen to call.
It also makes automation safer. Shell scripts, schedulers, and CI jobs often run programs non-interactively, so clear argument parsing is part of operational reliability, not just developer convenience.
That is why mature command-line tools invest so much effort in helpful usage text and predictable option behavior.
Common Pitfalls
- Accessing positional arguments by index without checking length first.
- Treating all arguments as trustworthy strings without validation.
- Reimplementing flag parsing manually when a standard library parser already exists.
- Forgetting that
argv[0]is usually the program or script name. - Writing CLIs that fail with vague errors instead of clear usage messages.
Summary
- Command-line arguments are usually exposed through an
argv-style interface. - Access is easy; robust parsing and validation are the real work.
- In Python, use
sys.argvfor basics andargparsefor real CLIs. - Expect missing, malformed, or unexpected input and handle it explicitly.
- A good command-line interface is part of the program design, not just input plumbing.

