Content · Programming · Python

Python argparse.add_argument() Method: Usage and Examples

add_argument() registers positional or optional CLI values; type= converts input while action= controls flag behavior.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

add_argument() registers positional or optional CLI values; type= converts input while action= controls flag behavior.

What It Does and Why It Matters


add_argument() registers positional or optional CLI values; type= converts input while action= controls flag behavior. Required options and positional values have different declaration rules.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

import argparse
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('filename')
argument_parser.add_argument('--retries', type=int, default=2)
parsed_arguments = argument_parser.parse_args(['report.txt', '--retries', '4'])
print(parsed_arguments.filename, parsed_arguments.retries)

How the Code Works


  • The imports make these standard-library tools available: argparse.
  • The name argument_parser receives the value of argparse.ArgumentParser(). Changing this input lets you explore a different case.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.
  • The parser configuration declares which options or positional arguments exist. parse_args() performs conversion and validation before the program reads attributes on the returned Namespace.

Expected output:

report.txt 4

For the demonstrated inputs, Python displays 'report.txt 4'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This is useful for scripts, maintenance commands and automated workflows. Treat inputs as untrusted, keep normal results separate from errors, and choose exit codes consistently. Keep reusable logic outside the command-line parser so it can be tested independently.

Practice Exercise


Add a required filename and an optional repetition count.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Using required=True on a positional argument or expecting --verbose to behave as a boolean without an action.
  • Overlooking the documented condition: required options and positional values have different declaration rules.

Key Takeaways


  • add_argument() registers positional or optional CLI values; type= converts input while action= controls flag behavior.
  • Add a required filename and an optional repetition count.
Share this resourceSend this page to someone who may find it useful.