feat(cli): Pass optional args to get matches (#2787)

* add optional args to cli get_matches

* guess that doesn't apply

* feedback changes

* clone?

* update changeset

* also reference cli-js
pull/2791/head
Mike Wyatt 4 weeks ago committed by GitHub
parent 5642283dba
commit f6e11282a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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.

@ -29,7 +29,11 @@ pub struct Cli<R: Runtime>(PluginApi<R, Config>);
impl<R: Runtime> Cli<R> {
pub fn matches(&self) -> Result<parser::Matches> {
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<String>) -> Result<parser::Matches> {
parser::get_matches(self.0.config(), self.0.app().package_info(), Some(args))
}
}

@ -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<Matches> {
pub fn get_matches(
cli: &Config,
package_info: &PackageInfo,
args: Option<Vec<String>>,
) -> crate::Result<Matches> {
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<Ma
Some(&about),
cli,
);
match app.try_get_matches() {
let matches = if let Some(args) = args {
app.try_get_matches_from(args)
} else {
app.try_get_matches()
};
match matches {
Ok(matches) => Ok(get_matches_internal(cli, &matches)),
Err(e) => match e.kind() {
ErrorKind::DisplayHelp => {

Loading…
Cancel
Save