mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-22 21:30:44 +02:00
chore: update documentation
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
|
||||
/**
|
||||
* The resolution of a single CLI argument match.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
interface ArgMatch {
|
||||
@@ -27,18 +29,26 @@ interface ArgMatch {
|
||||
}
|
||||
|
||||
/**
|
||||
* The result of matching a subcommand of the CLI definition.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
interface SubcommandMatch {
|
||||
/** The name of the matched subcommand. */
|
||||
name: string
|
||||
/** The argument matches of the subcommand, resolved the same way as the parent command's matches. */
|
||||
matches: CliMatches
|
||||
}
|
||||
|
||||
/**
|
||||
* The resolved matches of the CLI arguments and, if any, its matched subcommand.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
interface CliMatches {
|
||||
/** The matched arguments, keyed by argument name. */
|
||||
args: Record<string, ArgMatch>
|
||||
/** The matched subcommand, or `null` if no subcommand was invoked. */
|
||||
subcommand: SubcommandMatch | null
|
||||
}
|
||||
|
||||
@@ -61,6 +71,7 @@ interface CliMatches {
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @returns A promise resolving to the parsed CLI matches.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
async function getMatches(): Promise<CliMatches> {
|
||||
|
||||
@@ -4,8 +4,11 @@
|
||||
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
/// Errors that can be returned from the CLI plugin's commands.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// Parsing the process arguments against the CLI definition in `tauri.conf.json` failed,
|
||||
/// e.g. because a required argument is missing or an unknown flag was passed.
|
||||
#[error("failed to parse arguments: {0}")]
|
||||
ParseCli(#[from] clap::Error),
|
||||
}
|
||||
@@ -19,4 +22,5 @@ impl Serialize for Error {
|
||||
}
|
||||
}
|
||||
|
||||
/// Alias for a [`std::result::Result`] with the error type [`Error`].
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
@@ -25,19 +25,27 @@ use config::{Arg, Config};
|
||||
pub use error::{Error, Result};
|
||||
pub use parser::{ArgData, Matches, SubcommandMatches};
|
||||
|
||||
/// Access to the CLI APIs, managed by the app once the plugin is initialized.
|
||||
pub struct Cli<R: Runtime>(PluginApi<R, Config>);
|
||||
|
||||
impl<R: Runtime> Cli<R> {
|
||||
/// Parses the arguments the current process was started with against the CLI definition
|
||||
/// configured under `plugins.cli` in `tauri.conf.json` and returns the resolved
|
||||
/// [`parser::Matches`]. Errors if the arguments do not satisfy the CLI definition.
|
||||
pub fn matches(&self) -> Result<parser::Matches> {
|
||||
parser::get_matches(self.0.config(), self.0.app().package_info(), None)
|
||||
}
|
||||
|
||||
/// Same as [`Self::matches`], but parses the given `args` instead of the current process'
|
||||
/// arguments. Errors if `args` does not satisfy the CLI definition.
|
||||
pub fn matches_from(&self, args: Vec<String>) -> Result<parser::Matches> {
|
||||
parser::get_matches(self.0.config(), self.0.app().package_info(), Some(args))
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait to access the CLI APIs.
|
||||
pub trait CliExt<R: Runtime> {
|
||||
/// Returns the [`Cli`] instance managed by the app.
|
||||
fn cli(&self) -> &Cli<R>;
|
||||
}
|
||||
|
||||
@@ -52,6 +60,8 @@ fn cli_matches<R: Runtime>(_app: AppHandle<R>, cli: State<'_, Cli<R>>) -> Result
|
||||
cli.matches()
|
||||
}
|
||||
|
||||
/// Initializes the plugin, reading the CLI definition from the `plugins.cli` object in
|
||||
/// `tauri.conf.json`.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R, Config> {
|
||||
Builder::new("cli")
|
||||
.invoke_handler(tauri::generate_handler![cli_matches])
|
||||
|
||||
Reference in New Issue
Block a user