Interactive Prompts & Progress
Great CLIs talk with the user: asking questions when needed, and showing progress on long tasks. larzprompt and larzprogress make both easy - and testable.
Two touches make a command-line tool feel alive and considerate: asking the user for input when it's needed (a confirmation, a choice, a password), and showing progress on anything that takes more than a moment. larzprompt and larzprogress handle both cleanly - and larzprompt is built to be unit-testable, which interactive code usually isn't.
Ask the user well
Prompt for confirmations, selections, text and passwords - with re-prompting on invalid input - instead of crashing or making the user pass everything as flags.
from larzprompt import confirm, select, text
if confirm('Delete 3 files?'):
choice = select('Backup first?', ['yes', 'no', 'cancel'])
name = text('New name:')
...| Prompt | For |
|---|---|
| confirm | Yes/no confirmations before risky actions |
| select | Pick from a list of choices |
| text / integer | Free input, validated |
| password | Hidden secret input |
Good prompts guide the user and prevent mistakes (like confirming before something destructive), and re-ask on bad input rather than failing.
Show progress, and stay testable
For anything slow, a progress bar or spinner reassures the user it's working. And because larzprompt takes injectable input/output, your interactive flows are actually unit-testable.
from larzprogress import progress
for item in progress(items, label='Processing'):
handle(item) # a live progress bar, pipe-safeMake it interactive
- Prompt when needed - confirm/select/text/password, with re-prompting.
- Confirm risky actions - a yes/no before anything destructive.
- Show progress - bars/spinners for slow work; pipe-safe.
- Stay testable - injectable I/O lets you unit-test interactive flows.
๐ฏ Key takeaways
- Great CLIs ask the user when needed and show progress on long tasks.
- larzprompt gives confirm/select/text/password with re-prompting; confirm risky actions.
- larzprogress shows pipe-safe bars/spinners so slow tasks don't feel frozen.
- larzprompt's injectable I/O makes interactive flows unit-testable - rare and valuable.
Frequently asked
When should a CLI prompt versus take everything as flags?
Balance convenience with automatability. Flags/arguments are essential because they let the tool be scripted and automated (a tool that always stops to ask can't be used in a pipeline or cron job). But interactive prompts are valuable for confirmations before destructive actions, for guiding occasional/human use, and when required input is missing. A good pattern: accept everything as flags for automation, but prompt for missing required values or dangerous confirmations when run interactively - and provide a way to skip prompts (like a --yes flag) for scripts. So: default to flags for scriptability, add prompts thoughtfully for safety and human friendliness, and always leave an escape hatch for automation.
How do you even unit-test interactive prompts?
Normally it's painful, because the code calls out to real stdin/stdout, so a test would hang waiting for input. larzprompt solves this by letting you inject the input and output - in a test you provide a fake input source that returns canned answers and capture the output, so you can assert 'when the user answers yes, the function proceeds; when no, it aborts' with no real interaction. This means your interactive logic (which is often exactly the risky, decision-making part of a tool) can be tested like any other code, rather than being an untested black box. Designing for injectable I/O is what makes that possible, and it's built into larzprompt.
Related lessons
Beautiful Terminal Output
A tool that prints clear, well-formatted output feels professional and is easier to use. lโฆ
Build CLI Tools with LarzPackage & Ship Your CLI
A tool only helps others if they can install it. Packaging your CLI so it installs with piโฆ
Build CLI Tools with LarzTurn a Function Into a CLI
The core move: larzcli turns a plain Python function into a full command-line tool - argumโฆ