r1 把 `rename` 套在**插值后的整条文本**上,于是用户数据被改坏:账户名恰好是 `ull` 会变成
`unlucky-launcher`,路径 `D:/tmp/ull/.ULL/...` 会变成 `D:/tmp/unlucky-launcher/.ULL/...`,
回显的子命令 `ull` 也会被改名。
改成模板占位符:文案里写 `%prog%`,**只在渲染时**(print_report / print_error / 帮助)替换成
argv[0] 的 basename。占位符只存在于静态模板里,插值进来的账户名 / 路径 / 子命令碰不到它。
用 `%prog%` 而不是 `{prog}`:这些模板要活在 `format!` 里,`{prog}` 会被 format! 当成参数名
编译失败。
同时补上 r1 漏的反方向:**stdout 的正常输出**(Report.human 与 warnings)现在也做替换,
以前只有 stderr 的错误做了,两边不一致。
回归测试:`user_data_that_says_ull_is_never_rewritten` 断言「账户名 `ull`」「路径 `\ull\`」
「未知命令 `ull`」在 fill 之后逐字不变。
占位符替换覆盖 21 个文件的 63 处文案;测试夹具里形如 `ull-mcp-diagnose-<pid>` 的临时目录名
已回滚为字面量(那不是文案)。
69 lines
2.2 KiB
Rust
69 lines
2.2 KiB
Rust
//! 元命令:`--version` / 无子命令 / `help`。
|
||
|
||
use serde_json::json;
|
||
|
||
use crate::cli::{help, program};
|
||
use crate::os;
|
||
use crate::output::Report;
|
||
|
||
/// 启动器名(`${launcher_name}` 与 `--version` 输出共用)。
|
||
pub const LAUNCHER_NAME: &str = "UnluckyLauncher";
|
||
|
||
/// 版本 + 系统摘要(`ull --version`,D19 §5-2 要求附带 os 摘要)。
|
||
///
|
||
/// 探测走 `core::platform::os_info::current()`(进程内缓存),未达基线时附带升级指引。
|
||
/// 自称的名字取 argv[0](M82):GUI 二进制调进来时是 `unlucky-launcher`,`ull` 自己跑时仍是 `ull`。
|
||
#[must_use]
|
||
pub fn version() -> Report {
|
||
let info = os::detect();
|
||
let invoked_as = program::name();
|
||
let data = json!({
|
||
"name": invoked_as,
|
||
"package": env!("CARGO_PKG_NAME"),
|
||
"version": env!("CARGO_PKG_VERSION"),
|
||
"os": os::to_json(&info),
|
||
});
|
||
let mut human = vec![
|
||
format!(
|
||
"{invoked_as} {}({})",
|
||
env!("CARGO_PKG_VERSION"),
|
||
env!("CARGO_PKG_NAME")
|
||
),
|
||
format!("os: {}", os::summary_line(&info)),
|
||
];
|
||
human.extend(os::notes(&info));
|
||
Report::new(data, human)
|
||
}
|
||
|
||
/// 无子命令:版本摘要 + 帮助(用户原话「无子命令时 --version + --help」)。
|
||
#[must_use]
|
||
pub fn welcome() -> Report {
|
||
let version_report = version();
|
||
let mut human = version_report.human;
|
||
let text = general_help();
|
||
human.push(String::new());
|
||
human.extend(text.lines().map(str::to_string));
|
||
let mut data = version_report.data;
|
||
data["help"] = json!(text);
|
||
Report::new(data, human)
|
||
}
|
||
|
||
/// 帮助(`ull help <命令>` 给该命令,未知主题退回总帮助)。
|
||
#[must_use]
|
||
pub fn help(topic: Option<&str>) -> Report {
|
||
let text = match topic.and_then(help::topic) {
|
||
Some(text) => program::fill(text),
|
||
None => general_help(),
|
||
};
|
||
let data = json!({
|
||
"topic": topic,
|
||
"text": text,
|
||
});
|
||
Report::new(data, text.lines().map(str::to_string).collect())
|
||
}
|
||
|
||
/// 总帮助,按被调用的二进制名自述(`ull` 自己跑时逐字不变)。
|
||
fn general_help() -> String {
|
||
program::fill(help::general())
|
||
}
|