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 cliyarn run tauri add clipnpm tauri add clideno task tauri add clibun tauri add clicargo 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';// `"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` が実行されました }}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` は { args, subcommand } を持つ構造体です。 // 構造体のメンバー `args` は `HashMap<String, ArgData>` であり、`ArgData` は { value, occurrences } を持つ構造体です。 // もう一方の構造体メンバー `subcommand` は `Option<Box<SubcommandMatches>>` であり、`SubcommandMatches` は { name, matches } を持つ構造体です。 Ok(matches) => { println!("{:?}", matches) } Err(_) => {} } Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。
詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。
{ "$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 |
|---|---|
|
|
Enables the cli_matches command without any pre-configured scope. |
|
|
Denies the cli_matches command without any pre-configured scope. |
【※ この日本語版は、「Feb 22, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT