Instantiate argparse.ArgumentParser(), call add_argument() for each expected argument, then call parse_args() to parse sys.argv and return a Namespace object with the parsed values as attributes.
import argparse
parser = argparse.ArgumentParser(description="Greet a user")
parser.add_argument("name", help="Name of the person to greet")
args = parser.parse_args()
print(f"Hello, {args.name}!")
# python greet.py Sam -> "Hello, Sam!"
Real-world example
Building a simple CLI script that greets a user by name, passed as a positional command-line argument.
Likely follow-ups
What happens automatically if the user runs the script without providing the required 'name' argument?
A positional argument is REQUIRED and identified by its ORDER on the command line (declared without a leading dash); an optional argument is identified by a FLAG name (starting with '-' or '--') and is optional by default unless you explicitly mark it 'required=True'.
parser.add_argument("filename") # positional: required, identified by position
parser.add_argument("--verbose", action="store_true") # optional: identified by --verbose flag
# python script.py data.txt --verbose
Real-world example
Requiring a filename as a positional argument while making a --verbose logging flag optional.
Likely follow-ups
How would you make an optional-looking flag (like --output) actually REQUIRED?
Pass 'default=' to set the value used when the argument is omitted, and 'type=' to specify a callable (like int, float, or a custom function) that argparse applies to convert the raw string input — argparse automatically raises a helpful error if the conversion fails.
parser.add_argument("--count", type=int, default=1, help="Number of times to repeat")
args = parser.parse_args(["--count", "5"])
print(args.count) # 5 (an actual int, not the string '5')
Real-world example
Accepting a numeric --count or --port argument that's automatically validated and converted to an int, with a sensible default.
Likely follow-ups
What error does argparse raise (and how does it exit) if the user passes a non-numeric value for a type=int argument?
Pass 'choices=[...]' to restrict the argument to a specific set of allowed values — argparse automatically validates the input against this list and prints a helpful error message listing the valid choices if the user provides something else.
Set 'nargs' to control how many values an argument consumes: '+' requires ONE OR MORE, '*' allows ZERO OR MORE, and a specific integer requires EXACTLY that many — the parsed result becomes a list instead of a single value.
parser.add_argument("--files", nargs="+", help="One or more input files")
args = parser.parse_args(["--files", "a.txt", "b.txt", "c.txt"])
print(args.files) # ['a.txt', 'b.txt', 'c.txt']
Real-world example
Accepting a list of input files to process in a batch CLI tool, like 'mytool --files a.txt b.txt c.txt'.
Likely follow-ups
What's the difference in behavior between nargs='+' and nargs='*' when NO values are provided?
'action="store_true"' makes a flag default to False and become True if PRESENT on the command line (no value needed after it); 'action="store_false"' is the inverse, defaulting to True and becoming False if present — both avoid needing the user to type an explicit 'True'/'False' string.
Call add_subparsers() on the main parser to create a subparser group, then add each subcommand as its own separate ArgumentParser (via add_parser()) with its own specific arguments — each subcommand's parsed arguments merge into the same top-level Namespace, distinguished by a 'dest' attribute indicating which subcommand was invoked.
add_argument_group() creates a labeled section within the parser's help output, letting you organize related arguments (like all authentication-related flags) together for readability, WITHOUT changing how the arguments are actually parsed — it's purely a --help display organization feature.
parser = argparse.ArgumentParser()
auth_group = parser.add_argument_group("authentication")
auth_group.add_argument("--username")
auth_group.add_argument("--password")
output_group = parser.add_argument_group("output options")
output_group.add_argument("--format", choices=["json", "csv"])
# --help now shows these grouped under their respective section headers
Real-world example
Organizing a CLI tool's --help output into clear, logical sections (authentication, output, filtering) for a large number of flags.
Likely follow-ups
Does argument grouping have any effect on argument PARSING behavior, or is it purely cosmetic for --help?
add_mutually_exclusive_group() creates a group where argparse automatically raises an error if the user provides MORE THAN ONE of the group's arguments together; pass 'required=True' to the group to also enforce that AT LEAST ONE must be provided.
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--verbose", action="store_true")
group.add_argument("--quiet", action="store_true")
# python script.py --verbose --quiet
# error: argument --quiet: not allowed with argument --verbose
Real-world example
Enforcing that a CLI tool's output mode is EXACTLY one of --verbose, --quiet, or --normal, never a combination.
Likely follow-ups
Can a mutually exclusive group contain MORE than two arguments?
Define a plain function that takes the raw string argument and either returns the converted/validated value or raises argparse.ArgumentTypeError with a descriptive message — argparse catches this specific exception and formats it into the standard, consistent CLI error output automatically.
def positive_int(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError(f"{value} is not a positive integer")
return ivalue
parser.add_argument("--count", type=positive_int)
# python script.py --count -5
# error: argument --count: -5 is not a positive integer
Real-world example
Validating that a numeric CLI argument (like a batch size or retry count) is strictly positive, with a clear, standard-format error message.
Likely follow-ups
What's the difference between raising ArgumentTypeError versus a regular ValueError inside a custom type function?