From 9fd432aed36ed4aa483cb2c0b6548dd21b591663 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 04:40:23 +0000 Subject: [PATCH] fix(cli): support comma and range multi-select input --- pkg/cli/prompt.go | 23 ++++++++++++++++++++--- pkg/cli/prompt_test.go | 9 +++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pkg/cli/prompt.go b/pkg/cli/prompt.go index 61f806b..7ff4c8c 100644 --- a/pkg/cli/prompt.go +++ b/pkg/cli/prompt.go @@ -87,9 +87,26 @@ func MultiSelect(label string, options []string) ([]string, error) { } var selected []string - for _, s := range strings.Fields(input) { - n, err := strconv.Atoi(s) - if err != nil || n < 1 || n > len(options) { + normalized := strings.NewReplacer(",", " ").Replace(input) + for _, s := range strings.Fields(normalized) { + if strings.Contains(s, "-") { + parts := strings.SplitN(s, "-", 2) + if len(parts) != 2 { + continue + } + start, startErr := strconv.Atoi(parts[0]) + end, endErr := strconv.Atoi(parts[1]) + if startErr != nil || endErr != nil || start < 1 || end > len(options) || start > end { + continue + } + for n := start; n <= end; n++ { + selected = append(selected, options[n-1]) + } + continue + } + + n, convErr := strconv.Atoi(s) + if convErr != nil || n < 1 || n > len(options) { continue } selected = append(selected, options[n-1]) diff --git a/pkg/cli/prompt_test.go b/pkg/cli/prompt_test.go index d40d7d5..363a55e 100644 --- a/pkg/cli/prompt_test.go +++ b/pkg/cli/prompt_test.go @@ -52,6 +52,15 @@ func TestMultiSelect_Good(t *testing.T) { assert.Equal(t, []string{"a", "c"}, vals) } +func TestMultiSelect_Good_CommasAndRanges(t *testing.T) { + SetStdin(strings.NewReader("1-2,4\n")) + defer SetStdin(nil) + + vals, err := MultiSelect("Pick", []string{"a", "b", "c", "d"}) + assert.NoError(t, err) + assert.Equal(t, []string{"a", "b", "d"}, vals) +} + func TestConfirm_Good(t *testing.T) { SetStdin(strings.NewReader("y\n")) defer SetStdin(nil) -- 2.45.3