my code lab
lab doc style cli.md
6.6 kB · 228 lines
Markdown

# CLI Design Style Guide ## Architecture The CLI is a presentation layer. Separate it from program logic. Three phases: 1. **Gather input** - parse flags, arguments, stdin (frontend) 2. **Process** - business logic, computation (backend) 3. **Display results** - format output, report errors (frontend) Backend code should never write directly to stdout or stderr. Use callbacks or return values to communicate status. ## Interface patterns ### Single-command tools Syntax: `tool [options] [arguments]` Use for tools that do one thing. Flags fine-tune behavior - they should never be required. If a value is mandatory, make it a positional argument. ### Subcommand tools Syntax: `tool [general-options] command [command-options] [args]` General options apply to every command. Command-specific options come after the subcommand. Do not place command-specific flags before the subcommand name. When multiple subcommands need a similar flag, duplicate it per command rather than promoting it to a general option that some commands ignore. ## FreeBSD vs GNU style Tools in this repository are FreeBSD-native unless stated otherwise. - Single-letter flags only, parsed with `getopts`. Long options (`--force`, `--help`) are a GNU/Linux convention: `getopts` cannot parse them and FreeBSD base utilities do not accept them. - Needing a long option is a sign the interface is drifting from BSD style. Prefer another letter or a subcommand. Special-casing an argument before `getopts` to fake a long option is the telltale smell. - No `--help`, and no help command in single-command tools. The man page is the reference; usage errors print the usage synopsis (see Help below). ## Flags Flags express optionality. Never require them. Valid uses: - Debug/verbose modes - Output formatting - Configuration file paths - Optional parameters with sensible defaults Invalid uses: - Required data (use positional arguments) - Action selection (use subcommands) ## Option parsing Use the platform's standard parser. Do not write …