1
0
Files
coconut-leaf/backend/cli.go

46 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"flag"
"fmt"
"os"
)
// CLIArgs 保存从命令行解析得到的参数,对应旧后端 coconut-leaf.py 中 argparse 解析的结果。
type CLIArgs struct {
// Config 是 coconut-leaf 的配置文件路径(即旧后端的 CONFIG_TOML
Config string
// Init 指示是否在启动前初始化日历系统。
Init bool
}
// ParseCLI 使用标准库 flag 包解析命令行参数并返回 *CLIArgs。
// 当必需的 --config 参数缺失时,打印用法并以退出码 2 退出(与 argparse 的退出码一致)。
func ParseCLI() *CLIArgs {
args := &CLIArgs{}
// 自定义用法输出,复刻旧后端 ArgumentParser 的 description 横幅。
flag.Usage = func() {
out := flag.CommandLine.Output()
fmt.Fprintln(out, "The server of light, self-host and multi-account calendar system.")
fmt.Fprintln(out)
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
// --config 对应旧后端的 -c/--config必需--init 对应旧后端的 -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故解析后手动校验。
if args.Config == "" {
fmt.Fprintln(os.Stderr, "error: the required flag --config is not provided")
flag.Usage()
os.Exit(2)
}
return args
}