MCPcopy Index your code
hub / github.com/cli/cli / Select

Method Select

internal/prompter/prompter.go:116–146  ·  view source on GitHub ↗
(prompt, defaultValue string, options []string)

Source from the content-addressed store, hash-verified

114}
115
116func (p *accessiblePrompter) Select(prompt, defaultValue string, options []string) (int, error) {
117 var result int
118
119 // Remove invalid default values from the defaults slice.
120 if !slices.Contains(options, defaultValue) {
121 defaultValue = ""
122 }
123
124 prompt = p.addDefaultsToPrompt(prompt, []string{defaultValue})
125 formOptions := []huh.Option[int]{}
126 for i, o := range options {
127 // If this option is the default value, assign its index
128 // to the result variable. huh will treat it as a default selection.
129 if defaultValue == o {
130 result = i
131 }
132 formOptions = append(formOptions, huh.NewOption(o, i))
133 }
134
135 form := p.newForm(
136 huh.NewGroup(
137 huh.NewSelect[int]().
138 Title(prompt).
139 Value(&result).
140 Options(formOptions...),
141 ),
142 )
143
144 err := form.Run()
145 return result, err
146}
147
148func (p *accessiblePrompter) MultiSelect(prompt string, defaults []string, options []string) ([]int, error) {
149 var result []int

Callers

nothing calls this directly

Calls 7

addDefaultsToPromptMethod · 0.95
newFormMethod · 0.95
ContainsMethod · 0.80
OptionsMethod · 0.80
ValueMethod · 0.80
TitleMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected