1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use Command;
use std::env;
use getopts::Options;
use getopts::ParsingStyle;
pub struct Handler<'a> {
description: Option<&'a str>,
subcmd: Vec<Box<Command>>,
program: String,
}
impl<'a> Handler<'a> {
pub fn new() -> Handler<'a> {
Handler {
description: None,
subcmd: Vec::new(),
program: String::with_capacity(30),
}
}
pub fn set_description(&mut self, descr: &'a str) {
self.description = Some(descr);
}
pub fn add(&mut self, command: Box<Command>) {
self.subcmd.push(command);
}
pub fn run(&mut self) {
let args: Vec<String> = env::args().collect();
self.run_with_args(&args)
}
fn short_usage(&self) -> String {
debug_assert!(self.program.len() > 0);
let mut usage = String::with_capacity(150);
usage.push_str("Usage:\n");
usage.push_str(&format!("\t{} <command> [<args>...]\n", self.program));
usage.push_str(&format!("\t{} [options]", self.program));
usage
}
fn print_usage(&self, opts: &Options) {
let mut brief = String::with_capacity(250);
match self.description {
Some(descr) => brief.push_str(&format!("{}\n\n", descr)),
None => {},
}
brief.push_str(&self.short_usage());
println!("{}", opts.usage(&brief));
println!("Commands are:");
let cmd_name_max_len = {
let mut max_len = 0;
for cmd in self.subcmd.iter() {
if cmd.name().len() > max_len {
max_len = cmd.name().len();
}
}
max_len
};
for cmd in self.subcmd.iter() {
let mut name = cmd.name().to_string();
while name.len() < (cmd_name_max_len + 6) {
name.push(' ');
}
println!(" {}{}",name, cmd.description());
}
print!("\nSee '{} help <command>' for more information ", self.program);
println!("on a specific command.");
}
fn bad_usage(&self) {
println!("Invalid arguments.\n");
println!("{}", self.short_usage());
}
pub fn run_with_args(&mut self, args: &Vec<String>) {
self.program = args[0].clone();
let mut opts = Options::new();
opts.parsing_style(ParsingStyle::StopAtFirstFree);
opts.optflag("h", "help", "print this help menu");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
self.bad_usage();
return;
},
};
if matches.opt_present("h") {
if matches.free.len() != 0 {
self.bad_usage();
return;
}
self.print_usage(&opts);
return;
}
let command = if !matches.free.is_empty() {
matches.free[0].clone()
} else {
self.bad_usage();
return;
};
for cmd in self.subcmd.iter() {
if cmd.name() == command {
cmd.run(&args);
return;
}
}
if (command == "help") && (matches.free.len() == 2) {
self.help_for_command(&matches.free[1]);
return;
}
self.bad_usage();
}
fn help_for_command(&self, name: &str) {
for cmd in self.subcmd.iter() {
if cmd.name() == name {
println!("{}", cmd.help());
return;
};
};
}
}