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