From 23930c24b7eaf519d8ed36e4f433790eeb4d07fb Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 29 May 2026 14:24:25 +0800 Subject: Update docs and examples for clap binding with optional error parameter --- examples/example-clap-binding/src/main.rs | 6 +++--- mingling/src/example_docs.rs | 29 +++++++++++++++++++---------- mingling_macros/src/dispatcher_clap.rs | 5 +++++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/examples/example-clap-binding/src/main.rs b/examples/example-clap-binding/src/main.rs index aed437d..d3ad573 100644 --- a/examples/example-clap-binding/src/main.rs +++ b/examples/example-clap-binding/src/main.rs @@ -71,9 +71,9 @@ fn main() { // vvvvvvv vvvvvvvvvvvv vvvvvvvv #[derive(Default, clap::Parser, Groupped)] #[dispatcher_clap( - "greet", CMDGreet, // Bind EntryGreet to "greet" command - help = true, // Generate clap help for EntryGreet - error = ErrorGreetParsed // Generate and bind error type for parse failure + "greet", CMDGreet, // Bind EntryGreet to "greet" command + help = true, // Generate clap help for EntryGreet + error = ErrorGreetParsed, // Generate and bind error type for parse failure // ^^^^^\__ Using `error` intercepts parse failure information into the specified type, // which is then rendered by the renderer )] diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs index b5a78ea..a7e8d2e 100644 --- a/mingling/src/example_docs.rs +++ b/mingling/src/example_docs.rs @@ -294,8 +294,8 @@ pub mod example_basic {} /// > This example demonstrates how to bind clap_derive to Mingling /// /// **Note**: -/// If the `error` parameter of the `dispatcher_clap!` macro is enabled, parameters will be parsed using `try_parse_from` -/// This will cause clap's ColorChoice output to be plain text without ANSI colors +/// If the `error` parameter of the `dispatcher_clap!` macro is enabled, arguments will be parsed using `try_parse_from`. +/// If you need such output to support ANSI colors, enable the `color` feature of `clap`. /// /// Run: /// ```bash @@ -344,27 +344,36 @@ pub mod example_basic {} /// # Import `clap` to your project /// [dependencies.clap] /// version = "4.6.1" -/// # Enable `derive` features -/// features = ["derive"] +/// features = [ +/// # Enable `derive` feature to support `clap::Parser` +/// "derive", +/// # Enable `color` feature to support ANSI colors +/// "color", +/// ] /// ``` /// /// Source code (./src/main.rs) /// ```ignore -/// use mingling::{macros::dispatcher_clap, prelude::*, Groupped}; +/// use mingling::{macros::dispatcher_clap, prelude::*, setup::BasicProgramSetup, Groupped}; /// /// fn main() { /// let mut program = ThisProgram::new(); /// +/// // --------- IMPORTANT --------- +/// // Introduce BasicProgramSetup to support ["--help", "-h"] options +/// program.with_setup(BasicProgramSetup); +/// /// // Set clap help output mode /// program.stdout_setting.clap_help_print_behaviour = -/// mingling::ClapHelpPrintBehaviour::PrintDirectly; -/// // mingling::ClapHelpPrintBehaviour::WriteToRenderResult +/// mingling::ClapHelpPrintBehaviour::WriteToRenderResult; +/// // mingling::ClapHelpPrintBehaviour::PrintDirectly /// // /// // PrintDirectly: /// // Let Clap print help information directly to stdout /// // /// // WriteToRenderResult: /// // Capture Clap's help information and write to RenderResult +/// // --------- IMPORTANT --------- /// /// program.with_dispatcher(CMDGreet); /// program.exec_and_exit(); @@ -378,9 +387,9 @@ pub mod example_basic {} /// // vvvvvvv vvvvvvvvvvvv vvvvvvvv /// #[derive(Default, clap::Parser, Groupped)] /// #[dispatcher_clap( -/// "greet", CMDGreet, // Bind EntryGreet to "greet" command -/// help = true, // Generate clap help for EntryGreet -/// error = ErrorGreetParsed // Generate and bind error type for parse failure +/// "greet", CMDGreet, // Bind EntryGreet to "greet" command +/// help = true, // Generate clap help for EntryGreet +/// error = ErrorGreetParsed, // Generate and bind error type for parse failure /// // ^^^^^\__ Using `error` intercepts parse failure information into the specified type, /// // which is then rendered by the renderer /// )] diff --git a/mingling_macros/src/dispatcher_clap.rs b/mingling_macros/src/dispatcher_clap.rs index 8ef2125..5a6cf4c 100644 --- a/mingling_macros/src/dispatcher_clap.rs +++ b/mingling_macros/src/dispatcher_clap.rs @@ -23,6 +23,11 @@ impl Parse for ClapOptions { // Parse leading comma input.parse::()?; + // Allow trailing comma + if input.is_empty() { + break; + } + let key: Ident = input.parse()?; input.parse::()?; -- cgit