2026-07-14 21:04:26 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-15 10:53:24 +08:00
|
|
|
// CLIArgs holds the arguments parsed from the command line, mirroring the
|
|
|
|
|
// argparse result in the legacy coconut-leaf.py.
|
2026-07-14 21:04:26 +08:00
|
|
|
type CLIArgs struct {
|
2026-07-15 10:53:24 +08:00
|
|
|
// Config is the path to the configuration file (the legacy CONFIG_TOML).
|
2026-07-14 21:04:26 +08:00
|
|
|
Config string
|
2026-07-15 10:53:24 +08:00
|
|
|
// Init indicates whether to initialize the calendar system before running.
|
2026-07-14 21:04:26 +08:00
|
|
|
Init bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 10:53:24 +08:00
|
|
|
// 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).
|
2026-07-14 21:04:26 +08:00
|
|
|
func ParseCLI() *CLIArgs {
|
|
|
|
|
args := &CLIArgs{}
|
|
|
|
|
|
2026-07-15 10:53:24 +08:00
|
|
|
// Custom usage output mirroring the legacy ArgumentParser description banner.
|
2026-07-14 21:04:26 +08:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 10:53:24 +08:00
|
|
|
// --config maps to the legacy -c/--config (required), --init to -i/--init.
|
2026-07-14 21:04:26 +08:00
|
|
|
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()
|
|
|
|
|
|
2026-07-15 10:53:24 +08:00
|
|
|
// The standard flag package has no built-in "required", so validate manually.
|
2026-07-14 21:04:26 +08:00
|
|
|
if args.Config == "" {
|
|
|
|
|
fmt.Fprintln(os.Stderr, "error: the required flag --config is not provided")
|
|
|
|
|
flag.Usage()
|
|
|
|
|
os.Exit(2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args
|
|
|
|
|
}
|