refactor: migrate utils in backend
This commit is contained in:
@@ -6,20 +6,22 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// CLIArgs 保存从命令行解析得到的参数,对应旧后端 coconut-leaf.py 中 argparse 解析的结果。
|
||||
// CLIArgs holds the arguments parsed from the command line, mirroring the
|
||||
// argparse result in the legacy coconut-leaf.py.
|
||||
type CLIArgs struct {
|
||||
// Config 是 coconut-leaf 的配置文件路径(即旧后端的 CONFIG_TOML)。
|
||||
// Config is the path to the configuration file (the legacy CONFIG_TOML).
|
||||
Config string
|
||||
// Init 指示是否在启动前初始化日历系统。
|
||||
// Init indicates whether to initialize the calendar system before running.
|
||||
Init bool
|
||||
}
|
||||
|
||||
// ParseCLI 使用标准库 flag 包解析命令行参数并返回 *CLIArgs。
|
||||
// 当必需的 --config 参数缺失时,打印用法并以退出码 2 退出(与 argparse 的退出码一致)。
|
||||
// ParseCLI parses the command-line arguments via the standard flag package and
|
||||
// returns a *CLIArgs. When the required --config flag is missing, it prints the
|
||||
// usage and exits with status 2 (matching argparse's exit code).
|
||||
func ParseCLI() *CLIArgs {
|
||||
args := &CLIArgs{}
|
||||
|
||||
// 自定义用法输出,复刻旧后端 ArgumentParser 的 description 横幅。
|
||||
// Custom usage output mirroring the legacy ArgumentParser description banner.
|
||||
flag.Usage = func() {
|
||||
out := flag.CommandLine.Output()
|
||||
fmt.Fprintln(out, "The server of light, self-host and multi-account calendar system.")
|
||||
@@ -28,13 +30,13 @@ func ParseCLI() *CLIArgs {
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
// --config 对应旧后端的 -c/--config(必需),--init 对应旧后端的 -i/--init。
|
||||
// --config maps to the legacy -c/--config (required), --init to -i/--init.
|
||||
flag.StringVar(&args.Config, "config", "", "The configuration file `CONFIG_TOML` for coconut-leaf")
|
||||
flag.BoolVar(&args.Init, "init", false, "Set for initialize the calendar system")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// 标准库 flag 不支持 required,故解析后手动校验。
|
||||
// The standard flag package has no built-in "required", so validate manually.
|
||||
if args.Config == "" {
|
||||
fmt.Fprintln(os.Stderr, "error: the required flag --config is not provided")
|
||||
flag.Usage()
|
||||
|
||||
Reference in New Issue
Block a user