basic/basic.rs
1// In normal Rust development environment you can run this example code
2// with commmands like
3// cargo run --example basic -- -abc --foo
4// cargo run --example basic -- -f 123 --file=456 foo -v3 bar
5
6use just_getopt::{OptFlags, OptSpecs, OptValue};
7
8fn main() {
9 // The `OptSpecs::new()` function below creates a new option
10 // specification struct `OptSpecs`. Its `option()` methods configure
11 // three different options for logical meanings "help", "file" and
12 // "verbose". All three options can be written in command-line with
13 // a short variant (like "-h") and a long variant (like "--help").
14 // Some options accept or require a value.
15 //
16 // Flag OptionsEverywhere changes parser's behavior. This flag means
17 // that options and other arguments (non-options) can be mixed in
18 // their order in the command line. Without the flag the option
19 // parsing stops at the first non-option argument and the rest of
20 // the command-line is parsed as non-options.
21 let specs = OptSpecs::new()
22 .option("help", "h", OptValue::None) // Arguments: (id, name, value_type)
23 .option("help", "help", OptValue::None)
24 .option("file", "f", OptValue::RequiredNonEmpty)
25 .option("file", "file", OptValue::RequiredNonEmpty)
26 .option("verbose", "v", OptValue::OptionalNonEmpty)
27 .option("verbose", "verbose", OptValue::OptionalNonEmpty)
28 .flag(OptFlags::OptionsEverywhere);
29
30 // Get arguments iterator from operating system and skip the first item
31 let args = std::env::args().skip(1); // which is this program's file path.
32
33 // Parse program's command-line with the given specification `specs`.
34 let parsed = specs.getopt(args);
35
36 // With this you can see the parsed output which is an `Args`
37 // struct.
38 eprintln!("{:#?}", parsed);
39
40 // Create a condition variable for possible exit on error.
41 let mut error_exit = false;
42
43 // Report user about unknown options.
44 for u in &parsed.unknown {
45 eprintln!("Unknown option: {u}");
46 error_exit = true;
47 }
48
49 // Report user about missing values for options that require them
50 // (i.e. "file").
51 for o in parsed.required_value_missing() {
52 eprintln!("Value is required for option '{}'.", o.name);
53 error_exit = true;
54 }
55
56 // Exit if there were bad command-line arguments.
57 if error_exit {
58 eprintln!("Use '-h' for help.");
59 std::process::exit(1);
60 }
61
62 // Print help and exit because "-h" or "--help" was given. We use
63 // option's identifier string "help" here to find if the correct
64 // option was present in the command line. See the `id` argument of
65 // `option()` methods above.
66 if parsed.option_exists("help") {
67 println!("Print friendly help about program's usage.");
68 std::process::exit(0);
69 }
70
71 // Collect all (required) values for "-f" and "--file". We use
72 // option's identifier (id) string "file" to find the option.
73 for f in parsed.options_value_all("file") {
74 println!("File name: {:?}", f);
75 }
76
77 // Notice if "-v" or "--verbose" was given (even without a value).
78 // Then collect all its (optional) values. We use option's
79 // identifier (id) string "verbose".
80 if parsed.option_exists("verbose") {
81 println!("Option 'verbose' was given.");
82 for v in parsed.options_value_all("verbose") {
83 println!("Verbose level: {:?}", v);
84 }
85 }
86
87 // Collect all other (non-option) arguments.
88 for o in &parsed.other {
89 println!("Other argument: {:?}", o);
90 }
91}