refactor: use new struct in backend
This commit is contained in:
47
backend/cli/cli.go
Normal file
47
backend/cli/cli.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user