Skip to content
Myr

Simplify stripFlags

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 63121c8 · Browse files · 1 file+12 added−22 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+12 added−22 removed

  • other:+12 added−22 removed

1 file+12 added−22 removed

modified:command.go+12 added-22 removed
364 unmodified lines
365366{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`367}368369// Really only used when casting a command to a commander.370func (c *Command) resetChildrensParents() {371 for _, x := range c.commands {372 x.parent = c373 }374}375369func hasNoOptDefVal(name string, f *flag.FlagSet) bool {370 flag := f.Lookup(name)371 if flag == nil {372 return false373 }381 return len(flag.NoOptDefVal) > 0374 return flag.NoOptDefVal != ""375}376377func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {378 result := false379 fs.VisitAll(func(flag *flag.Flag) {387 if flag.Shorthand == name && len(flag.NoOptDefVal) > 0 {380 if flag.Shorthand == name && flag.NoOptDefVal != "" {381 result = true382 return383 }384 })385 return result386}
5 unmodified lines
392 c.mergePersistentFlags()393394 commands := []string{}395 inQuote := false396 flags := c.Flags()397398Loop:399 for len(args) > 0 {400 s := args[0]401 args = args[1:]402 if !inQuote {403 switch {404 case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):405 inQuote = true410 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], c.Flags()):406 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):407 // 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()):408 // delete arg from args.409 fallthrough // (do the same as below)410 case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):411 // 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 {412 // delete 'arg' from args or break the loop if len(args) <= 1.413 if len(args) <= 1 {414 break Loop415 } else {423 // ... else delete first two items.424 args = args[2:]416 args = args[1:]417 continue418 }419 case s != "" && !strings.HasPrefix(s, "-"):420 commands = append(commands, s)
2 unmodified lines
423424 if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {425 inQuote = false426 }435436 args = args[1:]427 }428429 return commands430}
804 unmodified lines