Building a Digital Garden CLIwith StructOpt
A small CLI tool is a great first Rust project. We cover accepting CLI arguments, handling errors, writing tests, and dealing with the file system. This is an introductory workshop that dives into Rust language concepts to explain them in real-world context as they come up.
Watch 1 hour, 12 minutes of guided lessons.
Extra Workshop Details
This workshop uses the StructOpt crate, which was merged into Clap as of v3. The APIs used in this workshop were kept the same as of the merge, with some minor naming changes being the biggest difference between StructOpt and Clap.
Future CLI workshops will use Clap.
Planning CLI structure by writing a README
Initialize a new Cargo package with a binary crate
Changing the name of the default Cargo binary
Introducing the todo! macro
Introducing color_eyre for reporting on panics and errors
Setting up a subcommand with StructOpt
Documenting a StructOpt CLI
Integration testing Cargo binaries with assert_cmd
Matching on StructOpt subcommands and use of value moved value
Introducing a library crate to support the binary crate
Implementing global flags for all subcommands in StructOpt
Passing control to the user's favorite editor and getting values back
Using the Option type to pick from multiple potential values
Asking the user for input with rprompt and loop
Using loop to rename files to prevent conflicts
Using extension traits to colorize terminal output with owo_colors
Integration testing binaries that spawn subcommands with assert_fs, predicates, and assert_cmd
Abstracting utility functions to make writing and maintaining integration tests easier