Skip to main content

gen_circleci_orb/help_parser/
mod.rs

1pub mod clap;
2pub mod types;
3
4pub use types::{CliDefinition, ParamType, Parameter, SubCommand};
5
6use anyhow::{Context, Result};
7use std::process::Command;
8
9/// Execute `<binary> --help` (and recursively `<binary> <sub> --help`) to
10/// build a `CliDefinition` from the program's help text.
11pub fn parse_binary(binary: &str) -> Result<CliDefinition> {
12    let top_help = run_help(binary, &[])?;
13    clap::parse_top_level(binary, &top_help)
14}
15
16pub(crate) fn run_help(binary: &str, subcommand: &[&str]) -> Result<String> {
17    let mut args: Vec<&str> = subcommand.to_vec();
18    args.push("--help");
19    let output = Command::new(binary)
20        .args(&args)
21        .output()
22        .with_context(|| format!("failed to run `{binary} {args:?}`"))?;
23    // clap writes --help to stdout; tolerate non-zero exit
24    let text = if output.stdout.is_empty() {
25        String::from_utf8_lossy(&output.stderr).into_owned()
26    } else {
27        String::from_utf8_lossy(&output.stdout).into_owned()
28    };
29    Ok(text)
30}