github.com/grantbow/bug@v0.3.1/bugapp/utils.go (about) 1 package bugapp 2 3 import ( 4 "os" 5 ) 6 7 func getEditor() string { 8 editor := os.Getenv("EDITOR") 9 10 if editor != "" { 11 return editor 12 } 13 return "vim" 14 15 } 16 17 type ArgumentList []string 18 19 func (args ArgumentList) HasArgument(arg string) bool { 20 for _, argCandidate := range args { 21 if arg == argCandidate { 22 return true 23 } 24 } 25 return false 26 } 27 28 func (args ArgumentList) GetArgument(argname, defaultVal string) string { 29 for idx, argCandidate := range args { 30 if argname == argCandidate { 31 // If it's the last argument, then return string 32 // "true" because we can't return idx+1, but it 33 // shouldn't be the default value when the argument 34 // isn't provided either.. 35 if idx >= len(args)-1 { 36 return "true" 37 } 38 return args[idx+1] 39 } 40 } 41 return defaultVal 42 } 43 44 func (args ArgumentList) GetAndRemoveArguments(argnames []string) (ArgumentList, []string) { 45 var nextArgumentType int = -1 46 matches := make([]string, len(argnames)) 47 var retArgs []string 48 for idx, argCandidate := range args { 49 // The last token was in argnames, so this one 50 // is the value. Set it in matches and reset 51 // nextArgumentType and continue to the next 52 // possible token 53 if nextArgumentType != -1 { 54 matches[nextArgumentType] = argCandidate 55 nextArgumentType = -1 56 continue 57 } 58 59 // Check if this is a argname we're looking for 60 for argidx, argname := range argnames { 61 if argname == argCandidate { 62 if idx >= len(args)-1 { 63 matches[argidx] = "true" 64 } 65 nextArgumentType = argidx 66 break 67 } 68 } 69 70 // It wasn't an argname, so add it to the return 71 if nextArgumentType == -1 { 72 retArgs = append(retArgs, argCandidate) 73 } 74 } 75 return retArgs, matches 76 }