| 321 | } |
| 322 | |
| 323 | func stripFlags(args []string, c *Command) []string { |
| 324 | if len(args) < 1 { |
| 325 | return args |
| 326 | } |
| 327 | c.mergePersistentFlags() |
| 328 | |
| 329 | commands := []string{} |
| 330 | |
| 331 | inQuote := false |
| 332 | inFlag := false |
| 333 | for _, y := range args { |
| 334 | if !inQuote { |
| 335 | switch { |
| 336 | case strings.HasPrefix(y, "\""): |
| 337 | inQuote = true |
| 338 | case strings.Contains(y, "=\""): |
| 339 | inQuote = true |
| 340 | case strings.HasPrefix(y, "--") && !strings.Contains(y, "="): |
| 341 | // TODO: this isn't quite right, we should really check ahead for 'true' or 'false' |
| 342 | inFlag = !isBooleanFlag(y[2:], c.Flags()) |
| 343 | case strings.HasPrefix(y, "-") && !strings.Contains(y, "=") && len(y) == 2 && !isBooleanShortFlag(y[1:], c.Flags()): |
| 344 | inFlag = true |
| 345 | case inFlag: |
| 346 | inFlag = false |
| 347 | case y == "": |
| 348 | // strip empty commands, as the go tests expect this to be ok.... |
| 349 | case !strings.HasPrefix(y, "-"): |
| 350 | commands = append(commands, y) |
| 351 | inFlag = false |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if strings.HasSuffix(y, "\"") && !strings.HasSuffix(y, "\\\"") { |
| 356 | inQuote = false |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return commands |
| 361 | } |
| 362 | |
| 363 | // argsMinusFirstX removes only the first x from args. Otherwise, commands that look like |
| 364 | // openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]). |