diff --git a/tools/cli/types.go b/tools/cli/types.go index be068af90..cbe033721 100644 --- a/tools/cli/types.go +++ b/tools/cli/types.go @@ -108,7 +108,11 @@ func (self *Option) parsed_value() any { return len(self.parsed_values_from_cmdline) case StringOption: if self.IsList { - return self.parsed_values_from_cmdline + ans := make([]string, len(self.parsed_values_from_cmdline)) + for i, x := range self.parsed_values_from_cmdline { + ans[i] = x.(string) + } + return ans } fallthrough default: diff --git a/tools/cli/types_test.go b/tools/cli/types_test.go index 99d6c5d2f..717383971 100644 --- a/tools/cli/types_test.go +++ b/tools/cli/types_test.go @@ -13,13 +13,21 @@ import ( var _ = fmt.Print +type empty_options struct { +} + +type base_options struct { + FromParent int +} + type options struct { + FromParent int SimpleString string Choices string - FromParent int SetMe bool Int int Float float64 + List []string } func TestCLIParsing(t *testing.T) { @@ -33,6 +41,14 @@ func TestCLIParsing(t *testing.T) { if err != nil { t.Fatal(err) } + if s, ok := expected_options.(*options); ok { + if s.Choices == "" { + s.Choices = "a" + } + if s.List == nil { + s.List = make([]string, 0) + } + } actual_options := reflect.New(reflect.TypeOf(expected_options).Elem()).Interface() err = cmd.GetOptionValues(actual_options) if err != nil { @@ -58,20 +74,30 @@ func TestCLIParsing(t *testing.T) { child1.Add(OptionSpec{Name: "--set-me", Type: "bool-set"}) child1.Add(OptionSpec{Name: "--int", Type: "int"}) child1.Add(OptionSpec{Name: "--float", Type: "float"}) + child1.Add(OptionSpec{Name: "--list", Type: "list"}) + child1.SubCommandIsOptional = true + gc1 := child1.AddSubCommand("", "gc1") rt( child1, "test --from-parent child1 -ps ss --choices b --from-parent one two", &options{SimpleString: "ss", Choices: "b", FromParent: 3}, "one", "two", ) - rt(child1, "test child1", &options{Choices: "a"}) - rt(child1, "test child1 --set-me --simple-string=foo one", &options{Choices: "a", SimpleString: "foo", SetMe: true}, "one") - rt(child1, "test child1 --set-me --simple-string= one", &options{Choices: "a", SetMe: true}, "one") - rt(child1, "test child1 --int -3 --simple-string -s --float=3.3", &options{Choices: "a", SimpleString: "-s", Int: -3, Float: 3.3}) + rt(child1, "test child1", &options{}) + rt(child1, "test child1 --set-me --simple-string=foo one", &options{SimpleString: "foo", SetMe: true}, "one") + rt(child1, "test child1 --set-me --simple-string= one", &options{SetMe: true}, "one") + rt(child1, "test child1 --int -3 --simple-string -s --float=3.3", &options{SimpleString: "-s", Int: -3, Float: 3.3}) + rt(child1, "test child1 --list -3 -p --list one", &options{FromParent: 1, List: []string{"-3", "one"}}) + rt(gc1, "test -p child1 -p gc1 xxx", &empty_options{}, "xxx") _, err := child1.ParseArgs(strings.Split("test child1 --choices x", " ")) if err == nil { t.Fatalf("Invalid choice not caught") } root.ResetAfterParseArgs() + gc1.ParseArgs(strings.Split("test child1 -p gc1 xxx", " ")) + err = gc1.GetOptionValues(&base_options{}) + if err == nil { + t.Fatalf("Invalid choice not caught") + } }