Command-line tools with Python
You write installable command-line tools with subcommands, configuration, coloured output and correct exit codes.
Level 201 · 17 lessons · 18 practice labs
Lessons
Arguments and interface
- The rules of a good command-line tool
- Subcommands and groups with argparse
- Argument types and validation
- Commands and options with click
- Short task: a note-taking tool with subcommands
Input, output and environment
- A tool that works with stdin, stdout and pipes
- Exit codes and stderr
- Tables, colour and progress bars with rich
- Configuration: file, environment variable and flag precedence
- Taking a password safely: getpass and environment variables
- Short task: a tool that filters CSV from a pipe
Distribution
- Entry points and installing with pipx
- Ctrl+C, signals and a clean exit
- Testing a command-line tool
- Shell completion and help text
- Short task: version and install your tool
Capstone
- Capstone: a command-line tool that takes and verifies backups
Practice labs
- Building a click command group with shared options: Write a group that passes a shared --profile option to subcommands through the context object, shows progress with rich and passes the CliRunner tests.
- An argument type that checks a number range: Write a custom argparse type function that accepts 1 to 100 and gives a clear error for values outside the range.
- Adding subcommands to an inventory tool: Define add, remove and list subcommands with argparse, each with its own help text and required arguments.
- Porting an argparse tool to click: Port a small file-renaming tool to click commands and options with the behaviour tests unchanged.
- Blocking conflicting options: Stop --json with --table and --quiet with --verbose being given together by applying rules with argparse groups and validation.
- Reading from a file or a pipe: Complete a tool that reads stdin when no file name is given and writes upper-cased lines to stdout.
- Tying errors to stderr and the right exit code: Fix a tool that prints everything to stdout and always returns 0 so usage errors return 2, runtime errors 1 and messages go to stderr.
- Exiting cleanly when head closes the pipe: Fix a tool that prints a BrokenPipeError trace when piped to head so it ends silently with the right exit code.
- Showing results in a rich table: Show a disk report with a rich table and progress bar, falling back to plain uncoloured text when output goes to a pipe.
- Applying flag, environment and file precedence: Write a resolver where a setting given three ways is won by the flag, then the environment variable, then the file.
- Keeping the password off the command line: Remove the --password option and write a safe flow that takes the password from an environment variable or asks with getpass.
- Stopping secrets leaking into output: Fix a tool that prints the key in clear in its config dump and error messages so secret fields are masked, proven by a test.
- Cleaning temp files on Ctrl+C: Add handling so that a long export interrupted with Ctrl+C deletes its temp file and exits with code 130.
- Testing a CLI tool with capsys: Write tests that call main(argv) directly and check stdout and stderr separately with capsys and the exit code via SystemExit, covering success, bad-argument and missing-file cases with parameters.
- Packaging a tool with an entry point and version: Turn a script into an installable package with pyproject.toml so the command works and --version reads the version from one source.
- Finishing cleanly on SIGTERM: Add a SIGTERM handler to a long-running export tool so it writes the record in hand, closes the file, notes where it stopped and exits with code 143.
- Making a tool runnable with python -m: Add a __main__.py to the package so the tool runs as python -m package_name, with arguments passed straight to the main function.
- Installing a tool into a clean environment: Build a wheel of the tool, install it into an empty virtual environment and verify the command runs, finding the undeclared dependency and adding it to the project file.
About this course
You write installable command-line tools with subcommands, configuration, coloured output and correct exit codes.
What you will learn
- Arguments and interface
- Input, output and environment
- Distribution
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.