-
Hi, first time using Parse in /// main.rs
use clap::Parser;
#[derive(Parser)]
struct Cli {
name: String,
age: u8,
}
fn main() {
let cli = Cli::parse();
foo(cli.name, cli.age);
}
fn foo(name: String, age: u8) {
println!("name: {}", name);
bar(age);
}
fn bar(age: u8) {
println!("age: {}", age);
} Or is it better to parse again in the function where you need the actual argument: /// main.rs
use clap::Parser;
#[derive(Parser)]
struct Cli {
name: String,
age: u8,
}
fn main() {
let cli = Cli::parse();
foo(cli.name);
}
fn foo(name: String) {
println!("name: {}", name);
bar();
}
fn bar() {
let cli = Cli::parse();
println!("age: {}", cli.age);
} Or is there a different way entirely? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The patterns I've seen used that seem to make the most sense
These can be mixed together. For example, cargo-release uses the subcommand run functions (https://github.com/crate-ci/cargo-release/blob/master/src/bin/cargo-release.rs) which will access individual fields (https://github.com/crate-ci/cargo-release/blob/master/src/steps/owner.rs#L42) but also layers parts of the |
Beta Was this translation helpful? Give feedback.
-
Not exactly sure what you're referring to with |
Beta Was this translation helpful? Give feedback.
The patterns I've seen used that seem to make the most sense
Context
that you put the fields into and pass that aroundThese can be mixed together. For example, cargo-release uses the subcommand run functions (https://github.com/crate-ci/cargo-release/blob/master/src/bin/cargo-release.rs) which will access individual fields (https://github.com/crate-ci/cargo-release/blob/master/src/steps/owner.rs#L42) but also layers parts of the
struct
into the config (https://github.com/crate-ci/cargo-release/blob/master/src/ste…