simple/
simple.rs

1use std::collections::HashMap;
2
3use qargparser as arg;
4
5#[derive(Default, Debug)]
6struct MyContext {
7  do_help: bool,
8  do_version: bool,
9  verbosity: u8,
10  fname: String,
11  params: HashMap<String, String>,
12  cmd: String,
13  subcmd: String
14}
15
16fn help_proc(
17  _spec: &arg::Spec<MyContext>,
18  ctx: &mut MyContext,
19  _args: &Vec<String>
20) {
21  ctx.do_help = true;
22}
23
24fn verbose_proc(
25  _spec: &arg::Spec<MyContext>,
26  ctx: &mut MyContext,
27  _args: &Vec<String>
28) {
29  ctx.verbosity += 1;
30}
31
32/*
33fn version_proc(_spec: &arg::Spec<MyContext>, ctx: &mut MyContext,
34    _args: &Vec<String>) {
35  ctx.do_version = true;
36}
37*/
38
39fn file_proc(
40  _spec: &arg::Spec<MyContext>,
41  ctx: &mut MyContext,
42  args: &Vec<String>
43) {
44  ctx.fname = args[0].clone();
45}
46
47fn param_proc(
48  _spec: &arg::Spec<MyContext>,
49  ctx: &mut MyContext,
50  args: &Vec<String>
51) {
52  ctx.params.insert(args[0].clone(), args[1].clone());
53}
54
55fn cmd_proc(
56  _spec: &arg::Spec<MyContext>,
57  ctx: &mut MyContext,
58  args: &Vec<String>
59) {
60  ctx.cmd = args[0].clone();
61}
62
63fn subcmd_proc(
64  _spec: &arg::Spec<MyContext>,
65  ctx: &mut MyContext,
66  args: &Vec<String>
67) {
68  ctx.subcmd = args[0].clone();
69}
70
71
72fn main() -> Result<(), Box<dyn std::error::Error>> {
73  let help_spec = arg::Builder::new()
74    .sopt('h')
75    .lopt("help")
76    .exit(true)
77    .help(&["Show this help."])
78    .build(help_proc);
79  let verbose_spec = arg::Builder::new()
80    .sopt('v')
81    .lopt("verbose")
82    .help(&["Increase level of verbosity.", "Be quiet by default."])
83    .build(verbose_proc);
84  let version_spec = arg::Builder::new()
85    .sopt('V')
86    .lopt("version")
87    .exit(true)
88    .help(&["Output tool version and exit."])
89    .build(
90      |_spec: &arg::Spec<MyContext>,
91       ctx: &mut MyContext,
92       _args: &Vec<String>| ctx.do_version = true
93    );
94  let file_spec = arg::Builder::new()
95    .sopt('f')
96    .lopt("file")
97    .help(&["Use data in FILE."])
98    .nargs(arg::Nargs::Count(1), &["FILE"])
99    .build(file_proc);
100  let param_spec = arg::Builder::new()
101    .sopt('p')
102    .lopt("param")
103    .help(&["Add a key/value parameter field. The key must be unique."])
104    .nargs(arg::Nargs::Count(2), &["KEY", "VALUE"])
105    .build(param_proc);
106  let cmd_spec = arg::Builder::new()
107    .name("command")
108    .required(true)
109    .nargs(arg::Nargs::Count(1), &["COMMAND"])
110    .help(&["The command to run."])
111    .build(cmd_proc);
112  let subcmd_spec = arg::Builder::new()
113    .name("subcmd")
114    .nargs(arg::Nargs::Count(1), &["SUBCMD"])
115    .help(&["Command-specific sub-command."])
116    .build(subcmd_proc);
117
118
119  let ctx = MyContext {
120    ..Default::default()
121  };
122  let mut prsr = arg::Parser::from_env(ctx);
123
124  prsr.add(help_spec)?;
125  prsr.add(verbose_spec)?;
126  prsr.add(version_spec)?;
127  prsr.add(file_spec)?;
128  prsr.add(param_spec)?;
129  prsr.add(cmd_spec)?;
130  prsr.add(subcmd_spec)?;
131
132  prsr.parse()?;
133
134  if prsr.get_ctx().do_help == true {
135    prsr.usage(&mut std::io::stdout());
136    std::process::exit(0);
137  }
138
139  let ctx = prsr.into_ctx();
140
141  println!("{:?}", &ctx);
142
143  Ok(())
144}
145
146/* vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : */