Fix parsing of list options
This commit is contained in:
parent
c4ab964d09
commit
90c1745976
@ -108,7 +108,11 @@ func (self *Option) parsed_value() any {
|
|||||||
return len(self.parsed_values_from_cmdline)
|
return len(self.parsed_values_from_cmdline)
|
||||||
case StringOption:
|
case StringOption:
|
||||||
if self.IsList {
|
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
|
fallthrough
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -13,13 +13,21 @@ import (
|
|||||||
|
|
||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
|
|
||||||
|
type empty_options struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type base_options struct {
|
||||||
|
FromParent int
|
||||||
|
}
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
|
FromParent int
|
||||||
SimpleString string
|
SimpleString string
|
||||||
Choices string
|
Choices string
|
||||||
FromParent int
|
|
||||||
SetMe bool
|
SetMe bool
|
||||||
Int int
|
Int int
|
||||||
Float float64
|
Float float64
|
||||||
|
List []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCLIParsing(t *testing.T) {
|
func TestCLIParsing(t *testing.T) {
|
||||||
@ -33,6 +41,14 @@ func TestCLIParsing(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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()
|
actual_options := reflect.New(reflect.TypeOf(expected_options).Elem()).Interface()
|
||||||
err = cmd.GetOptionValues(actual_options)
|
err = cmd.GetOptionValues(actual_options)
|
||||||
if err != nil {
|
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: "--set-me", Type: "bool-set"})
|
||||||
child1.Add(OptionSpec{Name: "--int", Type: "int"})
|
child1.Add(OptionSpec{Name: "--int", Type: "int"})
|
||||||
child1.Add(OptionSpec{Name: "--float", Type: "float"})
|
child1.Add(OptionSpec{Name: "--float", Type: "float"})
|
||||||
|
child1.Add(OptionSpec{Name: "--list", Type: "list"})
|
||||||
|
child1.SubCommandIsOptional = true
|
||||||
|
gc1 := child1.AddSubCommand("", "gc1")
|
||||||
|
|
||||||
rt(
|
rt(
|
||||||
child1, "test --from-parent child1 -ps ss --choices b --from-parent one two",
|
child1, "test --from-parent child1 -ps ss --choices b --from-parent one two",
|
||||||
&options{SimpleString: "ss", Choices: "b", FromParent: 3},
|
&options{SimpleString: "ss", Choices: "b", FromParent: 3},
|
||||||
"one", "two",
|
"one", "two",
|
||||||
)
|
)
|
||||||
rt(child1, "test child1", &options{Choices: "a"})
|
rt(child1, "test child1", &options{})
|
||||||
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=foo one", &options{SimpleString: "foo", SetMe: true}, "one")
|
||||||
rt(child1, "test child1 --set-me --simple-string= one", &options{Choices: "a", 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{Choices: "a", SimpleString: "-s", Int: -3, Float: 3.3})
|
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", " "))
|
_, err := child1.ParseArgs(strings.Split("test child1 --choices x", " "))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("Invalid choice not caught")
|
t.Fatalf("Invalid choice not caught")
|
||||||
}
|
}
|
||||||
root.ResetAfterParseArgs()
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user