For a while, I am shy to share my "work". So my signal to noise in this group is not so high. Then I read replies saying things positive "Don't worry about sharing advice as a beginner." This motivates me to be more open. So please look at my code. Give it review. Be honest please.
This is part of wrapper I have so I don't need to write main() every time I write a program. My programs get command line power and more but just look like.
fn program(options: &Opt) -> Result<(), ()> {}
Anyway, here is part of my code. Please review.
pub fn args<T: clap::Parser>() -> Result<T, return_code::ReturnCode> {
match std::panic::catch_unwind(T::try_parse) {
Ok(result) => match result {
Ok(result) => Ok(result),
Err(e) => {
eprintln!("{}", e);
Err(return_code::PARSE_ARG_ERROR)
}
},
Err(e) => {
eprintln!("Panic reading command line arguments{:?}", e);
Err(return_code::PARSE_ARG_PANIC)
}
}
}
#[allow(unused_variables)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn args_gives_struct() {
#[derive(clap::Parser, Debug)]
struct Opt {
#[clap(flatten)]
#[cfg(feature = "nomain_control")]
nomain_controls: control::ControlOptions,
}
if let Ok(options) = args::<Opt>() {
assert!(true);
} else {
assert!(false);
}
}
#[test]
fn args_gives_error() {
#[derive(clap::Parser, Debug)]
struct Opt {
#[clap(long)]
word: String,
#[clap(flatten)]
#[cfg(feature = "nomain_control")]
nomain_controls: control::ControlOptions,
}
if let Err(err) = args::<Opt>() {
assert_eq!(err, return_code::PARSE_ARG_ERROR);
} else {
assert!(false);
}
}
#[test]
fn args_gives_panic() {
#[derive(clap::Parser, Debug)]
struct Opt {
first: Vec<String>,
second: Vec<String>,
#[clap(flatten)]
#[cfg(feature = "nomain_control")]
nomain_controls: control::ControlOptions,
}
if let Err(err) = args::<Opt>() {
assert_eq!(err, return_code::PARSE_ARG_PANIC);
} else {
assert!(false);
}
}
}