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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright © 2015 - Samuel Dolt <samuel@dolt.ch>
//
// Licensed under the MIT license. This file may not be copied, modified,
// or distributed except according to those terms.
//
// See the COPYRIGHT file at the top-level directory of this distribution.


use Command;
use Message;
use CmdWrapper;
use CmdResult;

use std::env;
use std::io::Write;
use getopts::Options;
use getopts::ParsingStyle;
use tabwriter::TabWriter;
use strsim::damerau_levenshtein;

/// Command line parser and subcommand runner
///
/// # Example
///
/// ```
/// use subcmd::CmdHandler;
/// let mut handler = CmdHandler::new();
///
/// // Add your custom command here
/// // handler.add(Box::new(MyCommand));
/// // handler.add(Box::new(AnotherCommand));
///
/// handler.run(); // Run main logic
/// ```
pub struct CmdHandler<'a> {
    description: Option<&'a str>,
    commands: Vec<Box<Command>>,
    program_name: String,
    args: Vec<String>,
}

impl<'a> CmdHandler<'a> {
    /// Create a new `CmdHandler`
    pub fn new() -> CmdHandler<'a> {
        let args: Vec<String> = env::args().collect();
        let program_name = args[0].clone();

        CmdHandler {
            description: None,
            commands: Vec::new(),
            program_name: program_name,
            args: args,
        }
    }

    /// Set a one line description, used in `bin --help`
    pub fn set_description(&mut self, descr: &'a str) {
        self.description = Some(descr);
    }

    /// Get the program description
    pub fn get_description(&mut self) -> &'a str {
        match self.description {
            Some(descr) => descr,
            None => "",
        }
    }

    /// Override default args
    pub fn override_args(&mut self, args: Vec<String>) {
        self.args = args;
    }

    /// Register a new subcommand
    pub fn add(&mut self, command: Box<Command>) {
        self.commands.push(command);
    }

    fn short_usage(&self) -> String {
        let mut usage = String::with_capacity(150);
        usage.push_str("Usage:\n");
        usage.push_str(&format!("\t{} <command> [<args>...]\n", self.program_name));
        usage.push_str(&format!("\t{} [options]", self.program_name));

        usage
    }

    fn help(&self, opts: &Options) -> CmdResult {
        let mut brief = String::with_capacity(250);
        let mut msg = Message::new();

        match self.description {
            Some(descr) => brief.push_str(&format!("{}\n\n", descr)),
            None => {}
        }

        brief.push_str(&self.short_usage());


        msg.add_line(&opts.usage(&brief));
        msg.add_line("Commands are:");

        let mut tw = TabWriter::new(Vec::new());
        for cmd in self.commands.iter() {
            write!(&mut tw, "    {}\t{}\n", cmd.name(), cmd.description()).unwrap();
        }
        tw.flush().unwrap();

        msg.add_line(&String::from_utf8(tw.unwrap()).unwrap());

        msg.add_line(&format!("\nSee '{} help <command>' for more information ",
                              self.program_name));
        msg.add_line("on a specific command.");

        CmdResult::Help(msg)
    }

    fn bad_usage(&self) -> CmdResult {
        let mut msg = Message::new();
        msg.set_error(true);

        msg.add_line("Invalid arguments.");
        msg.add_line(&self.short_usage());
        CmdResult::BadUsage(msg)
    }


    /// Run the main logic
    pub fn run(mut self) -> CmdResult {
        let mut opts = Options::new();

        // We don't want to parse option after the subcommand
        opts.parsing_style(ParsingStyle::StopAtFirstFree);

        opts.optflag("h", "help", "print this help menu");

        // args[0] is the program name
        let matches = match opts.parse(&self.args[1..]) {
            Ok(m) => m,
            Err(_) => {
                // Raise on a unknow flag like `--unknow`
                return self.bad_usage();
            }
        };

        // Catch a -h/--help request
        if matches.opt_present("h") {
            // -h/--help don't allow other options/args
            if matches.free.len() != 0 {
                return self.bad_usage();
            }
            return self.help(&opts);
        }

        // Catch the command
        let command = if !matches.free.is_empty() {
            matches.free[0].clone()
        } else {
            return self.bad_usage();
        };

        // Try to find the command
        for index in 0..self.commands.len() {
            if self.commands[index].name() == command {
                let wrap = CmdWrapper::new(self.commands.remove(index), self.args);
                return CmdResult::Cmd(wrap);
            }
        }

        // Check built-in command
        if (command == "help") && (matches.free.len() == 2) {
            return self.help_for_command(&matches.free[1]);
        }


        // No command found, check for similariy
        let mut sim_cmd: Option<&Box<Command>> = None;
        // We only want command with a similarity lowest than 3
        let mut lowest_sim: usize = 3;
        for cmd in self.commands.iter() {
            let new_sim = damerau_levenshtein(cmd.name(), &command);
            if new_sim < lowest_sim {
                lowest_sim = new_sim;
                sim_cmd = Some(cmd);
            }
        }

        match sim_cmd {
            Some(cmd) => {
                let mut msg = Message::new();
                msg.set_error(true);
                msg.add_line("No such subcommand\n");
                msg.add_line(&format!("    Did you mean `{}`?", cmd.name()));
                return CmdResult::BadUsage(msg);
            }
            None => {}
        };

        let mut msg = Message::new();
        msg.set_error(true);
        msg.add_line("No such subcommand");

        CmdResult::UnknowCmd(msg)
    }


    fn help_for_command(&mut self, name: &str) -> CmdResult {
        for index in 0..self.commands.len() {
            if self.commands[index].name() == name {
                let wrap = CmdWrapper::new(self.commands.remove(index), self.args.clone());
                return CmdResult::HelpForCmd(wrap);
            };
        }

        self.bad_usage()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use Command;
    use CmdResult;

    struct CmdA;

    impl Command for CmdA {
        fn name<'a>(&self) -> &'a str {
            "cmd-a"
        }
        fn help<'a>(&self) -> &'a str {
            "HELP"
        }
        fn description<'a>(&self) -> &'a str {
            "DESCR"
        }
        fn run(&self, argv: &Vec<String>) {
            // DO NOTHING
        }
    }

    struct AnotherCmd;

    impl Command for AnotherCmd {
        fn name<'a>(&self) -> &'a str {
            "another-cmd"
        }
        fn help<'a>(&self) -> &'a str {
            "HELP another"
        }
        fn description<'a>(&self) -> &'a str {
            "DESCR another"
        }
        fn run(&self, argv: &Vec<String>) {
            // DO NOTHING
        }
    }

    #[test]
    fn test_usage() {
        let mut handler = CmdHandler::new();
        let args: Vec<String> = vec!["bin".to_string(), "-h".to_string()];

        handler.override_args(args);

        match handler.run() {
            CmdResult::Help(msg) => {
                assert!(msg.get().contains("Usage"));
                assert!(msg.get().contains("Commands are:"));
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn test_bad_usage() {
        let mut handler = CmdHandler::new();
        let args: Vec<String> = vec!["bin".to_string(), "--unknow".to_string()];

        handler.override_args(args);

        match handler.run() {
            CmdResult::BadUsage(msg) => {
                assert!(msg.get().contains("Invalid argument"));
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn test_bad_command() {
        let mut handler = CmdHandler::new();
        let args: Vec<String> = vec!["bin".to_string(), "cmd-b".to_string()];

        handler.override_args(args);
        handler.add(Box::new(CmdA));
        handler.add(Box::new(AnotherCmd));

        match handler.run() {
            CmdResult::BadUsage(msg) => {
                assert!(msg.get().contains("cmd-a"));
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn test_unknow_cmd() {
        let mut handler = CmdHandler::new();
        let args: Vec<String> = vec!["bin".to_string(), "bbbbbbbbbbb".to_string()];

        handler.override_args(args);
        handler.add(Box::new(CmdA));
        handler.add(Box::new(CmdA));

        match handler.run() {
            CmdResult::UnknowCmd(msg) => assert!(msg.get().contains("No such subcommand")),
            _ => unreachable!(),
        }
    }

    #[test]
    fn test_cmd() {
        let mut handler = CmdHandler::new();
        let args: Vec<String> = vec!["bin".to_string(), "cmd-a".to_string()];

        handler.override_args(args);
        handler.add(Box::new(CmdA));
        handler.add(Box::new(CmdA));

        match handler.run() {
            CmdResult::Cmd(cmd) => assert_eq!(cmd.name(), "cmd-a"),
            _ => unreachable!(),
        }
    }

    #[test]
    fn test_help_for_cmd() {
        let mut handler = CmdHandler::new();
        let args: Vec<String> = vec!["bin".to_string(), "help".to_string(), "cmd-a".to_string()];

        handler.override_args(args);
        handler.add(Box::new(CmdA));
        handler.add(Box::new(CmdA));

        match handler.run() {
            CmdResult::HelpForCmd(cmd) => assert_eq!(cmd.name(), "cmd-a"),
            _ => unreachable!(),
        }
    }

}