1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

//! [![](https://github.com/tauri-apps/plugins-workspace/raw/v2/plugins/cli/banner.png)](https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/cli)
//!
//! Parse arguments from your Command Line Interface.
//!
//! - Supported platforms: Windows, Linux and macOS.

#![doc(
    html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
    html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
)]

use tauri::{
    plugin::{Builder, PluginApi, TauriPlugin},
    AppHandle, Manager, Runtime, State,
};

mod config;
mod error;
mod parser;

use config::{Arg, Config};
pub use error::Error;
type Result<T> = std::result::Result<T, Error>;

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 trait CliExt<R: Runtime> {
    fn cli(&self) -> &Cli<R>;
}

impl<R: Runtime, T: Manager<R>> CliExt<R> for T {
    fn cli(&self) -> &Cli<R> {
        self.state::<Cli<R>>().inner()
    }
}

#[tauri::command]
fn cli_matches<R: Runtime>(_app: AppHandle<R>, cli: State<'_, Cli<R>>) -> Result<parser::Matches> {
    cli.matches()
}

pub fn init<R: Runtime>() -> TauriPlugin<R, Config> {
    Builder::new("cli")
        .invoke_handler(tauri::generate_handler![cli_matches])
        .setup(|app, api| {
            app.manage(Cli(api));
            Ok(())
        })
        .build()
}