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
#[doc(hidden)]
pub use abscissa_derive::Command;
use crate::{runnable::Runnable, terminal};
use clap::{FromArgMatches, Parser};
use std::{env, fmt::Debug};
use termcolor::ColorChoice;
pub trait Command: Debug + FromArgMatches + Runnable {
fn name() -> &'static str;
fn description() -> &'static str;
fn authors() -> &'static str;
fn parse_args<A: IntoIterator<Item = String>>(into_args: A) -> Self
where
Self: Parser,
{
let args = into_args.into_iter().collect::<Vec<_>>();
Self::try_parse_from(args.as_slice()).unwrap_or_else(|err| {
terminal::init(ColorChoice::Auto);
err.exit()
})
}
fn parse_env_args() -> Self
where
Self: Parser,
{
Self::parse_args(env::args())
}
}
#[cfg(test)]
mod tests {
use crate::{Command, Runnable};
use clap::Parser;
#[derive(Command, Debug, Parser)]
pub struct DummyCommand {}
impl Runnable for DummyCommand {
fn run(&self) {
panic!("unimplemented");
}
}
#[test]
fn derived_command_test() {
assert_eq!(DummyCommand::name(), "abscissa_core");
}
}