Skip to content

Commit

Permalink
Updated runner example (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
k88hudson-cfa authored Dec 18, 2024
1 parent 71284df commit ef55dd7
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions examples/runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,44 @@ 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<u8>,
}

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
// cargo run --example runner -- -p 5 --debugger
let context = run_with_custom_args(|context, args, custom_args: Option<CustomArgs>| {
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();
}
}

Expand Down

0 comments on commit ef55dd7

Please sign in to comment.