فهرست منبع

feat(core): add `App::get_cli_matches` helper ref #4145

Lucas Nogueira 3 سال پیش
والد
کامیت
617f1144f3

+ 5 - 0
.changes/add-app-get-matches-helper.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Added the `App::get_cli_matches` helper function.

+ 2 - 2
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": {

+ 3 - 0
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

+ 20 - 0
core/tauri/src/app.rs

@@ -630,6 +630,26 @@ impl<R: Runtime> App<R> {
       .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<crate::api::cli::Matches> {
+    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

+ 1 - 1
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())
     }
   }