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
//! Application (sub)command(s), i.e. app entry points

mod entrypoint;
mod help;
mod usage;

pub use self::{entrypoint::EntryPoint, help::Help, usage::Usage};
#[doc(hidden)]
pub use abscissa_derive::Command;

use crate::{runnable::Runnable, terminal};
use gumdrop::Options;
use std::fmt::Debug;

/// Subcommand of an application: derives or otherwise implements the `Options`
/// trait, but also has a `call()` method which can be used to invoke the given
/// (sub)command.
pub trait Command: Debug + Options + Runnable {
    /// Name of this program as a string
    fn name() -> &'static str;

    /// Description of this program
    fn description() -> &'static str;

    /// Version of this program
    fn version() -> &'static str;

    /// Authors of this program
    fn authors() -> &'static str;

    /// Parse command-line arguments from a string iterator
    fn from_args<A: IntoIterator<Item = String>>(into_args: A) -> Self {
        let args: Vec<_> = into_args.into_iter().collect();

        Self::parse_args_default(args.as_slice()).unwrap_or_else(|err| {
            terminal::init();
            Usage::for_command::<Self>().print_error_and_exit(err, args.as_slice());
        })
    }

    /// Parse command-line arguments from the environment
    fn from_env_args() -> Self {
        let mut args = ::std::env::args();
        assert!(args.next().is_some(), "expected one argument but got zero");
        Self::from_args(args)
    }

    /// Print usage information and exit
    fn print_usage_and_exit(args: &[String]) -> ! {
        Usage::for_command::<Self>().print_subcommand_and_exit(args);
    }

    /// Get usage information for a particular subcommand (if available)
    fn subcommand_usage(_command: &str) -> Option<Usage> {
        None
    }
}

#[cfg(test)]
mod tests {
    use crate::{Command, Options, Runnable};

    #[derive(Command, Debug, Options)]
    pub struct DummyCommand {}

    impl Runnable for DummyCommand {
        fn run(&self) {
            panic!("unimplemented");
        }
    }

    #[test]
    fn derived_command_test() {
        assert_eq!(DummyCommand::name(), "abscissa_core");
    }
}