diff --git a/.changes/add-app-get-matches-helper.md b/.changes/add-app-get-matches-helper.md new file mode 100644 index 00000000000..0e0c300e713 --- /dev/null +++ b/.changes/add-app-get-matches-helper.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Added the `App::get_cli_matches` helper function. diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 8674f46add8..2ed6c7c1b6f 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -2237,12 +2237,12 @@ impl PackageConfig { /// [`tauri init`](https://tauri.studio/v1/api/cli#init) command that lives in /// your Tauri application source directory (src-tauri). Once generated, you may /// modify it at will to customize your Tauri application. -/// +/// /// In addition to the JSON defined on the `tauri.conf.json` file, Tauri can /// read a platform-specific configuration from `tauri.linux.conf.json`, /// `tauri.windows.conf.json`, and `tauri.macos.conf.json` and merges it with /// the main `tauri.conf.json` configuration. -/// +/// /// ```json title="Example tauri.config.json file" /// { /// "build": { diff --git a/core/tauri/src/api/cli.rs b/core/tauri/src/api/cli.rs index a5aff92a17c..8b08260d994 100644 --- a/core/tauri/src/api/cli.rs +++ b/core/tauri/src/api/cli.rs @@ -84,6 +84,9 @@ impl Matches { /// Gets the argument matches of the CLI definition. /// +/// This is a low level API. If the application has been built, +/// prefer [`App::get_cli_matches`](`crate::App#method.get_cli_matches`). +/// /// # Examples /// /// ```rust,no_run diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index cf6a655500a..43204d36d57 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -630,6 +630,26 @@ impl App { .set_activation_policy(activation_policy); } + /// Gets the argument matches of the CLI definition configured in `tauri.conf.json`. + /// + /// # Examples + /// + /// ```rust,no_run + /// tauri::Builder::default() + /// .setup(|app| { + /// let matches = app.get_cli_matches()?; + /// Ok(()) + /// }); + /// ``` + #[cfg(cli)] + pub fn get_cli_matches(&self) -> crate::Result { + if let Some(cli) = &self.manager.config().tauri.cli { + crate::api::cli::get_matches(cli, self.manager.package_info()).map_err(Into::into) + } else { + Ok(Default::default()) + } + } + /// Runs the application. /// /// # Examples diff --git a/core/tauri/src/endpoints/cli.rs b/core/tauri/src/endpoints/cli.rs index ef9f7b44f31..c7c00ce315b 100644 --- a/core/tauri/src/endpoints/cli.rs +++ b/core/tauri/src/endpoints/cli.rs @@ -26,7 +26,7 @@ impl Cmd { .map(Into::into) .map_err(Into::into) } else { - Err(crate::error::into_anyhow("CLI definition not set under tauri.conf.json > tauri > cli (https://tauri.studio/docs/api/config#tauri.cli)")) + Ok(crate::api::cli::Matches::default().into()) } }