コンテンツにスキップ
Tauri

CLI(コマンドライン・インターフェイス)

《訳注》

Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。

Tauriは、堅牢なコマンドライン引数パーサー(構文解析ツール)である clap(英語版)を用いて、あなたのアプリに CLI 機能を追加できます。tauri.conf.json ファイルに簡単な CLI 定義を記述するだけで、インターフェースを定義し、JavaScript や Rust 上の引数照合マップを読み取ることが可能になります。

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios
  • Windows
    • OSの制限により、本番環境アプリでは、デフォルトでは呼び出しコンソールにテキストを書き戻すことができません。この回避策については、tauri#8305 をご確認ください。

はじめに、「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" として定義します。

「名前付き引数」は[キーと値]のペアで、キーが値を表します。以下の設定では:

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 として実行でき、引数照合マップには verbosetrueoccurrences = 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';
// `"withGlobalTauri": true` を使用する場合は、
// const { getMatches } = window.__TAURI__.cli; を使用できます;
const matches = await getMatches();
if (matches.subcommand?.name === 'run') {
// `./your-app run $ARGS` が実行されました
const args = matches.subcommand.matches.args;
if (args.debug?.value === true) {
// `./your-app run --debug` が実行されました
}
if (args.release?.value === true) {
// `./your-app run --release` が実行されました
}
}

デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。

詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。

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.

【※ この日本語版は、「Feb 22, 2025 英語版」に基づいています】


© 2025 Tauri Contributors. CC-BY / MIT