瀏覽代碼

Support commas in parserNumberMenu

Alexander Popov 6 年之前
父節點
當前提交
6eded7c4a3
共有 2 個文件被更改,包括 7 次插入2 次删除
  1. 5 2
      parser.go
  2. 2 0
      parser_test.go

+ 5 - 2
parser.go

@@ -8,6 +8,7 @@ import (
 	"os"
 	"strconv"
 	"strings"
+	"unicode"
 )
 
 // A basic set implementation for strings.
@@ -613,7 +614,7 @@ func (parser *arguments) parseCommandLine() (err error) {
 	return
 }
 
-//parses input for number menus
+//parses input for number menus splitted by spaces or commas
 //supports individual selection: 1 2 3 4
 //supports range selections: 1-4 10-20
 //supports negation: ^1 ^1-4
@@ -629,7 +630,9 @@ func parseNumberMenu(input string) (intRanges, intRanges, stringSet, stringSet)
 	otherInclude := make(stringSet)
 	otherExclude := make(stringSet)
 
-	words := strings.Fields(input)
+	words := strings.FieldsFunc(input, func(c rune) bool {
+		return unicode.IsSpace(c) || c == ','
+	})
 
 	for _, word := range words {
 		var num1 int

+ 2 - 0
parser_test.go

@@ -65,6 +65,7 @@ func TestParseNumberMenu(t *testing.T) {
 		"abort all none",
 		"a-b ^a-b ^abort",
 		"1\t2   3      4\t\t  \t 5",
+		"1 2,3, 4,  5,6 ,7  ,8",
 		"",
 		"   \t   ",
 		"A B C D E",
@@ -78,6 +79,7 @@ func TestParseNumberMenu(t *testing.T) {
 		{intRanges{}, intRanges{}, makeStringSet("abort", "all", "none"), make(stringSet)},
 		{intRanges{}, intRanges{}, makeStringSet("a-b"), makeStringSet("abort", "a-b")},
 		{intRanges{makeIntRange(1, 1), makeIntRange(2, 2), makeIntRange(3, 3), makeIntRange(4, 4), makeIntRange(5, 5)}, intRanges{}, make(stringSet), make(stringSet)},
+		{intRanges{makeIntRange(1, 1), makeIntRange(2, 2), makeIntRange(3, 3), makeIntRange(4, 4), makeIntRange(5, 5), makeIntRange(6, 6), makeIntRange(7, 7), makeIntRange(8, 8)}, intRanges{}, make(stringSet), make(stringSet)},
 		{intRanges{}, intRanges{}, make(stringSet), make(stringSet)},
 		{intRanges{}, intRanges{}, make(stringSet), make(stringSet)},
 		{intRanges{}, intRanges{}, makeStringSet("a", "b", "c", "d", "e"), make(stringSet)},