ParseBoolFlag scans os.Args backward to apply last-occurrence precedence for a boolean flag. Handles both --long[=true|false] and -s[=true|false] forms. Treats short flag in combined group (e.g. -xj) as implicit true. Returns false if the flag is absent or its value is invalid. Disabled entirely whe
(long string, short string)
| 73 | // Returns false if the flag is absent or its value is invalid. |
| 74 | // Disabled entirely when running under `go test`. |
| 75 | func ParseBoolFlag(long string, short string) bool { |
| 76 | if testing.Testing() { |
| 77 | return false |
| 78 | } |
| 79 | args := os.Args[1:] |
| 80 | longPrefix := "--" + long + "=" |
| 81 | shortPrefix := "-" + short + "=" |
| 82 | |
| 83 | for i := len(args) - 1; i >= 0; i-- { |
| 84 | arg := args[i] |
| 85 | switch { |
| 86 | case arg == "--"+long, arg == "-"+short: |
| 87 | return true |
| 88 | case strings.HasPrefix(arg, shortPrefix): |
| 89 | v, err := strconv.ParseBool(arg[len(shortPrefix):]) |
| 90 | if err == nil { |
| 91 | return v |
| 92 | } |
| 93 | case strings.HasPrefix(arg, longPrefix): |
| 94 | v, err := strconv.ParseBool(arg[len(longPrefix):]) |
| 95 | if err == nil { |
| 96 | return v |
| 97 | } |
| 98 | default: |
| 99 | if len(arg) > 1 && arg[0] == '-' && arg[1] != '-' { |
| 100 | for _, ch := range arg[1:] { |
| 101 | if string(ch) == short { |
| 102 | return true |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | return false |
| 109 | } |
| 110 | |
| 111 | // ColorsEnabled returns true if colored output is enabled |
| 112 | // Implementation from https://no-color.org/ |
no outgoing calls
no test coverage detected