From 518e3049481d653a13cbd7dc0c473a709dce69d9 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 3 Mar 2021 23:31:27 -0600 Subject: [PATCH] Add support for nil flag sets --- cmd/command.go | 1 + cmd/help.go | 28 ++++++++++++---------------- cmd/main.go | 10 +++++++--- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cmd/command.go b/cmd/command.go index 82a652c..830715e 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -21,6 +21,7 @@ type Config struct { type CmdArgs struct { Flags Flags + Args []string Client *lifx.Client Config Config Name string diff --git a/cmd/help.go b/cmd/help.go index ee16004..3430d65 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -1,32 +1,24 @@ package lumecmd import ( - "flag" "fmt" "sort" ) func NewCmdHelp() Command { return Command{ - Name: "help", - Func: HelpCmd, - Flags: func() *flag.FlagSet { - fs := flag.NewFlagSet("help", flag.ExitOnError) - - return fs - }(), + Name: "help", + Func: HelpCmd, Use: "", Short: "Show help for a command", } } func HelpCmd(args CmdArgs) (int, error) { - argv := args.Flags.Args() - - if len(argv) == 0 { + if len(args.Args) == 0 { printHelp(commandRegistry) - } else if len(argv) >= 1 { - printCmdHelp(argv[0]) + } else if len(args.Args) >= 1 { + printCmdHelp(args.Args[0]) } return ExitSuccess, nil @@ -63,11 +55,15 @@ func printCmdHelp(name string) error { if subCmd.Use != "" { fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use) - fmt.Println() + } else { + fmt.Printf("usage:\n lume %s\n", subCmd.Name) } - fmt.Print("flags:\n") - subCmd.Flags.PrintDefaults() + if subCmd.Flags != nil { + fmt.Println() + fmt.Print("flags:\n") + subCmd.Flags.PrintDefaults() + } return nil } diff --git a/cmd/main.go b/cmd/main.go index 95b2bbf..32db949 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -74,6 +74,7 @@ func Main(args []string) (int, error) { cmdArgs := CmdArgs{ Client: c, Config: config, + Args: args[2:], } cmd, ok := GetCommand(command) @@ -81,11 +82,14 @@ func Main(args []string) (int, error) { err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command) return ExitFailure, err } - fs := cmd.Flags - fs.Parse(args[2:]) - cmdArgs.Flags = Flags{FlagSet: fs} + fs := cmd.Flags + if fs != nil { + fs.Parse(args[2:]) + cmdArgs.Flags = Flags{FlagSet: fs} + } cmdArgs.Name = command + exitCode, err := cmd.Func(cmdArgs) if err != nil { err = fmt.Errorf("fatal: %s", err)