MCPcopy Create free account
hub / github.com/ddev/ddev / ParseBoolFlag

Function ParseBoolFlag

pkg/output/output_setup.go:75–109  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

73// Returns false if the flag is absent or its value is invalid.
74// Disabled entirely when running under `go test`.
75func 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/

Callers 2

CanRunWithoutDockerFunction · 0.92
output_setup.goFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected