跳转到内容
Tauri

命令行接口 (CLI)

Tauri 使您的应用程序能够通过 clap(一个强大的命令行参数解析器)拥有 CLI。通过 tauri.conf.json 文件中的简单 CLI 定义,您可以定义接口并读取其参数匹配 JavaScript/Rust 上的映射。

支持的平台

  • Windows
  • Linux
  • macOS

设置

这个插件要求 Rust 版本至少是 1.75

请安装 CLI 插件。

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

npm run tauri add cli

基本配置

tauri.conf.json 中,你可以使用以下结构来配置接口:

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"
}
]
}
}
}
}
}

添加参数

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

位置参数

位置参数由其在参数列表中的位置标识。通过以下配置:

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"

命名参数

命名参数是一个 (key, value) 对,键标识值。通过以下配置:

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"]

标志参数

标志参数是一个独立的键,它的存在或不存在为应用程序提供信息。通过以下配置:

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

用户可以以 ./app -v -v -v./app --verbose --verbose --verbose./app -vvv 的形式运行你的应用程序, 匹配的参数映射将会将 verbose 定义为 trueoccurrences = 3

子命令

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

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

它的配置和根应用的配置一样,都有 descriptionlongDescriptionargs 等。

用法

CLI 有 JavaScript 和 Rust 两种版本。

import { getMatches } from '@tauri-apps/plugin-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
}
}

权限

默认情况下,所有插件命令都被阻止,无法访问。 你必须在你的 capabilities 配置中定义一个权限列表。

更多信息请参见访问控制列表

src-tauri/capabilities/main.json
{
"$schema": "./schemas/desktop-schema.json",
"identifier": "main-capability",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": ["cli:default"]
}
权限描述
cli:default允许读取 CLI 匹配项。
cli:allow-cli-matches在没有预先配置作用域的情况下,启用 cli_matches 命令。
cli:deny-cli-matches拒绝没有预先配置作用域的 cli_matches 命令。

© 2024 Tauri Contributors. CC-BY / MIT