Learn argparse

Build command-line interfaces with arguments, options, help and subcommands

13 lessons · 2 hours · 4 levels · covers argparse Python 3.13

argparse is the standard library module for parsing command-line arguments. You declare the positional arguments, options and subcommands your script accepts, and it converts and validates them, generates --help output and reports usage errors. It's the dependency-free way to turn a script into a proper CLI tool.

Before you start

argparse builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 33 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The argparse tree

argparseYour first parserYour first parserPositional arguments vs optionsPositional argumentsvs optionsTypes and defaultsTypes and defaultsFlags and actionsFlags and actionschoices and nargschoices and nargsWriting good help outputWriting good helpoutputSubcommandsSubcommandsListsListsDefining functionsDefining functionsTesting with pytest and unittestTesting with pytestand unittestClasses and instancesClasses andinstancesDictionariesDictionaries
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Arguments and options

  1. Your first parser · Essentials · 8 min

    Create an ArgumentParser, declare arguments with add_argument, and read them from parse_args().

  2. Positional arguments vs options · Essentials · 8 min

    Distinguish required positional arguments from optional flags like -o and --output.

  3. Types and defaults · Essentials · 8 min

    Convert arguments with type=int, float or pathlib.Path, and give options defaults.

  4. Flags and actions · Essentials · 9 min

    Use actions such as store_true, count, append and BooleanOptionalAction for flags and repeatable options.

Designing the interface

  1. choices and nargs · Practical · 9 min

    Restrict values with choices and accept zero, one or many values per argument with nargs.

  2. Writing good help output · Practical · 8 min

    Shape --help with description, epilog, help strings, metavar and formatter classes.

  3. Subcommands · Practical · 12 min

    Build git-style CLIs with add_subparsers, one parser per command, and dispatch with set_defaults.

Validation and testing

  1. Argument groups and mutual exclusion · Advanced · 9 min

    Organise help with argument groups and forbid conflicting options with mutually exclusive groups.

  2. Custom types and validation · Advanced · 10 min

    Write your own type= functions and raise ArgumentTypeError for clear validation messages.

  3. Structuring and testing CLIs · Advanced · 11 min

    Write main(argv=None), test parsers with explicit lists, and control exit behaviour with exit_on_error and parse_known_args.

Extending argparse

  1. Custom Action classes · Expert · 12 min

    Subclass argparse.Action to run your own logic when an argument is parsed, such as KEY=VALUE options.

  2. Parent parsers, files and layered config · Expert · 12 min

    Share arguments with parents=, read arguments from @files, and layer defaults from config files and environment variables.

  3. How argparse parses edge cases · Expert · 12 min

    Understand prefix matching, negative numbers, the -- separator and how argparse assigns words to arguments.