add optional args to cli get_matches

pull/2787/head
Mike Wyatt 4 weeks ago
parent 5642283dba
commit afb5ade2da

@ -28,8 +28,8 @@ pub use parser::{ArgData, Matches, SubcommandMatches};
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())
pub fn matches(&self, args: Option<Vec<String>>) -> Result<parser::Matches> {
parser::get_matches(self.0.config(), self.0.app().package_info(), args)
}
}
@ -45,7 +45,7 @@ impl<R: Runtime, T: Manager<R>> CliExt<R> for T {
#[tauri::command]
fn cli_matches<R: Runtime>(_app: AppHandle<R>, cli: State<'_, Cli<R>>) -> Result<parser::Matches> {
cli.matches()
cli.matches(None::<Vec<String>>)
}
pub fn init<R: Runtime>() -> TauriPlugin<R, Config> {

@ -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,17 @@ 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(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());
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