flags rewrite : just well defined structs and a clear switch block
This commit is contained in:
parent
123eabca54
commit
f23a98fb5d
3 changed files with 63 additions and 76 deletions
|
|
@ -5,20 +5,34 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type Flag string
|
||||
|
||||
func (f Flag) Dash() string {
|
||||
return fmt.Sprintf("--%s", f)
|
||||
type Arg struct {
|
||||
Val string
|
||||
Help string
|
||||
}
|
||||
|
||||
// prepend --flag to args if a flag isn't there:
|
||||
// allows trailing flags:
|
||||
// um tag foo --invert
|
||||
// becomes:
|
||||
// um tag --query foo --invert
|
||||
func PrependFlagToArgs(args []string, flag Flag) []string {
|
||||
if len(args) >= 1 && !strings.HasPrefix(args[0], "-") {
|
||||
args = append([]string{flag.Dash()}, args...)
|
||||
}
|
||||
return args
|
||||
type Flag struct {
|
||||
Long string
|
||||
Short string
|
||||
Val string
|
||||
Help string
|
||||
}
|
||||
|
||||
// there's got to be a way to generalize the val field to string|bool
|
||||
type FlagBool struct {
|
||||
Long string
|
||||
Short string
|
||||
Val bool
|
||||
Help string
|
||||
}
|
||||
|
||||
func HasDashPrefix(s string) bool {
|
||||
return strings.HasPrefix(s, "-")
|
||||
}
|
||||
|
||||
func MissingValueArg(args []string, i int) bool {
|
||||
return i+1 == len(args) || HasDashPrefix(args[i+1])
|
||||
}
|
||||
|
||||
func HelpMissingVal(flag string) {
|
||||
fmt.Printf("%s needs a value assignment\n", flag)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue