mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-21 00:29:03 +01:00
refactor: improved init flow in main
This commit is contained in:
29
pkg/args.go
Normal file
29
pkg/args.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
)
|
||||
|
||||
type (
|
||||
Args struct {
|
||||
Command string
|
||||
Args []string
|
||||
}
|
||||
CommandValidator interface {
|
||||
IsCommandValid(cmd string) bool
|
||||
}
|
||||
)
|
||||
|
||||
func GetArgs(validator CommandValidator) Args {
|
||||
var args Args
|
||||
flag.Parse()
|
||||
args.Command = flag.Arg(0)
|
||||
if !validator.IsCommandValid(args.Command) {
|
||||
log.Fatalf("invalid command: %s", args.Command)
|
||||
}
|
||||
if len(flag.Args()) > 1 {
|
||||
args.Args = flag.Args()[1:]
|
||||
}
|
||||
return args
|
||||
}
|
||||
Reference in New Issue
Block a user