Skip to content

Commit 6e7c9a1

Browse files
--list-X for registries
1 parent 9fb85f1 commit 6e7c9a1

File tree

8 files changed

+31
-1
lines changed

8 files changed

+31
-1
lines changed

operation/src/aggregate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl Optionsable for ImplBe2 {
3434
fn options(opt: &mut OptionsPile<Options>) {
3535
opt.add_sub(|p| &mut p.aggs.0, aggregator::REGISTRY.labelled_single_options(&["a", "agg", "aggregator"], "aggregators to compute"));
3636
opt.add_sub(|p| &mut p.aggs.0, aggregator::REGISTRY.labelled_multiple_options(&["a", "agg", "aggregator"]));
37+
opt.add(aggregator::REGISTRY.help_options("aggregator"));
3738
opt.add_sub(|p| &mut p.tru, TwoRecordUnionOption::new_options());
3839
opt.match_zero(&["incremental"], |p| p.incremental.set(), "Output one record after each input (instead of one at end)");
3940
opt.match_zero(&["no-incremental"], |p| p.incremental.clear(), "(default)");

operation/src/clumper_options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl Optionsable for ClumperOptions {
1818
fn options(opt: &mut OptionsPile<ClumperOptions>) {
1919
opt.add_sub(|p| &mut (p.0).0, clumper::REGISTRY.single_options(&["c", "clumper"], "clumpers to bucket by"));
2020
opt.add_sub(|p| &mut (p.0).0, clumper::REGISTRY.multiple_options(&["c", "clumper"]));
21+
opt.add(clumper::REGISTRY.help_options("clumper"));
2122
opt.match_single(&["k", "key"], |p, a| {
2223
for a in a.split(',') {
2324
(p.0).0.push(clumper::key::Impl::init(&[a])?);

operation/src/decollate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl Optionsable for ImplBe2 {
2727
fn options(opt: &mut OptionsPile<Options>) {
2828
opt.add_sub(|p| &mut p.deaggs.0, deaggregator::REGISTRY.single_options(&["d", "deagg", "deaggregator"], "deaggregator to ... do"));
2929
opt.add_sub(|p| &mut p.deaggs.0, deaggregator::REGISTRY.multiple_options(&["d", "deagg", "deaggregator"]));
30+
opt.add(deaggregator::REGISTRY.help_options("deaggregator"));
3031
}
3132
}
3233

operation/src/eval.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl<B: EvalBe + 'static> Optionsable for EvalBe2<B> {
9090
opt.match_zero(&["no-invert"], |p| p.invert.clear(), "(default)");
9191
opt.match_extra_soft(|p, a| p.code.code.maybe_set_str(a), "code to execute");
9292
opt.match_single(&["engine"], |p, a| p.code.engine.set_str(a), "'engine' to execute code with");
93+
opt.add(executor::REGISTRY.help_options("executor"));
9394
opt.match_zero(&["lua"], |p| p.code.engine.set("lua".to_string()), "evaluate as lua");
9495
opt.match_zero(&["input-lines"], |p| p.input.set(InputType::Lines()), "provide input as string lines");
9596
opt.match_zero(&["input-records"], |p| p.input.set(InputType::Records()), "provide input as structured records");

operation/src/sort.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl Optionsable for ImplBe2 {
2929

3030
fn options(opt: &mut OptionsPile<Options>) {
3131
opt.add_sub(|p| &mut p.sorts, SortOptions::new_options(&["s", "sort"], "sorts"));
32+
opt.add(SortOptions::help_options());
3233
opt.match_single(&["l", "lex", "lexical"], |p, a| {
3334
for a in a.split(',') {
3435
p.sorts.push(sorts::lexical::Impl::init(&[a])?);

operation/src/sort_options.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ impl SortOptions {
1818
opt.add_sub(|p| &mut (p.0).0, sorts::REGISTRY.multiple_options(aliases));
1919
}
2020

21+
pub fn help_options<X: 'static>() -> OptionsPile<X> {
22+
return sorts::REGISTRY.help_options("sort");
23+
}
24+
2125
pub fn new_options(aliases: &[&str], help: impl ToOptionsHelp) -> OptionsPile<SortOptions> {
2226
let mut opt = OptionsPile::new();
2327
Self::options(&mut opt, aliases, help);

operation/src/to_ptable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl Optionsable for ImplBe2 {
4848
opt.match_single(&["v"], |p, a| p.vk.push_split(a), "keys to use as values (default: whatever keys didn't get used otherwise)");
4949
opt.add_sub(|p| &mut p.xs, SortOptions::new_options(&["xs"], "sorts for x 'records'"));
5050
opt.add_sub(|p| &mut p.ys, SortOptions::new_options(&["ys"], "sorts for y 'records'"));
51+
opt.add(SortOptions::help_options());
5152
}
5253
}
5354

registry/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,26 @@ use validates::ValidationResult;
1212

1313
pub struct Registry<R> {
1414
map: HashMap<&'static str, (usize, Box<Fn(&[&str]) -> ValidationResult<R> + Send + Sync>)>,
15+
aliaseses: Vec<Vec<&'static str>>,
1516
}
1617

1718
impl<R> Default for Registry<R> {
1819
fn default() -> Self {
1920
return Registry {
2021
map: HashMap::new(),
22+
aliaseses: Vec::new(),
2123
};
2224
}
2325
}
2426

2527
impl<R: 'static> Registry<R> {
2628
pub fn add<I: Registrant<R> + 'static>(&mut self) {
27-
for name in I::names() {
29+
let names = I::names();
30+
for name in names.iter() {
2831
let prev = self.map.insert(name, (I::argct(), Box::new(I::init)));
2932
assert!(prev.is_none(), "registry collision for {}", name);
3033
}
34+
self.aliaseses.push(names);
3135
}
3236

3337
pub fn find(&self, name: &str, args: &[&str]) -> ValidationResult<R> {
@@ -103,6 +107,22 @@ impl<R: 'static> Registry<R> {
103107
}, help);
104108
return opt;
105109
}
110+
111+
pub fn help_options<X: 'static>(&'static self, type_name: &str) -> OptionsPile<X> {
112+
let mut opt = OptionsPile::<X>::new();
113+
let aliaseses = &self.aliaseses;
114+
opt.match_zero(&[&format!("list-{}", type_name)], move |_p| {
115+
return ValidationError::help(aliaseses.iter().map(|aliases| {
116+
let (first, rest) = aliases.split_first().unwrap();
117+
let mut line = first.to_string();
118+
if !rest.is_empty() {
119+
line.push_str(&format!(" [{}]", rest.join(", ")));
120+
}
121+
return line;
122+
}).collect());
123+
}, format!("list {}s", type_name));
124+
return opt;
125+
}
106126
}
107127

108128
#[macro_export]

0 commit comments

Comments
 (0)