From 9c024454f2c6bc1bf767a72a62baa5521e1c209c Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Tue, 30 Mar 2021 23:54:05 -0500 Subject: [PATCH] Refactor global flags --- cmd/breathe.go | 4 +--- cmd/command.go | 14 ++++++++++++++ cmd/ls.go | 4 +--- cmd/poweroff.go | 4 +--- cmd/poweron.go | 4 +--- cmd/setcolor.go | 4 +--- cmd/setstate.go | 4 +--- cmd/setwhite.go | 4 +--- cmd/toggle.go | 4 +--- 9 files changed, 22 insertions(+), 24 deletions(-) diff --git a/cmd/breathe.go b/cmd/breathe.go index 5c7b1a7..72e1818 100644 --- a/cmd/breathe.go +++ b/cmd/breathe.go @@ -30,8 +30,6 @@ func NewCmdBreathe() Command { fs.Float64("peak", lifx.DefaultBreathePeak, "Defines where in a period the target color is at its maximum (min: 0.0, max: 1.0)") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector ] --color [--from-color ] [--period ] [--cycles ] [--persist ] [--power-on] [--peak ]", @@ -45,7 +43,7 @@ func BreatheCmd(ctx Context) (int, error) { c := ctx.Client breathe := lifx.NewBreathe() selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/command.go b/cmd/command.go index 2a14f2b..89d0907 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -81,6 +81,13 @@ func RegisterCommand(cmd Command) error { if _, ok := commandRegistry[cmd.Name]; ok { return fmt.Errorf("%s command is already registered") } + + if cmd.Flags == nil { + cmd.Flags = flag.NewFlagSet(cmd.Name, flag.ExitOnError) + } + + mergeGlobalFlags(cmd.Flags) + commandRegistry[cmd.Name] = cmd return nil } @@ -89,3 +96,10 @@ func GetCommand(name string) (Command, bool) { cmd, ok := commandRegistry[name] return cmd, ok } + +func mergeGlobalFlags(fs *flag.FlagSet) { + fs.Bool("debug", false, "Enable debug mode") + + outputFormat := fs.String("output-format", defaultOutputFormat, "Set the output format") + fs.StringVar(outputFormat, "o", defaultOutputFormat, "Set the output format") +} diff --git a/cmd/ls.go b/cmd/ls.go index 3734d03..25061c1 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -14,8 +14,6 @@ func NewCmdLs() Command { selector := fs.String("selector", defaultSelector, "Set the selector") fs.StringVar(selector, "s", defaultSelector, "Set the selector") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector=]", @@ -28,7 +26,7 @@ func LsCmd(ctx Context) (int, error) { c := ctx.Client selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/poweroff.go b/cmd/poweroff.go index f8e2b21..8d061cf 100644 --- a/cmd/poweroff.go +++ b/cmd/poweroff.go @@ -19,8 +19,6 @@ func NewCmdPoweroff() Command { selector := fs.String("selector", defaultSelector, "Set the selector") fs.StringVar(selector, "s", defaultSelector, "Set the selector") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector ] [--duration ]", @@ -34,7 +32,7 @@ func PoweroffCmd(ctx Context) (int, error) { c := ctx.Client duration := ctx.Flags.Float64("duration") selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") state := lifx.State{Power: "off", Duration: duration} if format == "" && ctx.Config.OutputFormat != "" { diff --git a/cmd/poweron.go b/cmd/poweron.go index af94a02..c70264f 100644 --- a/cmd/poweron.go +++ b/cmd/poweron.go @@ -19,8 +19,6 @@ func NewCmdPoweron() Command { selector := fs.String("selector", defaultSelector, "Set the selector") fs.StringVar(selector, "s", defaultSelector, "Set the selector") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector ] [--duration ]", @@ -34,7 +32,7 @@ func PoweronCmd(ctx Context) (int, error) { c := ctx.Client duration := ctx.Flags.Float64("duration") selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") state := lifx.State{Power: "on", Duration: duration} if format == "" && ctx.Config.OutputFormat != "" { diff --git a/cmd/setcolor.go b/cmd/setcolor.go index dacec05..4cf73fb 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -41,8 +41,6 @@ func NewCmdSetColor() Command { fast := fs.Bool("fast", defaultFast, "fast state") fs.BoolVar(fast, "f", defaultFast, "fast state") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector ] [--power (on|off)] [--hue ] [--saturation ] [--rgb ] [--name ] [--brightness ] [--duration ] [--fast]", @@ -56,7 +54,7 @@ func SetColorCmd(ctx Context) (int, error) { c := ctx.Client state := lifx.State{} selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/setstate.go b/cmd/setstate.go index b3a6583..f5ed011 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -34,8 +34,6 @@ func NewCmdSetState() Command { fast := fs.Bool("fast", defaultFast, "fast state") fs.BoolVar(fast, "f", defaultFast, "fast state") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector ] [--power (on|off)] [--color ] [--brightness ] [--duration ] [--infrared ] [--fast]", @@ -49,7 +47,7 @@ func SetStateCmd(ctx Context) (int, error) { c := ctx.Client state := lifx.State{} selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/setwhite.go b/cmd/setwhite.go index 86a14b7..57e0d4f 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -37,8 +37,6 @@ func NewCmdSetWhite() Command { fast := fs.Bool("fast", defaultFast, "fast state") fs.BoolVar(fast, "f", defaultFast, "fast state") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector ] [--power (on|off)] [--kelvin ] [--name ] [--brightness ] [--duration ] [--infrared] [--fast]", @@ -52,7 +50,7 @@ func SetWhiteCmd(ctx Context) (int, error) { c := ctx.Client state := lifx.State{} selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/toggle.go b/cmd/toggle.go index 4da47a6..b72c8cb 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -17,8 +17,6 @@ func NewCmdToggle() Command { selector := fs.String("selector", defaultSelector, "Set the selector") fs.StringVar(selector, "s", defaultSelector, "Set the selector") - fs.String("format", defaultOutputFormat, "Set the output format") - return fs }(), Use: "[--selector ] [--duration ]", @@ -32,7 +30,7 @@ func ToggleCmd(ctx Context) (int, error) { c := ctx.Client duration := ctx.Flags.Float64("duration") selector := ctx.Flags.String("selector") - format := ctx.Flags.String("format") + format := ctx.Flags.String("output-format") if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat