fix(cli): accept comma-separated multi-select input
All checks were successful
Security Scan / security (push) Successful in 19s
All checks were successful
Security Scan / security (push) Successful in 19s
This commit is contained in:
parent
02d4ee74e6
commit
b1850124de
2 changed files with 21 additions and 2 deletions
|
|
@ -91,6 +91,22 @@ func TestChooseMulti_Good_Filter(t *testing.T) {
|
|||
assert.Equal(t, []string{"apple", "apricot"}, vals)
|
||||
}
|
||||
|
||||
func TestChooseMulti_Good_Commas(t *testing.T) {
|
||||
SetStdin(strings.NewReader("1,3\n"))
|
||||
defer SetStdin(nil)
|
||||
|
||||
vals := ChooseMulti("Pick", []string{"a", "b", "c"})
|
||||
assert.Equal(t, []string{"a", "c"}, vals)
|
||||
}
|
||||
|
||||
func TestChooseMulti_Good_CommasAndRanges(t *testing.T) {
|
||||
SetStdin(strings.NewReader("1-2,4\n"))
|
||||
defer SetStdin(nil)
|
||||
|
||||
vals := ChooseMulti("Pick", []string{"a", "b", "c", "d"})
|
||||
assert.Equal(t, []string{"a", "b", "d"}, vals)
|
||||
}
|
||||
|
||||
func TestSetStdin_Good_ResetNil(t *testing.T) {
|
||||
original := stdin
|
||||
t.Cleanup(func() { stdin = original })
|
||||
|
|
|
|||
|
|
@ -503,12 +503,15 @@ func looksLikeMultiSelectionInput(input string) bool {
|
|||
return hasDigit
|
||||
}
|
||||
|
||||
// parseMultiSelection parses a multi-selection string like "1 3 5" or "1-3 5".
|
||||
// parseMultiSelection parses a multi-selection string like "1 3 5", "1,3,5",
|
||||
// or "1-3 5".
|
||||
// Returns 0-based indices.
|
||||
func parseMultiSelection(input string, maxItems int) ([]int, error) {
|
||||
selected := make(map[int]bool)
|
||||
|
||||
for part := range strings.FieldsSeq(input) {
|
||||
normalized := strings.NewReplacer(",", " ").Replace(input)
|
||||
|
||||
for part := range strings.FieldsSeq(normalized) {
|
||||
// Check for range (e.g., "1-3")
|
||||
if strings.Contains(part, "-") {
|
||||
var rangeParts []string
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue