From 29224ee030bd4e687e95d1d8df55303269b49337 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Mon, 1 Jun 2015 00:23:40 -0400 Subject: Add new StringSlice flag type Arguments passed on the command line will be split on "," and will be stored in a slice. We can see this already exits in codegangsta/cli https://github.com/codegangsta/cli/blob/44d40054fa6208a3013d7217aca72a2b8b0f5a0b/flag.go#L102 And people have written their own implementations for cobra/pflag https://github.com/GoogleCloudPlatform/kubernetes/blob/c5ba95ee26cbec9694a780544b559a797956ea54/pkg/util/list.go Lets just make it a first class flag --- string_slice_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 string_slice_test.go (limited to 'string_slice_test.go') diff --git a/string_slice_test.go b/string_slice_test.go new file mode 100644 index 0000000..019afee --- /dev/null +++ b/string_slice_test.go @@ -0,0 +1,34 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package pflag + +import ( + "fmt" + "strings" + "testing" +) + +func setUpSSFlagSet(ssp *[]string) *FlagSet { + f := NewFlagSet("test", ContinueOnError) + f.StringSliceVar(ssp, "ss", []string{}, "Command seperated list!") + return f +} + +func TestSS(t *testing.T) { + var ss []string + f := setUpSSFlagSet(&ss) + + vals := []string{"one", "two", "4", "3"} + arg := fmt.Sprintf("--ss=%s", strings.Join(vals, ",")) + err := f.Parse([]string{arg}) + if err != nil { + t.Fatal("expected no error; got", err) + } + for i, v := range ss { + if vals[i] != v { + t.Fatal("expected ss[%d] to be %s but got: %s", i, vals[i], v) + } + } +} -- cgit v1.2.3