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
// 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 std::env;
use getopts::Options;
use getopts::ParsingStyle;

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

impl<'a> Handler<'a> {
    /// Create a new `Handler`
    pub fn new() -> Handler<'a> {
        Handler {
            description: None,
            subcmd: Vec::new(),
            program: String::with_capacity(30),
        }
    }

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

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

    /// Main logic
    ///
    /// This function retrieve argv, parse-it and run the corresponding
    /// subcommand
    pub fn run(&mut self) {
        let args: Vec<String> = env::args().collect();
        self.run_with_args(&args)
    }

    fn short_usage(&self) -> String {
        // Here we should have a program name
        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();

            // Alignement
            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());
    }

    /// Run the main logic without auto retrieving of argv
    pub fn run_with_args(&mut self, args: &Vec<String>) {
        self.program = args[0].clone();

        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");

        let matches = match opts.parse(&args[1..]) {
            Ok(m) => m,
            Err(f) => {
                self.bad_usage();
                return;
            },
        };

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

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

        // Run the command
        for cmd in self.subcmd.iter() {
            if cmd.name() == command {
                cmd.run(&args);
                return;
            }
        }

        // Check built-in command
        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;
            };
        };
    }
}