添加一些说明(

This commit is contained in:
shenjack 2024-01-21 22:23:25 +08:00
parent 09273881a6
commit e30a9629da
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 76 additions and 16 deletions

13
run.conf Normal file
View File

@ -0,0 +1,13 @@
# 这是一个范例
# 除了 true 都会被认为是 false
show_console = true
# 就是 chdir (默认 ./lib)
chdir = ../lib
# 运行的可执行文件名称 (默认 ./main)
bin = ./main
# 运行的参数 (默认 "")
arg = aaa

View File

@ -2,21 +2,56 @@ use std::fmt::Display;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
pub const HELP_MESSAGE: &str = r#"call [options] [--] [arguments] pub const HELP_MESSAGE_EN: &str = r#"call [options] [--] [arguments]
Options: Options:
--hide Hide console window (default) --hide Hide console window (default)
--show Show console window --show Show console window
--chdir Change working directory to lib (default)
--chdir=xxx Change working directory to xxx --chdir=xxx Change working directory to xxx
--bin=xxx Specify executable file --bin=xxx Specify executable file
--config=xxx Specify configuration file --config=xxx Specify configuration file
--help Print this help message --help Print this help message(based on system language)
--help-zh Print this help message(but in Chinese)
--help-en Print this help message
Defaults: Defaults:
hide console hide console
chdir ./lib chdir ./lib
run ./main run ./main
"#; "#;
pub const HELP_MESSAGE_ZH: &str = r#"call [选项] [--] [参数]
:
--hide ()
--show
--chdir=xxx xxx
--bin=xxx
--config=xxx
--help 西()
--help-zh 西
--help-en 西()
:
./lib
./main
"#;
pub fn show_help() {
println!("version: {}", crate::VERSION);
match std::env::var("LANG") {
Ok(lang) => {
println!("{}", lang);
if lang.contains("en") {
println!("{}", HELP_MESSAGE_EN);
} else {
println!("{}", HELP_MESSAGE_ZH);
}
}
Err(_) => {
println!("{}", HELP_MESSAGE_ZH);
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct Config { pub struct Config {
pub show_console: bool, pub show_console: bool,
@ -96,17 +131,30 @@ impl Config {
continue; continue;
} }
let mut iter = line.splitn(2, "="); let mut iter = line.splitn(2, "=");
let key = iter.next().unwrap(); let key = iter.next().unwrap_or("").trim();
let value = iter.next().unwrap(); let value = iter.next().unwrap_or("").trim();
if key == "show_console" { match key {
"show_console" => {
show_console = value == "true"; show_console = value == "true";
} else if key == "chdir" { }
"chdir" => {
chdir = Some(value.to_string()); chdir = Some(value.to_string());
} else if key == "bin" { }
"bin" => {
bin = Some(value.to_string()); bin = Some(value.to_string());
} else if key == "arg" { }
"arg" => {
arg = Some(value.to_string()); arg = Some(value.to_string());
} }
"" => continue,
_ => {
// 警告一下
println!("Warning: unknown config key: {}", key);
if !value.is_empty() {
print!("value: {}", value)
}
}
}
} }
// 处理一下 bin // 处理一下 bin
let bin = if let Some(bin) = bin { let bin = if let Some(bin) = bin {
@ -131,8 +179,7 @@ impl Config {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
// 先检查有没有 --help // 先检查有没有 --help
if args.contains(&"--help".to_string()) { if args.contains(&"--help".to_string()) {
println!("v {}", crate::VERSION); show_help();
println!("{}", HELP_MESSAGE);
return None; return None;
} }