1
0

refactor: use new struct in backend

This commit is contained in:
2026-07-15 11:08:45 +08:00
parent a8de650a5a
commit 1cc6b7b1ba
4 changed files with 15 additions and 12 deletions

47
backend/cli/cli.go Normal file
View File

@@ -0,0 +1,47 @@
package cli
import (
"flag"
"fmt"
"os"
)
// CLIArgs holds the arguments parsed from the command line, mirroring the
// argparse result in the legacy coconut-leaf.py.
type CLIArgs struct {
// Config is the path to the configuration file (the legacy CONFIG_TOML).
Config string
// Init indicates whether to initialize the calendar system before running.
Init bool
}
// Parse 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 Parse() *CLIArgs {
args := &CLIArgs{}
// 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.")
fmt.Fprintln(out)
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
// --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()
// 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()
os.Exit(2)
}
return args
}