Skip to content

Commit b938076

Browse files
help "operation"
1 parent b7c08d8 commit b938076

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

operation/src/help.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use opts::parser::OptionsPile;
2+
use opts::parser::Optionsable;
3+
use opts::vals::OptionalStringOption;
4+
use std::sync::Arc;
5+
use stream::Stream;
6+
use super::OperationBe;
7+
use super::OperationRegistrant;
8+
use validates::Validates;
9+
use validates::ValidationError;
10+
use validates::ValidationResult;
11+
12+
#[derive(Default)]
13+
pub struct Options {
14+
name: OptionalStringOption,
15+
}
16+
17+
pub enum Void {
18+
}
19+
20+
impl Validates for Options {
21+
type Target = Void;
22+
23+
fn validate(self) -> ValidationResult<Void> {
24+
let name = self.name.validate()?;
25+
return ValidationError::help(match name {
26+
Some(name) => super::REGISTRY.find(&name, &[])?.help(),
27+
None => super::REGISTRY.help_list(),
28+
});
29+
}
30+
}
31+
32+
pub(crate) type Impl = OperationRegistrant<ImplBe>;
33+
34+
pub(crate) struct ImplBe();
35+
36+
impl Optionsable for ImplBe {
37+
type Options = Options;
38+
39+
fn options(opt: &mut OptionsPile<Options>) {
40+
opt.match_extra_soft(|p, a| p.name.maybe_set_str(a), "operation to show help for");
41+
}
42+
}
43+
44+
impl OperationBe for ImplBe {
45+
fn names() -> Vec<&'static str> {
46+
return vec!["help"];
47+
}
48+
49+
fn help_msg() -> &'static str {
50+
return "show help for an operation";
51+
}
52+
53+
fn get_extra(_o: Arc<Void>) -> Vec<String> {
54+
panic!();
55+
}
56+
57+
fn stream(_o: Arc<Void>) -> Stream {
58+
panic!();
59+
}
60+
}

operation/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ registry! {
6060
from_regex,
6161
from_split,
6262
grep,
63+
help,
6364
join,
6465
multiplex,
6566
parse,
@@ -92,6 +93,7 @@ pub trait OperationBe: Optionsable {
9293
}
9394

9495
pub trait OperationInbox {
96+
fn help(&self) -> Vec<String>;
9597
fn parse(&self, args: &mut Vec<String>) -> ValidationResult<StreamWrapper>;
9698
}
9799

@@ -126,6 +128,10 @@ impl<B: OperationBe + 'static> OperationInboxImpl<B> where <B::Options as Valida
126128
}
127129

128130
impl<B: OperationBe + 'static> OperationInbox for OperationInboxImpl<B> where <B::Options as Validates>::Target: Send + Sync {
131+
fn help(&self) -> Vec<String> {
132+
return Self::static_help();
133+
}
134+
129135
fn parse(&self, args: &mut Vec<String>) -> ValidationResult<StreamWrapper> {
130136
let opt = Self::new_options();
131137
let o = opt.to_parser().parse(args);

0 commit comments

Comments
 (0)