Repository: cli/cli
File: internal/prompter/prompter.go (Go)

Code before:
				Value(&result).
				Options(formOptions...),
		),
	)

	err := form.Run()
	return result, err
}

func (p *accessiblePrompter) MultiSelect(prompt string, defaults []string, options []string) ([]int, error) {
	var result []int
	prompt = p.addDefaultsToPrompt(prompt, defaults)
	formOptions := make([]huh.Option[int], len(options))
	for i, o := range options {
		// If this option is in the defaults slice,
		// let's add its index to the result slice and huh
		// will treat it as a default selection.
		if slices.Contains(defaults, o) {
			result = append(result, i)
		}

		formOptions[i] = huh.NewOption(o, i)
	}

	form := p.newForm(
		huh.NewGroup(
			huh.NewMultiSelect[int]().
				Title(prompt).
				Value(&result).
				Limit(len(options)).
				Options(formOptions...),
		),
	)

	if err := form.Run(); err != nil {
		return nil, err
	}

	return result, nil
}

func (p *accessiblePrompter) Input(prompt, defaultValue string) (string, error) {
	result := defaultValue
	prompt = p.addDefaultsToPrompt(prompt, []string{defaultValue})
	form := p.newForm(
		huh.NewGroup(
			huh.NewInput().
				Title(prompt).
				Value(&result),
		),
	)

Review comment (bug):
**question:** should we filter the defaults based on the options first before building the default prompt?

Wanting to see what this experience was like, I saw the `gh pr create` and `gh issue create` experience provide default values when flags are passed in for `--label`.  With these new changes, invalid labels are listed as the default though they are not selected in the listing because they are not valid options:

### Current experience

```shell
$ GH_ACCESSIBLE_PROMPTER=1 gh issue create --label bug --label documentation --label question --repo tinyfists/actions-experiments

Creating issue in tinyfists/actions-experiments

Title (required) () Testing out the current experience

Choose a template 
1. Services Delivery AMER East: Weekly Team Sync
2. Open a blank issue
Input a number between 1 and 2: 2

Body 
1. Launch vim
2. Skip
Input a number between 1 and 2: 2

What's next? 
1. Submit
2. Add metadata
3. Cancel
Input a number between 1 and 3: 2

What would you like to add? 
Select up to 4 options.
1.   Assignees
2.   Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 2

1.   Assignees
2. ✓ Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 0

Labels 
Select up to 15 options.
1.   :ballot_box_with_check: meeting-notes
2. ✓ bug
3. ✓ documentation
4.   duplicate
5.   enhancement
6.   good first issue
7.   help wanted
8.   invalid
9.   O: meeting minutes
10. ✓ question
11.   test
12.   test2
13.   triage
14.   whatever
15.   wontfix
0.   Confirm selection
Input a number between 0 and 15: 
```

```shell
$ GH_ACCESSIBLE_PROMPTER=1 gh issue create --label a --label b --label c --repo tinyfists/actions-experiments

Creating issue in tinyfists/actions-experiments

Title (required) () Testing out the current experience with invalid labels

Choose a template 
1. Services Delivery AMER East: Weekly Team Sync
2. Open a blank issue
Input a number between 1 and 2: 2

Body 
1. Launch vim
2. Skip
Input a number between 1 and 2: 2

What's next? 
1. Submit
2. Add metadata
3. Cancel
Input a number between 1 and 3: 2

What would you like to add? 
Select up to 4 options.
1.   Assignees
2.   Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 2

1.   Assignees
2. ✓ Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 0

Labels 
Select up to 15 options.
1.   :ballot_box_with_check: meeting-notes
2.   bug
3.   documentation
4.   duplicate
5.   enhancement
6.   good first issue
7.   help wanted
8.   invalid
9.   O: meeting minutes
10.   question
11.   test
12.   test2
13.   triage
14.   whatever
15.   wontfix
0.   Confirm selection
Input a number between 0 and 15: 
```

### New experience

```shell
$ GH_ACCESSIBLE_PROMPTER=1 bin/gh issue create --label bug --label documentation --label question --repo tinyfists/actions-experiments

Creating issue in tinyfists/actions-experiments

Title (required) Testing out the new experience

Choose a template 
1. Services Delivery AMER East: Weekly Team Sync
2. Open a blank issue
Input a number between 1 and 2: 2

Body 
1. Launch vim
2. Skip
Input a number between 1 and 2: 2

What's next? 
1. Submit
2. Add metadata
3. Cancel
Input a number between 1 and 3: 2

What would you like to add? 
Select up to 4 options.
1.   Assignees
2.   Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 2

1.   Assignees
2. ✓ Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 0

Labels (defaults: bug, documentation, question) 
Select up to 15 options.
1.   :ballot_box_with_check: meeting-notes
2. ✓ bug
3. ✓ documentation
4.   duplicate
5.   enhancement
6.   good first issue
7.   help wanted
8.   invalid
9.   O: meeting minutes
10. ✓ question
11.   test
12.   test2
13.   triage
14.   whatever
15.   wontfix
0.   Confirm selection
Input a number between 0 and 15:
```

```shell
$ GH_ACCESSIBLE_PROMPTER=1 bin/gh issue create --label a --label b --label c --repo tinyfists/actions-experiments                     

Creating issue in tinyfists/actions-experiments

Title (required) Testing out the new experience with invalid labels

Choose a template 
1. Services Delivery AMER East: Weekly Team Sync
2. Open a blank issue
Input a number between 1 and 2: 2

Body 
1. Launch vim
2. Skip
Input a number between 1 and 2: 2

What's next? 
1. Submit
2. Add metadata
3. Cancel
Input a number between 1 and 3: 2

What would you like to add? 
Select up to 4 options.
1.   Assignees
2.   Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 2

1.   Assignees
2. ✓ Labels
3.   Projects
4.   Milestone
0.   Confirm selection
Input a number between 0 and 4: 0

Labels (defaults: a, b, c) 
Select up to 15 options.
1.   :ballot_box_with_check: meeting-notes
2.   bug
3.   documentation
4.   duplicate
5.   enhancement
6.   good first issue
7.   help wanted
8.   invalid
9.   O: meeting minutes
10.   question
11.   test
12.   test2
13.   triage
14.   whatever
15.   wontfix
0.   Confirm selection
Input a number between 0 and 15: 
```

Code after:
		}
		formOptions = append(formOptions, huh.NewOption(o, i))
	}

	form := p.newForm(
		huh.NewGroup(
			huh.NewSelect[int]().
				Title(prompt).
				Value(&result).
				Options(formOptions...),
		),
	)

	err := form.Run()
	return result, err
}

func (p *accessiblePrompter) MultiSelect(prompt string, defaults []string, options []string) ([]int, error) {
	var result []int

	// Remove invalid default values from the defaults slice.
	defaults = slices.DeleteFunc(defaults, func(s string) bool {
		return !slices.Contains(options, s)
	})

	prompt = p.addDefaultsToPrompt(prompt, defaults)
	formOptions := make([]huh.Option[int], len(options))
	for i, o := range options {
		// If this option is in the defaults slice,
		// let's add its index to the result slice and huh
		// will treat it as a default selection.
		if slices.Contains(defaults, o) {
			result = append(result, i)
		}

		formOptions[i] = huh.NewOption(o, i)
	}

	form := p.newForm(
		huh.NewGroup(
			huh.NewMultiSelect[int]().
				Title(prompt).
				Value(&result).
				Limit(len(options)).
				Options(formOptions...),
		),
	)

	if err := form.Run(); err != nil {
		return nil, err
	}