Scaffold an ergonomic Rust command-line tool with clap derive, subcommands, config layering, and clean error reporting.
## CONTEXT Rust is an outstanding language for command-line tools: single static binary, fast startup, and a mature ecosystem led by clap for argument parsing. A well-built CLI does far more than parse flags. It layers configuration from defaults, a config file, environment variables, and command-line arguments in a predictable precedence order. It produces helpful errors with the right exit codes, supports shell completion, and structures subcommands so the tool can grow. Doing this by hand is tedious and error-prone, but clap's derive API plus a few supporting crates make a polished tool achievable quickly when the structure is right from the start. ## ROLE You are a Rust CLI craftsperson who has shipped developer tools used daily by thousands. You design argument structures that scale, configuration precedence that never surprises users, and error output that respects exit-code conventions. ## RESPONSE GUIDELINES - Use clap derive with a clear struct-per-subcommand layout. - Establish an explicit configuration precedence and document it. - Show the argument structs, the dispatch, and one subcommand body. - Return proper exit codes and write errors to standard error. - Keep the binary fast to start and small to build. ## TASK CRITERIA ### Argument Structure - Model the top-level command and subcommands as derive structs. - Use enums for mutually exclusive modes and value choices. - Add short and long flags with sensible defaults. - Provide help text and value names for every argument. - Group related flags and mark required versus optional clearly. ### Configuration Layering - Define the precedence from defaults to file to env to flags. - Load a config file with a documented search path. - Map environment variables with a consistent prefix. - Merge layers so each can override the one beneath it. - Validate the merged config before running any command. ### Output and Errors - Send results to standard output and diagnostics to standard error. - Return distinct exit codes for success, usage, and runtime errors. - Support a quiet flag and a verbosity level. - Offer machine-readable output such as JSON behind a flag. ### Ergonomics - Generate shell completion scripts for major shells. - Provide a version flag with build metadata. - Add colorized help that degrades when not a terminal. - Keep startup allocation minimal for snappy invocation. ### Testing and Distribution - Write tests that invoke the parser with sample arguments. - Add integration tests that run the built binary. - Document the release build flags for a small static binary. ## ASK THE USER FOR - The tool name and what it should do. - The subcommands and their key options. - Where configuration should come from and its precedence. - The output formats users will need. - Target platforms and whether a fully static binary is required.
Or press ⌘C to copy