From 05f445ddf2918a92c7ef5c8b3ea3e0d4114423fa Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 11 Apr 2021 02:05:05 +0000 Subject: [PATCH] Make output format flags boolean --- cmd/breathe.go | 5 ++++- cmd/command.go | 21 +++++++++++++++++++-- cmd/ls.go | 5 ++++- cmd/poweroff.go | 5 ++++- cmd/poweron.go | 5 ++++- cmd/setcolor.go | 5 ++++- cmd/setstate.go | 5 ++++- cmd/setwhite.go | 5 ++++- cmd/toggle.go | 5 ++++- 9 files changed, 51 insertions(+), 10 deletions(-) diff --git a/cmd/breathe.go b/cmd/breathe.go index 72e1818..9f4abe8 100644 --- a/cmd/breathe.go +++ b/cmd/breathe.go @@ -43,7 +43,10 @@ func BreatheCmd(ctx Context) (int, error) { c := ctx.Client breathe := lifx.NewBreathe() selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/command.go b/cmd/command.go index 89d0907..a668e67 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -1,6 +1,7 @@ package lumecmd import ( + "errors" "flag" "fmt" "strconv" @@ -100,6 +101,22 @@ func GetCommand(name string) (Command, bool) { 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") + formatTable := fs.Bool("table", false, "Format output as an ASCII table") + fs.BoolVar(formatTable, "t", false, "Format output as an ASCII table") + + fs.Bool("simple", false, "Format output simply") +} + +func getOutputFormatFromFlags(fs Flags) (string, error) { + formatSimple := fs.Bool("simple") + formatTable := fs.Bool("table") + + switch { + case formatSimple && formatTable: + return "", errors.New("only one output format permitted") + case formatTable: + return "table", nil + default: + return "simple", nil + } } diff --git a/cmd/ls.go b/cmd/ls.go index cf2fa3e..4420675 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -26,7 +26,10 @@ func LsCmd(ctx Context) (int, error) { c := ctx.Client selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/poweroff.go b/cmd/poweroff.go index 8d061cf..e4f1c57 100644 --- a/cmd/poweroff.go +++ b/cmd/poweroff.go @@ -32,8 +32,11 @@ func PoweroffCmd(ctx Context) (int, error) { c := ctx.Client duration := ctx.Flags.Float64("duration") selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") state := lifx.State{Power: "off", Duration: duration} + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/poweron.go b/cmd/poweron.go index c70264f..0e5b0cc 100644 --- a/cmd/poweron.go +++ b/cmd/poweron.go @@ -32,8 +32,11 @@ func PoweronCmd(ctx Context) (int, error) { c := ctx.Client duration := ctx.Flags.Float64("duration") selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") state := lifx.State{Power: "on", Duration: duration} + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/setcolor.go b/cmd/setcolor.go index 4cf73fb..5d0c04d 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -54,7 +54,10 @@ func SetColorCmd(ctx Context) (int, error) { c := ctx.Client state := lifx.State{} selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/setstate.go b/cmd/setstate.go index f5ed011..79033fc 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -47,7 +47,10 @@ func SetStateCmd(ctx Context) (int, error) { c := ctx.Client state := lifx.State{} selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/setwhite.go b/cmd/setwhite.go index 57e0d4f..d279976 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -50,7 +50,10 @@ func SetWhiteCmd(ctx Context) (int, error) { c := ctx.Client state := lifx.State{} selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat diff --git a/cmd/toggle.go b/cmd/toggle.go index b72c8cb..6c6e6ac 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -30,7 +30,10 @@ func ToggleCmd(ctx Context) (int, error) { c := ctx.Client duration := ctx.Flags.Float64("duration") selector := ctx.Flags.String("selector") - format := ctx.Flags.String("output-format") + format, err := getOutputFormatFromFlags(ctx.Flags) + if err != nil { + return ExitFailure, err + } if format == "" && ctx.Config.OutputFormat != "" { format = ctx.Config.OutputFormat