Add support for parsing global flags

This commit is contained in:
Ryan Cavicchioni 2021-03-28 21:33:43 -05:00
parent 1ef900c44c
commit b1a5844b6b
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

View File

@ -29,6 +29,10 @@ var GitCommit string
func Main(args []string) (int, error) { func Main(args []string) (int, error) {
var config *Config var config *Config
var err error var err error
var i int
flag.Parse()
i = flag.NFlag() + 1
if len(args) == 1 { if len(args) == 1 {
args = append(args, "help") args = append(args, "help")
@ -49,9 +53,8 @@ func Main(args []string) (int, error) {
return ExitFailure, fmt.Errorf("fatal: %s", err) return ExitFailure, fmt.Errorf("fatal: %s", err)
} }
flag.Parse() command := args[i]
i++
command := args[1]
c := lifx.NewClient( c := lifx.NewClient(
config.AccessToken, config.AccessToken,
@ -61,7 +64,7 @@ func Main(args []string) (int, error) {
Context := Context{ Context := Context{
Client: c, Client: c,
Config: *config, Config: *config,
Args: args[2:], Args: args[i:],
} }
cmd, ok := GetCommand(command) cmd, ok := GetCommand(command)
@ -72,7 +75,7 @@ func Main(args []string) (int, error) {
fs := cmd.Flags fs := cmd.Flags
if fs != nil { if fs != nil {
fs.Parse(args[2:]) fs.Parse(args[i:])
Context.Flags = Flags{FlagSet: fs} Context.Flags = Flags{FlagSet: fs}
} }
Context.Name = command Context.Name = command