Creating a Binary Project $ cargo new --bin catsay $ cargo run 1. Reading Command-Line Arguments with std::env::args Listing 2-2. $ cargo run -- "Hello I'm a cat" Hello I'm a cat \ \ /\_/\ ( o o ) =( I )= 2. Handling Complex Arguments with StructOpt [dependencies] structopt = "0.2.15" Listing 2-3. Reading a Single Positional Argument Using StructOpt extern crate structopt; use structopt::StructOpt; #[derive(StructOpt)] $ cargo run -- --help Listing 2-4. Configuring the Default Value and Description struct Options{ #[structopt(default_value = "Meow!")] /// What does the cat say? message: String, } $ cargo run -- --help ARGS: What does the cat say? [default: Meow!] 3. Adding Binary Flags (aka Switches) Listing 2-5. Adding a Binary Flag Called --dead #[derive(StructOpt)] struct Options { message: String, #[structopt(short = "d", long = "dead")] /// Make the cat appear dead dead: bool, } $ cargo run -- --help Listing 2-6. Showing Different Eyes Based on the Options::dead Flag 4. Printing to STDERR Listing 2-7. Printing to STDERR When an Error Happens $ cargo run "woof" 1> stdout.txt 2> stderr.txt $ cat stdout.txt $ cat stderr.txt 5. Printing with Color [dependencies] // ... colored = "1.7.0" Listing 2-9. Using the colored Crate 6. Reading the Cat Picture from a File Listing 2-10. Adding the catfile Option struct Options { // ... #[structopt(short = "f", long = "file", parse(from_os_str))] /// Load the cat picture from the specified file catfile: Option, } $ cargo run -- --help Listing 2-11. Reading the Catfile Listing 2-12. A Catfile 7. Better Error Handling Listing 2-13. Pseudo-Code for an Expended Version of the ? Operator Listing 2-14. Changing the Function Signature to Use the ? Operator Listing 2-15. Cargo.toml for Using the failure Crate Listing 2-16. Defining a Context on the Result Returned by read_to_string Listing 2-17. Cargo.toml for Using the exitfailure Crate Listing 2-18. Using exitfailure $ cargo run -- -f no/such/file.txt 8. Piping to Other Commands - Piping to STDOUT Without Color Listing 2-19. Raw Color Escaped Code cargo run > output.txt NO_COLOR=1 cargo run > output.txt - Accepting STDIN Listing 2-20. Reading from STDIN $ echo 'Grace' | target/debug/catsay-ag --stdin $ echo 'Grace' | NO_COLOR=1 target/debug/catsay-ag --stdin 9. Integration Testing Listing 2-21. A Basic Smoke Test Listing 2-22. Cargo.toml for Using assert_cmd Listing 2-23. Example Output of a Cargo Test Run $ cargo test Listing 2-24. Cargo.toml for Using Predicates Listing 2-25. Check that the STDOUT Contains a Certain String Listing 2-26. Check that a Bad Argument Triggers a Failure