Refactor global flags

This commit is contained in:
Ryan Cavicchioni 2021-03-30 23:54:05 -05:00
parent 05db35cdfe
commit 9c024454f2
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
9 changed files with 22 additions and 24 deletions

View File

@ -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 <selector>] --color <color> [--from-color <color>] [--period <period>] [--cycles <cycles>] [--persist <persist>] [--power-on] [--peak <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

View File

@ -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")
}

View File

@ -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=<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

View File

@ -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 <selector>] [--duration <sec>]",
@ -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 != "" {

View File

@ -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 <selector>] [--duration <sec>]",
@ -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 != "" {

View File

@ -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 <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--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

View File

@ -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 <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <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

View File

@ -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 <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--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

View File

@ -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 <selector>] [--duration <sec>]",
@ -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