Support commas in parserNumberMenu

This commit is contained in:
Alexander Popov 2018-07-02 14:26:51 +03:00
parent 3406575f9d
commit 6eded7c4a3
No known key found for this signature in database
GPG key ID: 34ECE73756447714
2 changed files with 7 additions and 2 deletions

View file

@ -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

View file

@ -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)},