Skip to content
Tauri 中文网

命令行接口 (CLI)

Tauri 通过 clap 让你的应用拥有 CLI,这是一个强大的命令行参数解析器。在你的 tauri.conf.json 文件中使用简单的 CLI 定义,你可以定义你的界面并在 JavaScript 和/或 Rust 中读取它的参数匹配映射。

🌐 Tauri enables your app to have a CLI through clap, a robust command line argument parser. With a simple CLI definition in your tauri.conf.json file, you can define your interface and read its argument matches map on JavaScript and/or Rust.

🌐 Supported Platforms

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios
  • Windows
    • 由于操作系统的限制,生产应用默认无法将文本写回调用控制台。请查看 tauri#8305 了解解决方法。

🌐 Setup

安装 CLI 插件以开始使用。

🌐 Install the CLI plugin to get started.

使用项目的包管理器添加依赖:

npm run tauri add cli

🌐 Base Configuration

tauri.conf.json 下,你有以下结构来配置接口:

🌐 Under tauri.conf.json, you have the following structure to configure the interface:

src-tauri/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"
}
]
}
}
}
}
}

🌐 Adding Arguments

args 数组表示其命令或子命令接受的参数列表。

🌐 The args array represents the list of arguments accepted by its command or subcommand.

🌐 Positional Arguments

位置参数是通过其在参数列表中的位置来识别的。使用以下配置:

🌐 A positional argument is identified by its position in the list of arguments. With the following configuration:

src-tauri/tauri.conf.json
{
"args": [
{
"name": "source",
"index": 1,
"takesValue": true
},
{
"name": "destination",
"index": 2,
"takesValue": true
}
]
}

用户可以以 ./app tauri.txt dest.txt 运行你的应用,并且参数匹配映射将定义 source"tauri.txt"destination"dest.txt"

🌐 Users can run your app as ./app tauri.txt dest.txt and the arg matches map will define source as "tauri.txt" and destination as "dest.txt".

🌐 Named Arguments

命名参数是一个(键,值)对,其中键用于标识值。使用以下配置:

🌐 A named argument is a (key, value) pair where the key identifies the value. With the following configuration:

tauri-src/tauri.conf.json
{
"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"]

🌐 Users can run your app as ./app --type foo bar, ./app -t foo -t bar or ./app --type=foo,bar and the arg matches map will define type as ["foo", "bar"].

🌐 Flag Arguments

标志参数是一个独立的键,其存在或缺失向你的应用提供信息。使用以下配置:

🌐 A flag argument is a standalone key whose presence or absence provides information to your application. With the following configuration:

tauri-src/tauri.conf.json
{
"args": [
{
"name": "verbose",
"short": "v"
}
]
}

用户可以以 ./app -v -v -v./app --verbose --verbose --verbose./app -vvv 的身份运行你的应用,而参数匹配映射将定义 verbosetrue,并带有 occurrences = 3

🌐 Users can run your app as ./app -v -v -v, ./app --verbose --verbose --verbose or ./app -vvv and the arg matches map will define verbose as true, with occurrences = 3.

🌐 Subcommands

一些 CLI 应用有作为子命令的附加接口。例如,git CLI 有 git branchgit commitgit push。你可以使用 subcommands 数组定义额外的嵌套接口:

🌐 Some CLI applications have additional interfaces as subcommands. For instance, the git CLI has git branch, git commit and git push. You can define additional nested interfaces with the subcommands array:

tauri-src/tauri.conf.json
{
"cli": {
...
"subcommands": {
"branch": {
"args": []
},
"push": {
"args": []
}
}
}
}

它的配置与根应用配置相同,包括 descriptionlongDescriptionargs 等。

🌐 Its configuration is the same as the root application configuration, with the description, longDescription, args, etc.

🌐 Usage

CLI 插件在 JavaScript 和 Rust 中均可用。

🌐 The CLI plugin is available in both JavaScript and 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
}
}

🌐 Permissions

默认情况下,所有潜在危险的插件命令和作用域都会被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用这些功能。

🌐 By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.

有关更多信息,请参见功能概览,有关使用插件权限的分步指南,请参见分步指南

🌐 See the Capabilities Overview for more information and the step by step guide to use plugin permissions.

src-tauri/capabilities/default.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "main-capability",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": ["cli:default"]
}

Default Permission

Allows reading the CLI matches

This default permission set includes the following:

  • allow-cli-matches

Permission Table

Identifier Description

cli:allow-cli-matches

Enables the cli_matches command without any pre-configured scope.

cli:deny-cli-matches

Denies the cli_matches command without any pre-configured scope.


Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站