1use qargparser as arg;
2
3#[derive(Default, Debug)]
4struct MyContext {
5 do_help: bool,
6 exec: String,
7 eargs: Vec<String>
8}
9
10fn help_proc(
11 _spec: &arg::Spec<MyContext>,
12 ctx: &mut MyContext,
13 _args: &Vec<String>
14) {
15 ctx.do_help = true;
16}
17
18fn exec_proc(
19 _spec: &arg::Spec<MyContext>,
20 ctx: &mut MyContext,
21 args: &Vec<String>
22) {
23 ctx.exec = args[0].clone();
24}
25
26fn eargs_proc(
27 _spec: &arg::Spec<MyContext>,
28 ctx: &mut MyContext,
29 args: &Vec<String>
30) {
31 for arg in args {
32 ctx.eargs.push(arg.clone());
33 }
34}
35
36
37fn main() -> Result<(), Box<dyn std::error::Error>> {
38 let help_spec = arg::Builder::new()
39 .sopt('h')
40 .lopt("help")
41 .exit(true)
42 .help(&["Show this help."])
43 .build(help_proc);
44 let exec_spec = arg::Builder::new()
45 .name("exec")
46 .nargs(arg::Nargs::Count(1), &["PRG"])
47 .help(&["The executable to run."])
48 .build(exec_proc);
49 let eargs_spec = arg::Builder::new()
50 .name("execargs")
51 .nargs(arg::Nargs::Remainder, &["PRGARG"])
52 .help(&["arguments to pass to the executable."])
53 .build(eargs_proc);
54
55 let ctx = MyContext {
56 ..Default::default()
57 };
58 let mut prsr = arg::Parser::from_env(ctx);
59
60 prsr.add(help_spec)?;
61 prsr.add(exec_spec)?;
62 prsr.add(eargs_spec)?;
63
64 prsr.parse()?;
65
66 if prsr.get_ctx().do_help == true {
67 prsr.usage(&mut std::io::stdout());
68 std::process::exit(0);
69 }
70
71 let ctx = prsr.into_ctx();
72
73 println!("{:?}", &ctx);
74
75 Ok(())
76}
77
78