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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Usage information presenter

use super::Command;
use crate::{terminal::stream::STDOUT, Version};
use std::{
    io::{self, Write},
    process,
};
use termcolor::{Color, ColorSpec, WriteColor};

/// Presenter for usage information for a particular `Command`
#[derive(Debug)]
pub struct Usage {
    /// Package name
    pub package_name: String,

    /// Package version
    pub package_version: Version,

    /// Package authors
    pub package_authors: Vec<String>,

    /// Package description
    pub package_description: Option<String>,

    /// Command-line options
    pub args: Vec<Argument>,

    /// Subcommands
    pub subcommands: Vec<Subcommand>,
}

impl Usage {
    /// Build usage information for a particular command
    pub fn for_command<C>() -> Self
    where
        C: Command,
    {
        let package_name = C::name().to_owned();
        let package_version = Version::parse(C::version()).expect("invalid version");
        let package_authors = C::authors().split(':').map(String::from).collect();

        let package_description = C::description()
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ");

        let args = C::usage()
            .split('\n')
            .filter_map(Argument::parse_usage)
            .collect();

        let subcommands = C::command_list()
            .map(|command_list| {
                command_list
                    .split('\n')
                    .map(|usage| Subcommand::parse_usage::<C>(usage))
                    .collect()
            })
            .unwrap_or_else(|| vec![]);

        Self {
            package_name,
            package_version,
            package_authors,
            package_description: if package_description.is_empty() {
                None
            } else {
                Some(package_description)
            },
            args,
            subcommands,
        }
    }

    /// Print usage for a particular subcommand
    pub fn print_subcommand(&self, args: &[String]) -> Result<(), io::Error> {
        let mut stdout = STDOUT.lock();
        stdout.reset()?;

        let mut recognized = vec![];
        let mut command = self;
        let mut description = None;

        for arg in args {
            match command.subcommands.iter().find(|cmd| &cmd.name == arg) {
                Some(subcommand) => {
                    recognized.push(arg.clone());
                    description = Some(&subcommand.description);
                    command = &subcommand.usage;
                }
                None => {
                    command.print_unrecognized_command(arg, &recognized)?;

                    // Force error status exit code
                    return Err(io::ErrorKind::NotFound.into());
                }
            }
        }

        command.print_info()?;

        let mut white = ColorSpec::new();
        white.set_fg(Some(Color::White));
        white.set_bold(true);

        stdout.set_color(&white)?;
        writeln!(stdout, "USAGE:")?;
        stdout.reset()?;

        let mut usage_items = vec![self.package_name.clone()];
        usage_items.extend(recognized.iter().map(|s| s.to_string()));
        let usage_string = usage_items.join(" ");

        if command.subcommands.is_empty() {
            writeln!(stdout, "    {} <OPTIONS>", &usage_string)?;
        } else {
            writeln!(stdout, "    {} <SUBCOMMAND>", &usage_string)?;
        }

        writeln!(stdout)?;

        if let Some(desc) = description {
            stdout.set_color(&white)?;
            writeln!(stdout, "DESCRIPTION:")?;
            stdout.reset()?;

            writeln!(stdout, "    {}", desc)?;
            writeln!(stdout)?;
        }

        command.print_usage()
    }

    /// Print usage for a particular subcommand and exit
    pub fn print_subcommand_and_exit(&self, args: &[String]) -> ! {
        let exit_code = match self.print_subcommand(args) {
            Ok(_) => 0,
            Err(_) => 1,
        };

        process::exit(exit_code);
    }

    /// Print information about a usage error
    pub(super) fn print_error_and_exit(&self, err: gumdrop::Error, args: &[String]) -> ! {
        // TODO(tarcieri): better personalize errors based on args
        if args.is_empty() {
            self.print_info().unwrap();
            self.print_usage().unwrap();
            process::exit(0);
        }

        let mut command = self;
        let mut description = None;

        for arg in args {
            if let Some(sub) = command.subcommands.iter().find(|cmd| &cmd.name == arg) {
                command = &sub.usage;
                description = Some(&sub.description);
            } else {
                break;
            }
        }

        let mut stdout = STDOUT.lock();
        stdout.reset().unwrap();

        let mut red = ColorSpec::new();
        red.set_fg(Some(Color::Red));
        red.set_bold(true);

        stdout.set_color(&red).unwrap();
        write!(stdout, "error: ").unwrap();
        stdout.reset().unwrap();

        writeln!(stdout, "{}", err).unwrap();
        writeln!(stdout).unwrap();

        command.print_info().unwrap();

        if let Some(desc) = description {
            let mut white = ColorSpec::new();
            white.set_fg(Some(Color::White));
            white.set_bold(true);

            stdout.set_color(&white).unwrap();
            writeln!(stdout, "DESCRIPTION:").unwrap();
            stdout.reset().unwrap();

            writeln!(stdout, "    {}", desc).unwrap();
            writeln!(stdout).unwrap();
        }

        command.print_usage().unwrap();
        process::exit(1);
    }

    /// Print information about an unrecognized command
    fn print_unrecognized_command(
        &self,
        unrecognized: &str,
        recognized: &[String],
    ) -> Result<(), io::Error> {
        let mut stdout = STDOUT.lock();
        stdout.reset().unwrap();

        let mut unrecognized_items = recognized.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        unrecognized_items.push(unrecognized.to_owned());

        let unrecognized_string = unrecognized_items.join(" ");

        let mut red = ColorSpec::new();
        red.set_fg(Some(Color::Red));
        red.set_bold(true);

        let mut yellow = ColorSpec::new();
        yellow.set_fg(Some(Color::Yellow));
        yellow.set_bold(true);

        let mut green = ColorSpec::new();
        green.set_fg(Some(Color::Green));
        green.set_bold(true);

        stdout.set_color(&red)?;
        write!(stdout, "error: ")?;
        stdout.reset()?;

        write!(stdout, "The subcommand ")?;

        stdout.set_color(&yellow)?;
        write!(stdout, "{:?} ", &unrecognized_string)?;
        stdout.reset()?;

        writeln!(stdout, "wasn't recognized.")?;
        writeln!(stdout)?;

        let mut white = ColorSpec::new();
        white.set_fg(Some(Color::White));
        white.set_bold(true);

        stdout.set_color(&yellow)?;
        writeln!(stdout, "USAGE:")?;
        stdout.reset()?;

        if self.subcommands.is_empty() {
            writeln!(stdout, "    {} <OPTIONS>", recognized.join(" "))?;
        } else {
            writeln!(stdout, "    {} <SUBCOMMAND>", recognized.join(" "))?;
        }

        writeln!(stdout)?;
        self.print_usage()
    }

    /// Print program and usage information
    pub fn print_info(&self) -> Result<(), io::Error> {
        let mut stdout = STDOUT.lock();
        stdout.reset()?;

        let mut white = ColorSpec::new();
        white.set_fg(Some(Color::White));
        white.set_bold(true);

        stdout.set_color(&white)?;
        writeln!(stdout, "{} {}", &self.package_name, &self.package_version)?;
        stdout.reset()?;

        if !self.package_authors.is_empty() {
            writeln!(stdout, "{}", self.package_authors.join(", "))?;
        }

        if let Some(ref description) = self.package_description {
            writeln!(stdout, "{}", description)?;
        }

        writeln!(stdout)?;
        Ok(())
    }

    /// Print usage information only
    pub fn print_usage(&self) -> Result<(), io::Error> {
        let mut stdout = STDOUT.lock();

        let mut white = ColorSpec::new();
        white.set_fg(Some(Color::White));
        white.set_bold(true);

        if !self.args.is_empty() {
            stdout.set_color(&white)?;
            writeln!(stdout, "FLAGS:")?;
            stdout.reset()?;

            for arg in &self.args {
                arg.print(&mut stdout)?;
            }

            writeln!(stdout)?;
        }

        if !self.subcommands.is_empty() {
            stdout.set_color(&white)?;
            writeln!(stdout, "SUBCOMMANDS:")?;
            stdout.reset()?;

            for command in &self.subcommands {
                command.print_brief(&mut stdout)?;
            }
        }

        Ok(())
    }
}

/// Presenter for flags/options
#[derive(Debug)]
pub struct Argument {
    /// Short name (one char)
    pub short: Option<char>,

    /// Long name
    pub long: Option<String>,

    /// Long param
    pub long_param: Option<String>,

    /// Description
    pub description: Option<String>,
}

impl Argument {
    /// Parse flags from `gumdrop` usage string.
    // TODO(tarcieri): less hacky approach
    fn parse_usage(usage: &str) -> Option<Self> {
        let words = usage.split_whitespace().collect::<Vec<_>>();

        if words.is_empty() {
            return None;
        }

        let mut arg = Self {
            short: None,
            long: None,
            long_param: None,
            description: None,
        };

        if words[0].starts_with('-') && !words[0].starts_with("--") {
            arg.short = Some(words[0].chars().nth(1).expect("truncated short arg"));
            arg.parse_long_arg(&words[1..]);
        } else {
            arg.parse_long_arg(&words);
        }

        if arg.short.is_some() || arg.long.is_some() {
            Some(arg)
        } else {
            None
        }
    }

    /// Parse the long form argument and description
    fn parse_long_arg(&mut self, usage: &[&str]) {
        if usage.is_empty() {
            return;
        }

        let word = usage[0];

        if word.starts_with("--") {
            if usage.len() < 2 {
                return;
            }

            self.long = Some(word[2..].to_owned());

            if usage[1].chars().all(|c| c.is_uppercase() || c == '-') {
                self.long_param = Some(usage[1].to_owned());
                self.parse_description(&usage[2..]);
            } else {
                self.parse_description(&usage[1..])
            }
        } else {
            self.parse_description(usage)
        }
    }

    /// Parse description
    fn parse_description(&mut self, usage: &[&str]) {
        if !usage.is_empty() {
            self.description = Some(usage.join(" "));
        }
    }

    /// Print the argument to the given I/O stream
    fn print(&self, stream: &mut impl Write) -> Result<(), io::Error> {
        let mut arg_str = String::new();

        if let Some(short) = self.short {
            arg_str.push('-');
            arg_str.push(short);

            if self.long.is_some() {
                arg_str.push_str(", ");
            }
        }

        if let Some(ref long) = self.long {
            arg_str.push_str("--");
            arg_str.push_str(long);

            if let Some(ref param) = self.long_param {
                arg_str.push(' ');
                arg_str.push_str(param);
            }
        }

        let description = self.description.as_ref().map(String::as_str).unwrap_or("");
        writeln!(stream, "    {:<25} {}", &arg_str, description)
    }
}

/// Presenter for subcommands
#[derive(Debug)]
pub struct Subcommand {
    /// Subcommand name
    pub name: String,

    /// Subcommand description
    pub description: String,

    /// Subcommand usage
    pub usage: Usage,
}

impl Subcommand {
    /// Parse usage information for a particular subcommand
    // TODO(tarcieri): less hacky approach
    fn parse_usage<C>(usage_string: &str) -> Self
    where
        C: Command,
    {
        let words = usage_string.split_whitespace().collect::<Vec<_>>();
        let name = words[0].to_owned();
        let description = words[1..].join(" ");
        let usage = C::subcommand_usage(&name)
            .unwrap_or_else(|| panic!("error fetching usage for subcommand: {:?}", name));

        Self {
            name,
            description,
            usage,
        }
    }

    /// Print the subcommand to the given I/O stream
    fn print_brief(&self, stream: &mut impl Write) -> Result<(), io::Error> {
        writeln!(stream, "    {:<10} {}", &self.name, &self.description)
    }
}