SetFieldDefault sets the value of flag to val if flag is not present in fields. For example, SetFieldDefault(fields, "--foo", "bar") will append "--foo=bar" if fields is empty or does not include a "--foo" flag. It returns fields unchanged if "--foo" is present.
(fields []string, flag, val string)
| 188 | // fields is empty or does not include a "--foo" flag. |
| 189 | // It returns fields unchanged if "--foo" is present. |
| 190 | func SetFieldDefault(fields []string, flag, val string) []string { |
| 191 | fields, cur, present := ExtractField(fields, flag) |
| 192 | if !present { |
| 193 | cur = val |
| 194 | } |
| 195 | return append(fields, flag+"="+cur) |
| 196 | } |
| 197 | |
| 198 | // ExtractField input ("--a=this --b=that --c=other", "--b") returns [--a=this, --c=other"], "that", true |
| 199 | // |