diff --git a/README.md b/README.md index 1dd45fee..4f70670a 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,30 @@ your-project/ 随后运行 `mxu.exe`(Windows)或 `./mxu`(Linux/macOS)即可!~ +### 命令行参数 + +MXU 支持以下启动参数: + +| 参数 | 功能 | 说明 | +| --- | --- | --- | +| `-h` / `--help` | 显示帮助信息 | 输出 MXU 当前支持的命令行参数说明并退出,不启动图形界面。 | +| `--autostart` | 标记为“开机自启动”启动 | 进入开机自启动模式,并触发自动执行逻辑。该参数主要由 MXU 创建的系统自启动任务自动传入,通常无需手动设置。 | +| `-i <实例名>` / `--instance <实例名>` | 指定要自动启动的实例 | 仅在 `--autostart` 模式下生效。若指定的实例名存在,则优先使用该实例,而不是设置中配置的默认自动执行实例。也支持 `-i=<实例名>`、`--instance=<实例名>` 写法。 | +| `-q` / `--quit-after-run` | 自动执行完成后退出程序 | 当本次启动实际触发了自动执行后,等待任务结束并自动关闭 MXU,适合配合自启动场景做“一次性后台执行”。 | + +示例: + +```bash +# 查看命令行帮助 +mxu.exe --help + +# 使用系统自启动模式,并指定自动执行的实例名 +mxu.exe --autostart --instance "日常任务" + +# 自动执行完成后自动退出 +mxu.exe --autostart -i "日常任务" --quit-after-run +``` + ### 用户文件 用户配置保存在 `config` 文件夹中,调试日志保存在 `debug` 文件夹中。亦可在 设置 - 调试 中直接打开文件夹。 diff --git a/src-tauri/src/commands/system.rs b/src-tauri/src/commands/system.rs index 048e7f25..c8c2eeac 100644 --- a/src-tauri/src/commands/system.rs +++ b/src-tauri/src/commands/system.rs @@ -492,6 +492,49 @@ pub fn is_autostart() -> bool { std::env::args().any(|arg| arg == "--autostart") } +/// 检查命令行是否包含 -h/--help 参数 +pub fn has_help_flag() -> bool { + std::env::args().skip(1).any(|arg| arg == "-h" || arg == "--help") +} + +/// 生成命令行帮助文本 +pub fn get_cli_help_text() -> String { + let exe_name = std::env::current_exe() + .ok() + .and_then(|path| path.file_name().map(|name| name.to_string_lossy().into_owned())) + .filter(|name| !name.is_empty()) + .unwrap_or_else(|| "mxu".to_string()); + + format!( + "\ +MXU 命令行参数 + +用法: + {exe_name} [参数] + +参数: + -h, --help + 显示本帮助并退出 + + --autostart + 以开机自启动模式运行,并触发自动执行逻辑 + 通常由 MXU 创建的系统自启动任务自动传入 + + -i, --instance <实例名> + 指定自动执行时使用的实例名 + 仅在 --autostart 模式下生效 + 也支持 -i=<实例名> 与 --instance=<实例名> 写法 + + -q, --quit-after-run + 当本次启动实际触发自动执行后,在任务完成时自动退出 + +示例: + {exe_name} --autostart --instance \"日常任务\" + {exe_name} --autostart -i \"日常任务\" --quit-after-run +" + ) +} + /// 从命令行参数中获取指定选项的值 /// 支持 `-x value`、`--name value`、`-x=value`、`--name=value` 格式 /// 返回第一个匹配的值;若值缺失或以 `-` 开头则视为无效并跳过 diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index dc1b3ee0..884ea142 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,6 +5,11 @@ mod webview2; fn main() { + if mxu_lib::commands::system::has_help_flag() { + print!("{}", mxu_lib::commands::system::get_cli_help_text()); + return; + } + #[cfg(target_os = "windows")] { // 设置 WebView2 数据目录为程序所在目录下的 webview_data 文件夹