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
//! Help command

use super::Command;
use crate::runnable::Runnable;
use gumdrop::{Error, Opt, Options, Parser};
use std::marker::PhantomData;

/// Help command which prints usage information.
///
/// Generic over `Command` types.
#[derive(Debug, Default)]
pub struct Help<C: Command> {
    /// Obtain help for a particular subcommand
    pub opts: Vec<String>,

    /// Command for which to obtain usage information
    command: PhantomData<C>,
}

impl<C> Command for Help<C>
where
    C: Command,
{
    fn name() -> &'static str {
        C::name()
    }

    fn description() -> &'static str {
        C::description()
    }

    fn version() -> &'static str {
        C::version()
    }

    fn authors() -> &'static str {
        C::authors()
    }
}

impl<C> Options for Help<C>
where
    C: Command,
{
    fn parse<S: AsRef<str>>(parser: &mut Parser<'_, S>) -> Result<Self, Error> {
        let mut opts = vec![];

        while let Some(opt) = parser.next_opt() {
            match opt {
                Opt::Free(free_opt) => opts.push(free_opt.to_owned()),
                _ => return Err(Error::unexpected_argument(opt)),
            }
        }

        Ok(Self {
            opts,
            command: PhantomData,
        })
    }

    fn parse_command<S: AsRef<str>>(
        _name: &str,
        parser: &mut Parser<'_, S>,
    ) -> Result<Self, Error> {
        // TODO(tarcieri): is this necessary or the best approach?
        Self::parse(parser)
    }

    fn usage() -> &'static str {
        ""
    }

    fn command_usage(_command: &str) -> Option<&'static str> {
        None
    }

    fn command_list() -> Option<&'static str> {
        None
    }
}

impl<C> Runnable for Help<C>
where
    C: Command,
{
    /// Print help information for the given command
    fn run(&self) {
        C::print_usage_and_exit(&self.opts);
    }
}