62 lines
910 B
Go
62 lines
910 B
Go
package flag
|
|
|
|
func isShort(e string) bool {
|
|
re := []rune(e)
|
|
|
|
return e != "--" && len(re) == 2 && re[0] == '-'
|
|
}
|
|
|
|
func isLong(e string) bool {
|
|
return len(e) > 2 && e[:2] == "--"
|
|
}
|
|
|
|
func isCombined(e string) bool {
|
|
re := []rune(e)
|
|
|
|
return len(re) > 2 && re[0] != '-'
|
|
}
|
|
|
|
func isUnixFlag(e string) bool {
|
|
return isShort(e) || isLong(e)
|
|
}
|
|
|
|
func formatArg(e string) []string {
|
|
if isCombined(e) {
|
|
var out []string
|
|
for _, r := range e[1:] {
|
|
out = append(out, string([]rune{'-', r}))
|
|
}
|
|
return out
|
|
}
|
|
|
|
return []string{e}
|
|
}
|
|
|
|
func formatArgs(args []string) []string {
|
|
var (
|
|
out []string
|
|
ignore bool
|
|
)
|
|
|
|
for _, e := range args {
|
|
if ignore {
|
|
out = append(out, e)
|
|
} else {
|
|
ignore = e == "--"
|
|
out = append(out, formatArg(e)...)
|
|
}
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func containsUnixFlag(args []string) bool {
|
|
for _, arg := range args {
|
|
if isUnixFlag(arg) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|