basic/
basic.rs

1extern crate rags_rs as rags;
2use rags::argparse;
3
4static LONG_DESC: &'static str =
5"This example aims to show beginner to intermediate options on the parser
6as well as good practices.
7
8As such, the usefulness of the binary is minimal but it will show you how
9an application should be structured, options passed, errors handled, and
10using parser state to control execution flow (print_help+exit, subcommands, etc).";
11
12#[derive(Debug)]
13pub struct Options {
14    debug: bool,
15    verbosity: usize,
16
17    subcmds: Vec<String>,
18
19    build_release: bool,
20    build_link: Vec<String>,
21    package: String,
22
23    dry_run: bool,
24
25    initial_file: String,
26    additional_files: Vec<String>,
27}
28impl Default for Options {
29    fn default() -> Options {
30        Options {
31            debug: false,
32            verbosity: 0,
33
34            subcmds: vec!(),
35
36            build_release: false,
37            build_link: vec!(),
38            package: "main".to_string(),
39
40            dry_run: false,
41
42            initial_file: "".to_string(),
43            additional_files: vec!(),
44        }
45    }
46}
47
48fn handle_args(parser: &mut rags::Parser, opts: &mut Options) -> Result<(), rags::Error> {
49    parser
50        .app_desc("example using most rags features")
51        .app_long_desc(LONG_DESC)
52        .group("logging", "adjust logging output")?
53            .flag('D', "debug", "enter debug mode", &mut opts.debug, false)?
54            .count('v', "verbose", "increase vebosity (can be given multiple times)",
55                &mut opts.verbosity, 1)?
56            .done()?
57        .subcommand("build", "build a target", &mut opts.subcmds, None)?
58            .arg('p', "package", "rename the package", &mut opts.package, Some("PKG"), true)?
59            .list('l', "lib", "libraries to link", &mut opts.build_link, Some("LIB"), false)?
60            .long_flag("release", "do a release build", &mut opts.build_release, false)?
61            .positional("file", "file to build", &mut opts.initial_file, true)?
62            .positional_list("files", "additional files to build",
63                &mut opts.additional_files, false)?
64            .done()?
65        .subcommand("clean", "clean all build artifacts", &mut opts.subcmds, None)?
66            .flag('p', "print-only", "print what files would be cleaned, but do not clean",
67                &mut opts.dry_run, false)?
68            .done()?
69    ;
70
71    Ok(())
72}
73
74fn main() {
75    let mut opts = Options::default();
76    let mut parser = argparse!();
77    match handle_args(&mut parser, &mut opts) {
78        Ok(_) => {}
79        Err(e) => {
80            println!("");
81            println!("ERROR: {}", e);
82            println!("");
83            parser.print_help();
84            std::process::exit(1);
85        }
86    }
87
88    if parser.wants_help() {
89        parser.print_help();
90    } else {
91        println!("final config: {:?}", opts);
92    }
93}