Python, properly · Chapter 41 of 42
Command-line tools
By the end of this chapter you can turn a script into a real command-line tool: arguments and options with argparse, subcommands, help that writes itself, the same with typer from type hints, and exit codes and stderr that let other programs use your tool. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world Python
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
Your scripts work, but each one has its values typed into the code: to bill a different order, Farah opens the file and edits a line. Bhavna wants to type python3 orders_cli.py show D0042 in the terminal; the nightly job wants to run your checker and stop if it fails.
input() (chapter 5) asks a person, one question at a time, which a nightly job cannot answer. Command-line tools take everything at once, as words after the program's name, and answer in a way both people and programs understand. Every tool you have used in this lab, python3, pytest, uv, psql, works this way.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
bill.py: the bill, from the command line
Farah wants a bill from the terminal. With argparse, python3 bill.py roti:4 chole:1 prints Total: Rs 210. Each item is dish-id:quantity (one or more items), priced from /workspace/data/menu.json. The optional --coupon is DABBA10 (10% off, rounded with round) or FIRST50 (Rs 50 off, never below 0).
Errors go to stderr with nothing on stdout: a dish not on the menu is named and ends with exit code 1. No items, an item that is not dish-id:quantity with a whole quantity of at least 1, or any other coupon is a usage error with exit code 2. --help shows the usage.
- Task 2
orders_cli.py: a command with subcommands
Bhavna wants one tool for September's orders, with argparse subcommands. python3 orders_cli.py count prints how many orders there are; --status and --payment (both optional, together or alone) count only the matching ones. python3 orders_cli.py show D0004 prints that order as D0004 C046 out_for_delivery Rs 350 upi (id, customer, status, total, payment), from ORDERS in the file, already cleaned.
show with an unknown id says so on stderr and exits with code 1. No command at all is a usage error, exit code 2.
- Task 3
dabba.py: a typer app
The same kind of tool with typer. python3 dabba.py riders prints every rider in file order as R2 Imran Shaikh, Bandra (bike); --area keeps the riders in that area (none for an area with no riders). python3 dabba.py menu prints today's available dishes in menu order as Dal Tadka: Rs 140; --max-price N keeps dishes costing N or less, and --veg keeps only the veg category (not jain).
menu --max-price cheap must be refused with exit code 2, and --help must list both commands.
- Task 4 · interview question
Interview: check_orders.py, exit codes and stderr
A nightly job will run python3 check_orders.py FILE before importing an orders export, and stop if it fails. The file has the columns of /workspace/data/orders.csv; the header is line 1, so the first order is line 2. For every row, in file order, it reports: an order_id seen on an earlier row as line 5: D0001 appears again; a total with no digits as line 3: total 'free' is not a number; a status that is not placed, cooking, out_for_delivery, delivered or cancelled (in any case) as line 4: unknown status 'lost'.
With problems, print each on stderr and exit with code 1, printing nothing on stdout. With none, print OK: 2 orders (the number of orders) on stdout. A file that does not exist is a message on stderr and exit code 2. The check also runs it on September's orders.
What you will learn
- sys.argv: the words after the program's name.
- argparse: positional arguments, options, types, choices, flags, subcommands, and the help and usage errors it writes for you.
- typer: the same tool from a function's type hints.
- Exit codes and stderr: how a tool tells another program that it failed.
More in Real-world Python
Build "Command-line tools" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.