Skip to content
Myr

Simplify stripFlags function

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent aea9481 · Browse files · 1 file+28 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+28 added−20 removed

  • other:+28 added−20 removed

1 file+28 added−20 removed

modified:command.go+28 added-20 removed
390 unmodified lines
391 return result392}393394func stripFlags(args []string, c *Command) []string {395 if len(args) < 1 {395 if len(args) == 0 {396 return args397 }398 c.mergePersistentFlags()399400 commands := []string{}401401 inQuote := false403 inFlag := false404 for _, y := range args {402403Loop:404 for len(args) > 0 {405 s := args[0]406 if !inQuote {407 switch {407 case strings.HasPrefix(y, "\""):408 case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):409 inQuote = true409 case strings.Contains(y, "=\""):410 inQuote = true411 case strings.HasPrefix(y, "--") && !strings.Contains(y, "="):412 // TODO: this isn't quite right, we should really check ahead for 'true' or 'false'413 inFlag = !hasNoOptDefVal(y[2:], c.Flags())414 case strings.HasPrefix(y, "-") && !strings.Contains(y, "=") && len(y) == 2 && !shortHasNoOptDefVal(y[1:], c.Flags()):415 inFlag = true416 case inFlag:417 inFlag = false418 case y == "":419 // strip empty commands, as the go tests expect this to be ok....420 case !strings.HasPrefix(y, "-"):421 commands = append(commands, y)422 inFlag = false410 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], c.Flags()):411 // If '--flag arg' then412 // delete two items from args.413 fallthrough // Do the same as below.414 case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], c.Flags()):415 // If '-f arg' then416 // delete two items from args.417418 // If there are only two elements in args or less,419 // break loop, ...420 if len(args) <= 2 {421 break Loop422 } else {423 // ... else delete first two items.424 args = args[2:]425 continue426 }427 case s != "" && !strings.HasPrefix(s, "-"):428 commands = append(commands, s)429 }430 }431426 if strings.HasSuffix(y, "\"") && !strings.HasSuffix(y, "\\\"") {432 if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {433 inQuote = false434 }435436 args = args[1:]437 }438439 return commands440}
804 unmodified lines