command-line-arguments
programming
duplicate-question
software-development
faq

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.

python
1import sys
2
3print(sys.argv)
4print("script name:", sys.argv[0])
5print("extra args:", sys.argv[1:])

If you run:

bash
python app.py hello 42

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:

c
1#include <stdio.h>
2
3int main(int argc, char *argv[]) {
4    for (int i = 0; i < argc; i++) {
5        printf("argv[%d] = %s\n", i, argv[i]);
6    }
7    return 0;
8}

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.

python
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument("--port", type=int, default=8080)
5parser.add_argument("--verbose", action="store_true")
6args = parser.parse_args()
7
8print(args.port)
9print(args.verbose)

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.argv for basics and argparse for 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.

Course illustration
Course illustration

All Rights Reserved.