From afb5ade2da60479aa4bf4892bf5d22c67aa2cf43 Mon Sep 17 00:00:00 2001 From: Mike Wyatt Date: Sat, 21 Jun 2025 14:53:42 -0300 Subject: [PATCH 1/6] add optional args to cli get_matches --- plugins/cli/src/lib.rs | 6 +++--- plugins/cli/src/parser.rs | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/plugins/cli/src/lib.rs b/plugins/cli/src/lib.rs index 3e1443769e..df9fe75c88 100644 --- a/plugins/cli/src/lib.rs +++ b/plugins/cli/src/lib.rs @@ -28,8 +28,8 @@ pub use parser::{ArgData, Matches, SubcommandMatches}; pub struct Cli(PluginApi); impl Cli { - pub fn matches(&self) -> Result { - parser::get_matches(self.0.config(), self.0.app().package_info()) + pub fn matches(&self, args: Option>) -> Result { + parser::get_matches(self.0.config(), self.0.app().package_info(), args) } } @@ -45,7 +45,7 @@ impl> CliExt for T { #[tauri::command] fn cli_matches(_app: AppHandle, cli: State<'_, Cli>) -> Result { - cli.matches() + cli.matches(None::>) } pub fn init() -> TauriPlugin { diff --git a/plugins/cli/src/parser.rs b/plugins/cli/src/parser.rs index 64ec2fb70d..25e125689d 100644 --- a/plugins/cli/src/parser.rs +++ b/plugins/cli/src/parser.rs @@ -79,7 +79,11 @@ impl Matches { /// Ok(()) /// }); /// ``` -pub fn get_matches(cli: &Config, package_info: &PackageInfo) -> crate::Result { +pub fn get_matches( + cli: &Config, + package_info: &PackageInfo, + args: Option>, +) -> crate::Result { let about = cli .description() .unwrap_or(&package_info.description.to_string()) @@ -92,7 +96,17 @@ pub fn get_matches(cli: &Config, package_info: &PackageInfo) -> crate::Result Ok(get_matches_internal(cli, &matches)), Err(e) => match e.kind() { ErrorKind::DisplayHelp => { From 8408ffb0372431624f2f163ba7ce57d9e6603d11 Mon Sep 17 00:00:00 2001 From: Mike Wyatt Date: Sat, 21 Jun 2025 15:07:52 -0300 Subject: [PATCH 2/6] guess that doesn't apply --- plugins/cli/src/parser.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/plugins/cli/src/parser.rs b/plugins/cli/src/parser.rs index 25e125689d..6cfe54e152 100644 --- a/plugins/cli/src/parser.rs +++ b/plugins/cli/src/parser.rs @@ -97,10 +97,7 @@ pub fn get_matches( cli, ); - let matches = if let Some(mut args) = args { - // try_get_matches_from assumes the first argument is the command name, - // so we prepend something to get the correct matches. - args.insert(0, package_info.name.clone()); + let matches = if let Some(args) = args { app.try_get_matches_from(args) } else { app.try_get_matches() From e6eb455f241cc4940ddf394287b7d59276304a64 Mon Sep 17 00:00:00 2001 From: Mike Wyatt Date: Sun, 22 Jun 2025 12:40:54 -0300 Subject: [PATCH 3/6] feedback changes --- .changes/add-optional-args-cli.md | 5 +++++ plugins/cli/src/lib.rs | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changes/add-optional-args-cli.md diff --git a/.changes/add-optional-args-cli.md b/.changes/add-optional-args-cli.md new file mode 100644 index 0000000000..6d4db17141 --- /dev/null +++ b/.changes/add-optional-args-cli.md @@ -0,0 +1,5 @@ +--- +"cli": minor +--- + +Added `tauri_plugin_cli::matches_from`. This can be combined with the `args` passed to the callback of `tauri_plugin_single_instance` to parse the command line arguments passed to subsequent instances of the application. diff --git a/plugins/cli/src/lib.rs b/plugins/cli/src/lib.rs index df9fe75c88..e927a348b5 100644 --- a/plugins/cli/src/lib.rs +++ b/plugins/cli/src/lib.rs @@ -28,8 +28,12 @@ pub use parser::{ArgData, Matches, SubcommandMatches}; pub struct Cli(PluginApi); impl Cli { - pub fn matches(&self, args: Option>) -> Result { - parser::get_matches(self.0.config(), self.0.app().package_info(), args) + pub fn matches(&self) -> Result { + parser::get_matches(self.0.config(), self.0.app().package_info(), None) + } + + pub fn matches_from(&self, args: Vec) -> Result { + parser::get_matches(self.0.config(), self.0.app().package_info(), Some(args)) } } @@ -45,7 +49,7 @@ impl> CliExt for T { #[tauri::command] fn cli_matches(_app: AppHandle, cli: State<'_, Cli>) -> Result { - cli.matches(None::>) + cli.matches() } pub fn init() -> TauriPlugin { From 0fbe8403d654bc090e88ef9bb646428bc359fdca Mon Sep 17 00:00:00 2001 From: Mike Wyatt Date: Sun, 22 Jun 2025 12:49:33 -0300 Subject: [PATCH 4/6] clone? --- plugins/cli/src/parser.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/cli/src/parser.rs b/plugins/cli/src/parser.rs index 6cfe54e152..466885f14e 100644 --- a/plugins/cli/src/parser.rs +++ b/plugins/cli/src/parser.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; mod macros; /// The resolution of a argument match. -#[derive(Default, Debug, Serialize)] +#[derive(Default, Debug, Serialize, Clone)] #[non_exhaustive] pub struct ArgData { /// - [`Value::Bool`] if it's a flag, @@ -33,7 +33,7 @@ pub struct ArgData { } /// The matched subcommand. -#[derive(Default, Debug, Serialize)] +#[derive(Default, Debug, Serialize, Clone)] #[non_exhaustive] pub struct SubcommandMatches { /// The subcommand name. @@ -43,7 +43,7 @@ pub struct SubcommandMatches { } /// The argument matches of a command. -#[derive(Default, Debug, Serialize)] +#[derive(Default, Debug, Serialize, Clone)] #[non_exhaustive] pub struct Matches { /// Data structure mapping each found arg with its resolution. From 90bd96a89090e9f642c70524f4c0071b03be7eea Mon Sep 17 00:00:00 2001 From: Mike Wyatt Date: Sun, 22 Jun 2025 12:50:57 -0300 Subject: [PATCH 5/6] update changeset --- .changes/add-cli-matches-from.md | 5 +++++ .changes/add-optional-args-cli.md | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 .changes/add-cli-matches-from.md delete mode 100644 .changes/add-optional-args-cli.md diff --git a/.changes/add-cli-matches-from.md b/.changes/add-cli-matches-from.md new file mode 100644 index 0000000000..837991c44e --- /dev/null +++ b/.changes/add-cli-matches-from.md @@ -0,0 +1,5 @@ +--- +"cli": minor +--- + +Added `Cli.matches_from(args)`. This can be combined with the `args` passed to the callback of `tauri_plugin_single_instance::init` to parse the command line arguments passed to subsequent instances of the application. diff --git a/.changes/add-optional-args-cli.md b/.changes/add-optional-args-cli.md deleted file mode 100644 index 6d4db17141..0000000000 --- a/.changes/add-optional-args-cli.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"cli": minor ---- - -Added `tauri_plugin_cli::matches_from`. This can be combined with the `args` passed to the callback of `tauri_plugin_single_instance` to parse the command line arguments passed to subsequent instances of the application. From 9763dc236617339cd9c4e99dcf2b11cb92c837af Mon Sep 17 00:00:00 2001 From: Mike Wyatt Date: Sun, 22 Jun 2025 12:53:26 -0300 Subject: [PATCH 6/6] also reference cli-js --- .changes/add-cli-matches-from.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changes/add-cli-matches-from.md b/.changes/add-cli-matches-from.md index 837991c44e..4703a5ba7a 100644 --- a/.changes/add-cli-matches-from.md +++ b/.changes/add-cli-matches-from.md @@ -1,5 +1,6 @@ --- "cli": minor +"cli-js": minor --- Added `Cli.matches_from(args)`. This can be combined with the `args` passed to the callback of `tauri_plugin_single_instance::init` to parse the command line arguments passed to subsequent instances of the application.