MCPcopy
hub / github.com/spf13/cobra / getFlagNameCompletions

Function getFlagNameCompletions

completions.go:596–627  ·  view source on GitHub ↗
(flag *pflag.Flag, toComplete string)

Source from the content-addressed store, hash-verified

594}
595
596func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []Completion {
597 if nonCompletableFlag(flag) {
598 return []Completion{}
599 }
600
601 var completions []Completion
602 flagName := "--" + flag.Name
603 if strings.HasPrefix(flagName, toComplete) {
604 // Flag without the =
605 completions = append(completions, CompletionWithDesc(flagName, flag.Usage))
606
607 // Why suggest both long forms: --flag and --flag= ?
608 // This forces the user to *always* have to type either an = or a space after the flag name.
609 // Let's be nice and avoid making users have to do that.
610 // Since boolean flags and shortname flags don't show the = form, let's go that route and never show it.
611 // The = form will still work, we just won't suggest it.
612 // This also makes the list of suggested flags shorter as we avoid all the = forms.
613 //
614 // if len(flag.NoOptDefVal) == 0 {
615 // // Flag requires a value, so it can be suffixed with =
616 // flagName += "="
617 // completions = append(completions, CompletionWithDesc(flagName, flag.Usage))
618 // }
619 }
620
621 flagName = "-" + flag.Shorthand
622 if len(flag.Shorthand) > 0 && strings.HasPrefix(flagName, toComplete) {
623 completions = append(completions, CompletionWithDesc(flagName, flag.Usage))
624 }
625
626 return completions
627}
628
629func completeRequireFlags(finalCmd *Command, toComplete string) []Completion {
630 var completions []Completion

Callers 2

getCompletionsMethod · 0.85
completeRequireFlagsFunction · 0.85

Calls 2

nonCompletableFlagFunction · 0.85
CompletionWithDescFunction · 0.85

Tested by

no test coverage detected