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
//! Shared, reusable help formatting for all clap help surfaces.
//!
//! The CLI has many nested command trees. A single helper here ensures that any
//! command whose help text is rendered uses the same wrapping + indentation
//! behavior.
use anyhow::Result;
use clap::{Command, CommandFactory, FromArgMatches};
/// Reusable help behavior shared by all CLI help entrypoints.
#[derive(Debug, Clone, Copy)]
pub struct HelpSuite;
impl HelpSuite {
/// Apply the project's preferred help formatting style to this command and all
/// nested subcommands.
#[inline]
fn apply_to(command: &mut Command) {
*command = command.clone().next_line_help(true);
for child in command.get_subcommands_mut() {
Self::apply_to(child);
}
}
/// Build a parser command tree with the shared help format applied.
pub fn command<T: CommandFactory>() -> Command {
let mut command = T::command();
Self::apply_to(&mut command);
command
}
/// Parse the active process args with the shared help format applied.
pub fn parse_with_help<T: CommandFactory + FromArgMatches>() -> Result<T> {
let cmd = Self::command::<T>();
let matches = cmd.get_matches();
T::from_arg_matches(&matches).map_err(anyhow::Error::new)
}
}
/// Construct the `newt` parser command with shared help formatting applied.
pub fn help_command() -> Command {
HelpSuite::command::<crate::Cli>()
}
/// Parse `newt` args with shared help formatting applied.
pub fn parse_with_help() -> Result<crate::Cli> {
HelpSuite::parse_with_help::<crate::Cli>()
}