Building a command-line tool
Command-line tools are where most people write their first real Rust, and for good reason: no
framework, no async at the start, and the feedback loop is one cargo run.
This is the whole path from env::args to a stripped release binary — including the parts nobody
documents, like detecting a pipe or respecting NO_COLOR.
Articles
Section titled “Articles”- How to read command-line argumentsstd::env::args, and the first element you forgot
- How to pass arguments with cargo runthe – separator and why it exists
- How to parse arguments with clapthe derive API, in one page
- How to add subcommands with clapan enum of commands, and shared options
- How to get a good –help for freedoc comments, defaults, and required arguments
- How to read a line from standard inputthe newline you did not ask for
- How to read several values from one line of inputsplit, map, collect
- How to read standard input until the endthe read loop behind a Unix pipe
- How to exit with a status codeprocess::exit, and what it skips
- How to colour terminal outputANSI codes, the crate, and NO_COLOR
- How to detect whether output is a terminalis_terminal, and why your colours vanish in a pipe
- How to add a progress bar to a CLIindicatif, and stderr versus stdout
- How to let a CLI read a config filedefaults, $HOME, and the file that is not there
- How to test a CLI end to endassert_cmd, and a fake server for the network
- How to build a binary you can hand to someonerelease profile, stripping, and musl
- How to cross-compile for another platformtargets, linkers, and what actually breaks