diff --git a/.changes/add-cli-matches-from.md b/.changes/add-cli-matches-from.md new file mode 100644 index 00000000..4703a5ba --- /dev/null +++ b/.changes/add-cli-matches-from.md @@ -0,0 +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. diff --git a/plugins/cli/src/lib.rs b/plugins/cli/src/lib.rs index 3e144376..e927a348 100644 --- a/plugins/cli/src/lib.rs +++ b/plugins/cli/src/lib.rs @@ -29,7 +29,11 @@ pub struct Cli(PluginApi); impl Cli { pub fn matches(&self) -> Result { - parser::get_matches(self.0.config(), self.0.app().package_info()) + 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)) } } diff --git a/plugins/cli/src/parser.rs b/plugins/cli/src/parser.rs index 64ec2fb7..466885f1 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. @@ -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,14 @@ pub fn get_matches(cli: &Config, package_info: &PackageInfo) -> crate::Result Ok(get_matches_internal(cli, &matches)), Err(e) => match e.kind() { ErrorKind::DisplayHelp => {