Skip to content

Commit f6e1128

Browse files
authored
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
1 parent 5642283 commit f6e1128

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

.changes/add-cli-matches-from.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli": minor
3+
"cli-js": minor
4+
---
5+
6+
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.

plugins/cli/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub struct Cli<R: Runtime>(PluginApi<R, Config>);
2929

3030
impl<R: Runtime> Cli<R> {
3131
pub fn matches(&self) -> Result<parser::Matches> {
32-
parser::get_matches(self.0.config(), self.0.app().package_info())
32+
parser::get_matches(self.0.config(), self.0.app().package_info(), None)
33+
}
34+
35+
pub fn matches_from(&self, args: Vec<String>) -> Result<parser::Matches> {
36+
parser::get_matches(self.0.config(), self.0.app().package_info(), Some(args))
3337
}
3438
}
3539

plugins/cli/src/parser.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::collections::HashMap;
1919
mod macros;
2020

2121
/// The resolution of a argument match.
22-
#[derive(Default, Debug, Serialize)]
22+
#[derive(Default, Debug, Serialize, Clone)]
2323
#[non_exhaustive]
2424
pub struct ArgData {
2525
/// - [`Value::Bool`] if it's a flag,
@@ -33,7 +33,7 @@ pub struct ArgData {
3333
}
3434

3535
/// The matched subcommand.
36-
#[derive(Default, Debug, Serialize)]
36+
#[derive(Default, Debug, Serialize, Clone)]
3737
#[non_exhaustive]
3838
pub struct SubcommandMatches {
3939
/// The subcommand name.
@@ -43,7 +43,7 @@ pub struct SubcommandMatches {
4343
}
4444

4545
/// The argument matches of a command.
46-
#[derive(Default, Debug, Serialize)]
46+
#[derive(Default, Debug, Serialize, Clone)]
4747
#[non_exhaustive]
4848
pub struct Matches {
4949
/// Data structure mapping each found arg with its resolution.
@@ -79,7 +79,11 @@ impl Matches {
7979
/// Ok(())
8080
/// });
8181
/// ```
82-
pub fn get_matches(cli: &Config, package_info: &PackageInfo) -> crate::Result<Matches> {
82+
pub fn get_matches(
83+
cli: &Config,
84+
package_info: &PackageInfo,
85+
args: Option<Vec<String>>,
86+
) -> crate::Result<Matches> {
8387
let about = cli
8488
.description()
8589
.unwrap_or(&package_info.description.to_string())
@@ -92,7 +96,14 @@ pub fn get_matches(cli: &Config, package_info: &PackageInfo) -> crate::Result<Ma
9296
Some(&about),
9397
cli,
9498
);
95-
match app.try_get_matches() {
99+
100+
let matches = if let Some(args) = args {
101+
app.try_get_matches_from(args)
102+
} else {
103+
app.try_get_matches()
104+
};
105+
106+
match matches {
96107
Ok(matches) => Ok(get_matches_internal(cli, &matches)),
97108
Err(e) => match e.kind() {
98109
ErrorKind::DisplayHelp => {

0 commit comments

Comments
 (0)