From a132ab2a3b0fdc14322749fa9e0ba8cad161e276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 8 Jul 2015 11:09:15 -0700 Subject: [PATCH] Process `--noop` separately from `slurpGlobalFlags()` This relieves `slurpGlobalFlags` of having too many duties --- commands/args.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/commands/args.go b/commands/args.go index 5f82345e..3df3b69d 100644 --- a/commands/args.go +++ b/commands/args.go @@ -168,7 +168,8 @@ func NewArgs(args []string) *Args { globalFlags []string ) - slurpGlobalFlags(&args, &globalFlags, &noop) + slurpGlobalFlags(&args, &globalFlags) + noop = removeValue(&globalFlags, "--noop") if len(args) == 0 { params = []string{} @@ -188,7 +189,7 @@ func NewArgs(args []string) *Args { } } -func slurpGlobalFlags(args *[]string, globalFlags *[]string, noop *bool) { +func slurpGlobalFlags(args *[]string, globalFlags *[]string) { slurpNextValue := false commandIndex := 0 @@ -208,15 +209,8 @@ func slurpGlobalFlags(args *[]string, globalFlags *[]string, noop *bool) { if commandIndex > 0 { aa := *args + *globalFlags = aa[0:commandIndex] *args = aa[commandIndex:] - - for _, arg := range aa[0:commandIndex] { - if arg == "--noop" { - *noop = true - } else { - *globalFlags = append(*globalFlags, arg) - } - } } } @@ -230,3 +224,16 @@ func removeItem(slice []string, index int) (newSlice []string, item string) { return newSlice, item } + +func removeValue(slice *[]string, value string) (found bool) { + var newSlice []string + for _, item := range *slice { + if item == value { + found = true + } else { + newSlice = append(newSlice, item) + } + } + *slice = newSlice + return found +} -- GitLab