Skip to main content

basics/
basics.rs

1fn main() -> noargs::Result<()> {
2    let mut args = noargs::raw_args();
3    args.metadata_mut().app_name = env!("CARGO_PKG_NAME");
4    args.metadata_mut().app_description = env!("CARGO_PKG_DESCRIPTION");
5
6    if noargs::VERSION_FLAG.take(&mut args).is_present() {
7        println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
8        return Ok(());
9    }
10    noargs::HELP_FLAG.take_help(&mut args);
11
12    // Important: call flag()/opt() before arg().
13    // Otherwise values like "-v" — or any unknown "--bogus" — can be silently
14    // consumed as positional arguments.
15    //
16    // Required option / positional: set example("...") so help mode renders
17    // a meaningful Usage / Example line. Optional fields covered by default()
18    // or present_and_then() do not need example().
19    let verbose = noargs::flag("verbose")
20        .short('v')
21        .doc("Enable verbose output")
22        .take(&mut args)
23        .is_present();
24    let dry_run = noargs::flag("dry-run")
25        .doc("Print parsed values and exit without running processing")
26        .take(&mut args)
27        .is_present();
28    let retries: usize = noargs::opt("retries")
29        .short('r')
30        .ty("N")
31        .doc("How many times to retry")
32        .default("1")
33        .take(&mut args)
34        .then(|o| o.value().parse())?;
35    let endpoint: String = noargs::opt("endpoint")
36        .ty("URL")
37        .doc("Server endpoint")
38        .env("NOARGS_ENDPOINT")
39        .default("http://localhost:8080")
40        .take(&mut args)
41        .then(|o| o.value().parse())?;
42    let format: String = noargs::opt("format")
43        .ty("FORMAT")
44        .doc("Output format")
45        .example("json")
46        .take(&mut args)
47        .then(|o| o.value().parse())?;
48    let timeout_secs: Option<u64> = noargs::opt("timeout")
49        .ty("SECONDS")
50        .doc("Optional timeout in seconds")
51        .take(&mut args)
52        .present_and_then(|o| o.value().parse())?;
53
54    let input: String = noargs::arg("<INPUT>")
55        .doc("Input file path")
56        .example("input.txt")
57        .take(&mut args)
58        .then(|a| a.value().parse())?;
59    // Optional positional with default(): the value is always a String —
60    // absent uses "out.txt", present uses the given path.
61    // For "absent vs present" distinction, use present_and_then() and bind
62    // to Option<String> — see the README's [BAZ] example.
63    let output: String = noargs::arg("[OUTPUT]")
64        .doc("Output file path")
65        .default("out.txt")
66        .take(&mut args)
67        .then(|a| a.value().parse())?;
68
69    if let Some(help) = args.finish()? {
70        // When help is requested, finish() returns the built help text.
71        // Print it here and exit without running application logic.
72        print!("{help}");
73        return Ok(());
74    }
75
76    if dry_run {
77        println!(
78            "dry-run: verbose={verbose}, retries={retries}, endpoint={endpoint}, format={format}, timeout_secs={timeout_secs:?}, input={input}, output={output}"
79        );
80        return Ok(());
81    }
82
83    println!("processing: {input} -> {output}");
84    println!("using endpoint={endpoint}, format={format}, retries={retries}");
85    if verbose {
86        println!("verbose: timeout_secs={timeout_secs:?}");
87    }
88
89    Ok(())
90}