(lastArg string, flags []Flag, writer io.Writer)
| 213 | } |
| 214 | |
| 215 | func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) { |
| 216 | // Trim to handle both "-short" and "--long" flags. |
| 217 | cur := strings.TrimLeft(lastArg, "-") |
| 218 | for _, flag := range flags { |
| 219 | if bflag, ok := flag.(*BoolFlag); ok && bflag.Hidden { |
| 220 | continue |
| 221 | } |
| 222 | |
| 223 | usage := "" |
| 224 | if docFlag, ok := flag.(DocGenerationFlag); ok { |
| 225 | usage = docFlag.GetUsage() |
| 226 | } |
| 227 | |
| 228 | name := strings.TrimSpace(flag.Names()[0]) |
| 229 | // this will get total count utf8 letters in flag name |
| 230 | count := utf8.RuneCountInString(name) |
| 231 | if count > 2 { |
| 232 | count = 2 // reuse this count to generate single - or -- in flag completion |
| 233 | } |
| 234 | // if flag name has more than one utf8 letter and last argument in cli has -- prefix then |
| 235 | // skip flag completion for short flags example -v or -x |
| 236 | if strings.HasPrefix(lastArg, "--") && count == 1 { |
| 237 | continue |
| 238 | } |
| 239 | // match if last argument matches this flag and it is not repeated |
| 240 | if strings.HasPrefix(name, cur) && cur != name /* && !cliArgContains(name, os.Args)*/ { |
| 241 | flagCompletion := fmt.Sprintf("%s%s", strings.Repeat("-", count), name) |
| 242 | if usage != "" { |
| 243 | flagCompletion = fmt.Sprintf("%s:%s", flagCompletion, usage) |
| 244 | } |
| 245 | fmt.Fprintln(writer, flagCompletion) |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func DefaultCompleteWithFlags(ctx context.Context, cmd *Command) { |
| 251 | args := os.Args |
no test coverage detected