Library › Coding AI › Software developers

Write an argparse parser from a CLI spec, and nothing more

Practice: Setting limits and rules. About 5 minutes.

About this exercise

Turn the ledgerctl spec into a build_parser() function using argparse only (no click or typer). Set prog='ledgerctl' and the description exactly 'Summarize ledger exports into a CSV report.'. Parse --since with date.fromisoformat. build_parser must return the parser without calling parse_args or defining main. Imports: only argparse and date. Exactly 1 code block of at most 14 lines, under 20 words of text.

Common mistakes when you ask AI for this

  • Getting a whole new CLI. The AI writes main(), calls parse_args and wires up the report logic too. Try: Say: "Only build_parser(), which returns the parser. No parse_args, no main."
  • Missing the exact strings. Help output is user-facing; the AI writes its own description and skips prog. Try: Give them: "prog='ledgerctl', description exactly 'Summarize ledger exports into a CSV report.'"
  • A string date or old default. The AI leaves --since as a string or picks up v1's summary.csv. Try: Add: "--since type=date.fromisoformat; --out default report.csv."
  • A different library. The AI may suggest click or typer, which aren't installed. Try: Add: "argparse only; imports only argparse and date."

What a strong prompt includes

  • Names the library
  • Gives prog and description
  • Types the date
  • Rules out parse_args
  • Limits imports

Practice this now ↑

Related exercises