Skip to content

Commit

Permalink
Add preliminary help subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Feb 1, 2023
1 parent 0acfea0 commit 76a2200
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pineappl_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ version.workspace = true
[dependencies]
anyhow = "1.0.48"
clap = { features = ["deprecated", "derive"], version = "4.0.32" }
clap_mangen = "0.2.7"
enum_dispatch = "0.3.7"
flate2 = { optional = true, version = "1.0.22" }
git-version = "0.3.5"
Expand Down
49 changes: 49 additions & 0 deletions pineappl_cli/src/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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};

/// 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>,
}

impl Subcommand for Opts {
fn run(&self, _: &GlobalConfiguration) -> Result<ExitCode> {
let cmd = crate::Opts::command();
let subcmd = if self.subcommand.is_empty() {
cmd
} else {
let mut iter = self.subcommand.iter();
let mut cmd = cmd;
while let Some(s) = iter.next() {
// TODO: if `unwrap` fails the arguments to `help` are wrong
cmd = cmd.find_subcommand(s).unwrap().clone();
}
cmd
};
let man = Man::new(subcmd);
let mut buffer = Vec::new();

man.render(&mut buffer)?;

// TODO: check remaining `unwrap`s

// TODO: is this the best way? What do we do when `man` isn't available (Windows, ...)?
let mut child = Command::new("man")
.args(["-l", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.spawn()
.unwrap();

child.stdin.take().unwrap().write_all(&buffer).unwrap();
child.wait().unwrap();

Ok(ExitCode::SUCCESS)
}
}
2 changes: 2 additions & 0 deletions pineappl_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod convolute;
mod delete;
mod diff;
mod evolve;
mod help;
mod helpers;
mod import;
mod info;
Expand Down Expand Up @@ -59,6 +60,7 @@ enum SubcommandEnum {
Delete(delete::Opts),
Diff(diff::Opts),
Evolve(evolve::Opts),
Help(help::Opts),
Import(import::Opts),
Info(info::Opts),
Merge(merge::Opts),
Expand Down

0 comments on commit 76a2200

Please sign in to comment.