tauri_plugin_cli/
lib.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Parse arguments from your Command Line Interface.
6//!
7//! - Supported platforms: Windows, Linux and macOS.
8
9#![doc(
10    html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
11    html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
12)]
13
14use tauri::{
15    plugin::{Builder, PluginApi, TauriPlugin},
16    AppHandle, Manager, Runtime, State,
17};
18
19mod config;
20mod error;
21mod parser;
22
23use config::{Arg, Config};
24
25pub use error::{Error, Result};
26pub use parser::{ArgData, Matches, SubcommandMatches};
27
28pub struct Cli<R: Runtime>(PluginApi<R, Config>);
29
30impl<R: Runtime> Cli<R> {
31    pub fn matches(&self) -> Result<parser::Matches> {
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))
37    }
38}
39
40pub trait CliExt<R: Runtime> {
41    fn cli(&self) -> &Cli<R>;
42}
43
44impl<R: Runtime, T: Manager<R>> CliExt<R> for T {
45    fn cli(&self) -> &Cli<R> {
46        self.state::<Cli<R>>().inner()
47    }
48}
49
50#[tauri::command]
51fn cli_matches<R: Runtime>(_app: AppHandle<R>, cli: State<'_, Cli<R>>) -> Result<parser::Matches> {
52    cli.matches()
53}
54
55pub fn init<R: Runtime>() -> TauriPlugin<R, Config> {
56    Builder::new("cli")
57        .invoke_handler(tauri::generate_handler![cli_matches])
58        .setup(|app, api| {
59            app.manage(Cli(api));
60            Ok(())
61        })
62        .build()
63}