/
help.rs
59 lines (48 loc) · 1.81 KB
/
help.rs
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
use super::helpers::{GlobalConfiguration, Subcommand};
use anyhow::Result;
use clap::{CommandFactory, Parser};
use clap_mangen::Man;
use std::io::Write;
use std::process::{Command, ExitCode, Stdio};
fn find_subcommand(cmd: clap::Command, args: &[String]) -> Option<clap::Command> {
if args.is_empty() {
Some(cmd)
} else {
let mut iter = args.iter();
let mut cmd = cmd;
while let Some(s) = iter.next() {
cmd = cmd.find_subcommand(s)?.clone();
}
Some(cmd)
}
}
/// Display a manpage for selected subcommands.
#[derive(Parser)]
pub struct Opts {
/// Name of the (chain of) subcommand(s) to read the manpage of.
subcommand: Vec<String>,
}
// TODO: installing manpages requires <https://github.com/rust-lang/cargo/issues/2729>, but we
// could go ahead and install them ourselves to /path/of/pineappl/../share/man/man1/pineappl.1.gz
impl Subcommand for Opts {
fn run(&self, _: &GlobalConfiguration) -> Result<ExitCode> {
// TODO: SYNOPSIS doesn't show top-level subcommands: instead of `pineappl-convolute` only
// `convolute` is shown
// TODO: VERSION shows the version with two `v` in front of it
let mut cmd = crate::Opts::command();
cmd.build();
// TODO: if `unwrap` fails the arguments were wrong
let man = Man::new(find_subcommand(cmd, &self.subcommand).unwrap());
let mut buffer = Vec::new();
man.render(&mut buffer)?;
// TODO: if `man` can't be found display the usual `--help` message
let mut child = Command::new("man")
.args(["/dev/stdin"])
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.spawn()?;
child.stdin.take().unwrap().write_all(&buffer)?;
child.wait()?;
Ok(ExitCode::SUCCESS)
}
}