In previous lessons, most notably Get User-provided Options From the Command-line, we provided a means for the user to specify which image of several to animate. But we need to be able to get several more functions, such as:
fly - whether the object should fly or not. This can be a true/false option, with default of false.
speed - at what speed should the image move across the screen.
coalcar - whether to include the coalcar trailer on the C51 and D51 trains, with a default of true.
accident - whether the object appears to have an accident, with a default of false.
help - thankfully, the Clap parser already and automatically gives this to us.
Let's Fly!
Let's start by adding the option to fly. We'll have to make a change to our "Args" struct, and make relevant changes to the main loop in the "main()" function.
Note that "fly" is of type boolean, rather than String. What this means in practice is that if the option is provided on the command-line, then it's true, and if it's not provided, then it's false. This means that instead of having a command that looks like this:
$ cargo run -- --fly = true
we can have one that looks like this:
$ cargo run -- --fly
It saves the user a bit of typing. Sweet!
When I chose to fly the Twin-Engine ($ cargo run -- --fly --kind=twinengine), I discovered that it leaves behind an unwanted trail of plane pieces. To fix this, let's erase the bottom-most line of the previous frame:
Full-Speed Ahead!
Or quarter-speed. Or a crawl. Whatever.
At this point, you can run the program with a command like one of the following:
cargo run -- --speed zippy --fly cargo run -- -f cargo run -- -s f -f cargo run -- --kind=plane --speed fast -fly cargo run -- --kind=plane -s f -f cargo run -- -sz -f -ktwin cargo run -- --fly --speed ultrafast cargo run -- --help
By the way, if for some reason you wanted to use different letters/words for the options, say, "t" and "takeoff" instead of "f" and "fly", you could write the "arg" line like this: #[arg(short = "t", long = "takeoff")].
You can also change the value of the option in the Help screen like this: #[arg(short, long, default_value_t = String::from("N"), value_name="Velocity, Baby, yeah!")], which would generate this:
And when entering your program arguments, the following are all identical:
$ cargo run -- -s f
$ cargo run -- -sf
$ cargo run -- -s=f
$ cargo run -- -s fast
$ cargo run -- -sfast
$ cargo run -- -s=fast
$ cargo run -- --speed=fast
$ cargo run -- --speed fast
(But "--speedf" and "--speedfast" won't work.)
And just as a reminder, cargo run -- -s f is equivalent to ./target/debug/sl -s f, because when you do a "cargo run", cargo is compiling your source code into a binary executable named "sl" which it places into "./target/debug", and then running that. You can run either command to get the same result.
Be aware that if you don't provide a default for an option (using the default_value_t bit), the option is required to be entered on the command-line.
Affixing the Coalcar
Accident?
We can add "accident" (which I'll call "oops") in a similar fashion. In "src/main.rs":
Also in "src/main.rs", we will need to feed this option to the "draw_d51()" function, so it can draw the accident features.
And in "src/parsing.rs":
And in "src/d51.rs":
When you compile this, the compiler will complain about all the unused variables, but the program should compile and run as it did before.