From ef55dd7400cd5d64024af79561df21cf23d03129 Mon Sep 17 00:00:00 2001 From: Kate Hudson <145493147+k88hudson-cfa@users.noreply.github.com> Date: Wed, 18 Dec 2024 17:06:13 -0500 Subject: [PATCH] Updated runner example (#129) --- examples/runner/main.rs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/runner/main.rs b/examples/runner/main.rs index 813a9c3..35f39bd 100644 --- a/examples/runner/main.rs +++ b/examples/runner/main.rs @@ -4,11 +4,25 @@ use ixa::ContextPeopleExt; #[derive(Args, Debug)] struct CustomArgs { + // Example of a boolean argument. + // --say-hello custom_args.say_hello is true + // (nothing) custom_args.say_hello is false + #[arg(long)] + say_hello: bool, + + // Example of an optional argument with a required value + // -p 12 custom_args.starting_population is Some(12) + // -p This is invalid; you have to pass a value. + // (nothing) custom_args.starting_population is None #[arg(short = 'p', long)] starting_population: Option, } fn main() { + // The runner reads arguments from the command line. + // args refer to `BaseArgs` (see runner.rs for all available args) + // custom_args are optional for any arguments you want to define yourself. + // // Try running the following: // cargo run --example runner -- --seed 42 // cargo run --example runner -- --starting-population 5 @@ -16,12 +30,18 @@ fn main() { let context = run_with_custom_args(|context, args, custom_args: Option| { println!("Setting random seed to {}", args.random_seed); - // If an initial population was provided, add each person - if let Some(custom_args) = custom_args { - if let Some(population) = custom_args.starting_population { - for _ in 0..population { - context.add_person(()).unwrap(); - } + // As long as you specified a custom type in the signature (CustomArgs), + // you should assume custom_args is Some (even if no args were passed + // through the command line). It's None if you didn't specify any custom type. + let custom_args = custom_args.unwrap(); + + if custom_args.say_hello { + println!("Hello"); + } + + if let Some(population) = custom_args.starting_population { + for _ in 0..population { + context.add_person(()).unwrap(); } }