diff options
| -rw-r--r-- | mingling/Cargo.toml | 2 | ||||
| -rw-r--r-- | mingling_core/Cargo.toml | 1 | ||||
| -rw-r--r-- | mingling_core/src/program/config.rs | 17 | ||||
| -rw-r--r-- | mingling_macros/src/dispatcher_clap.rs | 19 |
4 files changed, 35 insertions, 4 deletions
diff --git a/mingling/Cargo.toml b/mingling/Cargo.toml index 7c5d4c5..47d3109 100644 --- a/mingling/Cargo.toml +++ b/mingling/Cargo.toml @@ -21,7 +21,7 @@ async = ["mingling_core/async", "mingling_macros/async"] default = ["mingling_core/default"] -clap_parser = ["mingling_macros/clap_parser"] +clap_parser = ["mingling_core/clap_parser", "mingling_macros/clap_parser"] full = ["mingling_core/full", "mingling_macros/full", "comp", "parser"] general_renderer = [ "mingling_core/general_renderer", diff --git a/mingling_core/Cargo.toml b/mingling_core/Cargo.toml index b6846e7..b13d3d3 100644 --- a/mingling_core/Cargo.toml +++ b/mingling_core/Cargo.toml @@ -12,6 +12,7 @@ async = [] full = ["comp", "general_renderer"] +clap_parser = [] comp = ["dep:just_template"] debug = ["dep:log", "dep:env_logger"] general_renderer = [ diff --git a/mingling_core/src/program/config.rs b/mingling_core/src/program/config.rs index 2f5de4c..78c6185 100644 --- a/mingling_core/src/program/config.rs +++ b/mingling_core/src/program/config.rs @@ -6,6 +6,21 @@ pub struct ProgramStdoutSetting { /// Render results and output pub render_output: bool, + + #[cfg(feature = "clap_parser")] + /// Behavior when Clap Dispatcher outputs help information + pub clap_help_print_behaviour: ClapHelpPrintBehaviour, +} + +#[cfg(feature = "clap_parser")] +#[derive(Debug, Default, Clone)] +pub enum ClapHelpPrintBehaviour { + /// Write to RenderResult + WriteToRenderResult, + + /// Print directly + #[default] + PrintDirectly, } impl Default for ProgramStdoutSetting { @@ -13,6 +28,8 @@ impl Default for ProgramStdoutSetting { ProgramStdoutSetting { error_output: true, render_output: true, + #[cfg(feature = "clap_parser")] + clap_help_print_behaviour: ClapHelpPrintBehaviour::default(), } } } diff --git a/mingling_macros/src/dispatcher_clap.rs b/mingling_macros/src/dispatcher_clap.rs index 765a448..5184bce 100644 --- a/mingling_macros/src/dispatcher_clap.rs +++ b/mingling_macros/src/dispatcher_clap.rs @@ -164,6 +164,7 @@ impl Parse for DispatcherClapInput { } } +#[cfg(feature = "clap_parser")] pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream { let attr_input = parse_macro_input!(attr as DispatcherClapInput); let input_struct = parse_macro_input!(item as ItemStruct); @@ -241,9 +242,21 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream #[allow(non_snake_case)] #[::mingling::macros::help] fn #help_fn_name(_prev: #struct_name) { - <#struct_name as ::clap::CommandFactory>::command() - .write_help(r) - .unwrap(); + use clap::ColorChoice; + + let this = ::mingling::this::<#program_ident>(); + match this.stdout_setting.clap_help_print_behaviour { + ::mingling::ClapHelpPrintBehaviour::WriteToRenderResult => { + <#struct_name as ::clap::CommandFactory>::command() + .color(ColorChoice::Always) + .write_help(r) + .unwrap(); + } + ::mingling::ClapHelpPrintBehaviour::PrintDirectly => { + let mut command = <#struct_name as ::clap::CommandFactory>::command(); + command.print_help().unwrap(); + } + } } }) } else { |
