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

48 lines
1.4 KiB
Go
Raw Normal View History

2026-07-15 11:08:45 +08:00
package cli
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.
type CLIArgs struct {
2026-07-15 10:53:24 +08:00
// Config is the path to the configuration file (the legacy CONFIG_TOML).
Config string
2026-07-15 10:53:24 +08:00
// Init indicates whether to initialize the calendar system before running.
Init bool
}
2026-07-15 11:08:45 +08:00
// Parse parses the command-line arguments via the standard flag package and
2026-07-15 10:53:24 +08:00
// 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-15 11:08:45 +08:00
func Parse() *CLIArgs {
args := &CLIArgs{}
2026-07-15 10:53:24 +08:00
// 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()
}
2026-07-15 10:53:24 +08:00
// --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()
2026-07-15 10:53:24 +08:00
// 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
}