| 1 |
package flags |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
const ( |
| 10 |
defaultShortOptDelimiter = '/' |
| 11 |
defaultLongOptDelimiter = "/" |
| 12 |
defaultNameArgDelimiter = ':' |
| 13 |
) |
| 14 |
|
| 15 |
func argumentIsOption(arg string) bool { |
| 16 |
|
| 17 |
|
| 18 |
return len(arg) > 0 && (arg[0] == '-' || arg[0] == '/') |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func stripOptionPrefix(optname string) (string, bool) { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
if strings.HasPrefix(optname, "--") { |
| 29 |
return optname[2:], true |
| 30 |
} else if strings.HasPrefix(optname, "-") { |
| 31 |
return optname[1:], false |
| 32 |
} else if strings.HasPrefix(optname, "/") { |
| 33 |
optname = optname[1:] |
| 34 |
if len(optname) > 1 { |
| 35 |
return optname, true |
| 36 |
} |
| 37 |
return optname, false |
| 38 |
} |
| 39 |
|
| 40 |
return optname, false |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func splitOption(option string) (string, *string) { |
| 46 |
if len(option) == 0 { |
| 47 |
return option, nil |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
var pos int |
| 55 |
if option[0] == '/' { |
| 56 |
pos = strings.Index(option, ":") |
| 57 |
} else if option[0] == '-' { |
| 58 |
pos = strings.Index(option, "=") |
| 59 |
} |
| 60 |
|
| 61 |
if pos >= 0 { |
| 62 |
rest := option[pos+1:] |
| 63 |
return option[:pos], &rest |
| 64 |
} |
| 65 |
|
| 66 |
return option, nil |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func newHelpGroup(showHelp func() error) *Group { |
| 71 |
|
| 72 |
|
| 73 |
var help struct { |
| 74 |
ShowHelp func() error `short:"?" description:"Show this help message"` |
| 75 |
ShowHelp2 func() error `short:"h" long:"help" description:"Show this help message"` |
| 76 |
} |
| 77 |
help.ShowHelp = showHelp |
| 78 |
help.ShowHelp2 = showHelp |
| 79 |
return NewGroup("Help Options", &help) |
| 80 |
} |