Files
coconut-leaf/backend/cli/cli.go
T

56 lines
1.9 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-17 21:20:39 +08:00
// Username is the first admin user's name; required together with -init.
2026-07-17 10:53:29 +08:00
Username string
2026-07-17 21:20:39 +08:00
// Password is the first admin user's password; required together with -init.
2026-07-17 10:53:29 +08:00
Password string
}
2026-07-15 11:08:45 +08:00
// Parse parses the command-line arguments via the standard flag package and
2026-07-17 21:20:39 +08:00
// returns a *CLIArgs. When the required -config flag is missing, it prints the
2026-07-15 10:53:24 +08:00
// 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-17 21:20:39 +08:00
// -config maps to the legacy -c/--config (required), -init to -i/--init.
// -username/-password replace the legacy interactive GetUsernamePassword
2026-07-17 10:53:29 +08:00
// prompt and are required together with --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")
2026-07-17 21:20:39 +08:00
flag.StringVar(&args.Username, "username", "", "The first admin user's name (required with -init)")
flag.StringVar(&args.Password, "password", "", "The first admin user's password (required with -init)")
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 == "" {
2026-07-17 21:20:39 +08:00
fmt.Fprintln(os.Stderr, "error: the required flag -config is not provided")
flag.Usage()
os.Exit(2)
}
return args
}