Plan C - Make Coalcar Optional
Click here to return to "Plan C - Add More Drawings".
To make the coalcar optional, we need a command-line option for that. We want it to be a boolean (true/false - yes/no), and to default to showing the coalcar.
get_options.rs
...
struct Args {
/// Move the train '[[z]ip]py', '[f]ast', '[[m]ed]ium', '[s]low' or '[c]rawl'?
#[arg(short, long, default_value_t = String::from("med"))]
// The above tells clap that the next line is an argument, that can be entered in as "s" or "speed".
speed: String,
/// Fly?
#[arg(short, long, default_value_t = false)]
fly: bool,
/// What Kind of object? D51 | C51 | Little | Jack | Boat | Plane | Twin | Motorcycle
#[arg(short, long, default_value_t = String::from("D51"))]
kind: String,
/// Oops?
#[arg(short, long, default_value_t = false)]
oops: bool,
/// Disable eXtra trailing vehicle?
#[arg(short = 'x', long, default_value_t = false)]
notail: bool,
} // end of Args definitions
...
pub fn parse_opts() -> (u64, bool, String, bool, String, String, bool ) {
We don't need to do any processing of the variable within the "parse_opts()" function, other than to return the value to the "main()" function:
get_options.rs
...
_ => {
// If none of them match, then ...
println!("Invalid input; run program with '--help' option for more information. You entered '{}', so the default of 'd51' was used.", switches.kind);
(String::from("D51"), D51.to_string(), COALCAR.to_string())
}
};
return (speed, switches.fly, kind, switches.oops, image_string, tail, switches.notail );
} // end of parse_opts()
...
And then in "main()", we need to recieve that option, and make a decision based on it:
get_options.rs
n main() {
// Get the program options from the command-line, if any.
let (speed, fly, kind, oops, image_string, tail_string, notail ) = parse_opts();
// Convert image_string to a vector of String vectors
let mut image_vecvec = string_to_stringvecvec(&image_string);
// If we have a tail for the main image, such as a coalcar, do likewise to it, and then stitch the two images together.
if tail_string != "".to_string() && !notail {
let tail_vecvec = string_to_stringvecvec(&tail_string);
image_vecvec = stitch_tail_to_body(image_vecvec, tail_vecvec);
}
// Animate the resulting image vector.
draw_image(speed, fly, kind, oops, image_vecvec);
} // end of main()
So, if the "tail_string" is not a blank, empty string, meaning there is no coalcar (or other trailing tailpiece), and if the "notail" switch is not set, then process the tail; otherwise, don't. You can test it with:
$ cargo run -- --kind=d51 --notail
or something like
$ cargo run -- -kc -sf -x
Now we can chug along and Add the Accident Mode .