命令行界面 (CLI)
Tauri 使您的应用能够通过 clap 拥有 CLI,这是一个强大的命令行参数解析器。通过在您的 tauri.conf.json
文件中定义一个简单的 CLI 定义,您可以定义您的接口,并在 JavaScript 和/或 Rust 上读取其参数匹配映射。
支持的平台
此插件需要至少 1.77.2 的 Rust 版本
平台 | 级别 | 注释 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
- Windows
- 由于操作系统限制,生产应用默认情况下无法将文本写回调用控制台。请查看 tauri#8305 以获取解决方法。
设置
安装 CLI 插件即可开始使用。
使用您项目的包管理器添加依赖项
npm run tauri add cli
yarn run tauri add cli
pnpm tauri add cli
deno task tauri add cli
bun tauri add cli
cargo tauri add cli
-
在
src-tauri
文件夹中运行以下命令,将插件添加到Cargo.toml
项目的依赖项中cargo add tauri-plugin-cli --target 'cfg(any(target_os = "macos", windows, target_os = "linux"))'- 修改
lib.rs
以初始化插件
src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(desktop)]app.handle().plugin(tauri_plugin_cli::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");}- 使用您首选的 JavaScript 包管理器安装 JavaScript Guest 绑定
npm install @tauri-apps/plugin-cliyarn add @tauri-apps/plugin-clipnpm add @tauri-apps/plugin-clideno add npm:@tauri-apps/plugin-clibun add @tauri-apps/plugin-cli - 修改
基本配置
在 tauri.conf.json
下,您有以下结构来配置接口
{ "plugins": { "cli": { "description": "Tauri CLI Plugin Example", "args": [ { "short": "v", "name": "verbose", "description": "Verbosity level" } ], "subcommands": { "run": { "description": "Run the application", "args": [ { "name": "debug", "description": "Run application in debug mode" }, { "name": "release", "description": "Run application in release mode" } ] } } } }}
添加参数
args
数组表示其命令或子命令接受的参数列表。
位置参数
位置参数由其在参数列表中的位置标识。使用以下配置
{ "args": [ { "name": "source", "index": 1, "takesValue": true }, { "name": "destination", "index": 2, "takesValue": true } ]}
用户可以将您的应用作为 ./app tauri.txt dest.txt
运行,参数匹配映射将 source
定义为 "tauri.txt"
,并将 destination
定义为 "dest.txt"
。
命名参数
命名参数是(键,值)对,其中键标识值。使用以下配置
{ "args": [ { "name": "type", "short": "t", "takesValue": true, "multiple": true, "possibleValues": ["foo", "bar"] } ]}
用户可以将您的应用作为 ./app --type foo bar
、./app -t foo -t bar
或 ./app --type=foo,bar
运行,参数匹配映射将 type
定义为 ["foo", "bar"]
。
标志参数
标志参数是一个独立的键,其存在与否为您的应用程序提供信息。使用以下配置
{ "args": [ { "name": "verbose", "short": "v" } ]}
用户可以将您的应用作为 ./app -v -v -v
、./app --verbose --verbose --verbose
或 ./app -vvv
运行,参数匹配映射将 verbose
定义为 true
,且 occurrences = 3
。
子命令
某些 CLI 应用程序具有作为子命令的附加接口。例如,git
CLI 具有 git branch
、git commit
和 git push
。您可以使用 subcommands
数组定义其他嵌套接口
{ "cli": { ... "subcommands": { "branch": { "args": [] }, "push": { "args": [] } } }}
其配置与根应用程序配置相同,包括 description
、longDescription
、args
等。
用法
CLI 插件在 JavaScript 和 Rust 中均可用。
import { getMatches } from '@tauri-apps/plugin-cli';// when using `"withGlobalTauri": true`, you may use// const { getMatches } = window.__TAURI__.cli;
const matches = await getMatches();if (matches.subcommand?.name === 'run') { // `./your-app run $ARGS` was executed const args = matches.subcommand.matches.args; if (args.debug?.value === true) { // `./your-app run --debug` was executed } if (args.release?.value === true) { // `./your-app run --release` was executed }}
use tauri_plugin_cli::CliExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_cli::init()) .setup(|app| { match app.cli().matches() { // `matches` here is a Struct with { args, subcommand }. // `args` is `HashMap<String, ArgData>` where `ArgData` is a struct with { value, occurrences }. // `subcommand` is `Option<Box<SubcommandMatches>>` where `SubcommandMatches` is a struct with { name, matches }. Ok(matches) => { println!("{:?}", matches) } Err(_) => {} } Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
权限
默认情况下,所有潜在危险的插件命令和作用域都被阻止,无法访问。您必须修改 capabilities
配置中的权限才能启用这些。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": ["cli:default"]}
默认权限
允许读取 CLI 匹配项
此默认权限集包括以下内容
allow-cli-matches
权限表
标识符 | 描述 |
---|---|
|
启用 cli_matches 命令,无需任何预配置的作用域。 |
|
拒绝 cli_matches 命令,无需任何预配置的作用域。 |
© 2025 Tauri 贡献者。CC-BY / MIT