final checks on opt values moved after option parsing is complete
This commit is contained in:
+57
-50
@@ -37,7 +37,7 @@ func TestOneOptWithEqual(t *testing.T) {
|
||||
func addBoxOption(cli *CliParser, boxPtr *[]int) {
|
||||
// Define options
|
||||
optRef := cli.AddIntArrayOpt("box", "b", boxPtr, []int{1, 2, 3, 4}, "Box spec: Left,Width,Top,Height; provide 4 int values")
|
||||
optRef.OnFinalCheck(func(cliValue string, currentValue any) (err error) {
|
||||
optRef.OnFinalCheck(func(currentValue any) (err error) {
|
||||
if array, ok := currentValue.([]int); ok {
|
||||
if len(array) != 4 {
|
||||
err = fmt.Errorf("--box option requires exactly 4 items, %d provided", len(array))
|
||||
@@ -49,8 +49,34 @@ func addBoxOption(cli *CliParser, boxPtr *[]int) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArrayOpt(t *testing.T) {
|
||||
func testIntArrayOptOk(t *testing.T, n int, args []string) {
|
||||
var box []int
|
||||
var cli CliParser
|
||||
t.Logf("Arg set n. %d", n)
|
||||
addBoxOption(&cli, &box)
|
||||
cli.Init(args, "1.0.0", "TestIntArrayOpt description")
|
||||
if err := cli.Parse(); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(box) != 4 {
|
||||
t.Errorf(`Expected 4 items, got %d`, len(box))
|
||||
}
|
||||
}
|
||||
|
||||
func testIntArrayOptKo(t *testing.T, n int, args []string, msg string) {
|
||||
var box []int
|
||||
var cli CliParser
|
||||
t.Logf("Arg set n. %d", n)
|
||||
addBoxOption(&cli, &box)
|
||||
cli.Init(args, "1.0.0", "TestIntArrayOpt description")
|
||||
if err := cli.Parse(); err == nil {
|
||||
t.Errorf("Expected error, got nil")
|
||||
} else if err.Error() != msg {
|
||||
t.Errorf(`Invalid error: %q; expected: %q`, err.Error(), msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntArrayOpt(t *testing.T) {
|
||||
var args []string
|
||||
// Always recover from panic to return error before adding options
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
@@ -61,68 +87,49 @@ func TestIntArrayOpt(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
args_1 := []string{
|
||||
n := 0
|
||||
|
||||
n++
|
||||
args = []string{
|
||||
"TestIntArrayOpt",
|
||||
"--box=100,200,150,450",
|
||||
}
|
||||
testIntArrayOptOk(t, n, args)
|
||||
|
||||
if true {
|
||||
var cli CliParser
|
||||
t.Logf("Arg set n. 1")
|
||||
addBoxOption(&cli, &box)
|
||||
cli.Init(args_1, "1.0.0", "TestIntArrayOpt description")
|
||||
if err := cli.Parse(); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(box) != 4 {
|
||||
t.Errorf(`Expected 4 items, got %d`, len(box))
|
||||
}
|
||||
}
|
||||
|
||||
args_2 := []string{
|
||||
n++
|
||||
args = []string{
|
||||
"TestIntArrayOpt",
|
||||
"--box=100,200,150",
|
||||
}
|
||||
if true {
|
||||
var cli CliParser
|
||||
t.Logf("Arg set n. 2")
|
||||
addBoxOption(&cli, &box)
|
||||
cli.Init(args_2, "1.0.0", "TestIntArrayOpt description")
|
||||
if err := cli.Parse(); err == nil {
|
||||
t.Errorf("Expected error, got nil")
|
||||
} else if err.Error() != "--box option requires exactly 4 items, 3 provided" {
|
||||
t.Errorf(`Invalid error: %q; expected: "--box option requires exactly 4 items, 3 provided"`, err.Error())
|
||||
}
|
||||
}
|
||||
testIntArrayOptKo(t, n, args, "--box option requires exactly 4 items, 3 provided")
|
||||
|
||||
args_3 := []string{
|
||||
n++
|
||||
args = []string{
|
||||
"TestIntArrayOpt",
|
||||
"--box", "100,200,150,450",
|
||||
}
|
||||
if true {
|
||||
var cli CliParser
|
||||
t.Logf("Arg set n. 3")
|
||||
addBoxOption(&cli, &box)
|
||||
cli.Init(args_3, "1.0.0", "TestIntArrayOpt description")
|
||||
if err := cli.Parse(); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(box) != 4 {
|
||||
t.Errorf(`Expected 4 items, got %d`, len(box))
|
||||
}
|
||||
}
|
||||
testIntArrayOptOk(t, n, args)
|
||||
|
||||
args_4 := []string{
|
||||
n++
|
||||
args = []string{
|
||||
"TestIntArrayOpt",
|
||||
"--box",
|
||||
}
|
||||
if true {
|
||||
var cli CliParser
|
||||
t.Logf("Arg set n. 4")
|
||||
addBoxOption(&cli, &box)
|
||||
cli.Init(args_4, "1.0.0", "TestIntArrayOpt description")
|
||||
if err := cli.Parse(); err == nil {
|
||||
t.Errorf("Expected error, got nil")
|
||||
} else if err.Error() != `option "box" requires a value` {
|
||||
t.Errorf(`Invalid error - Expected: 'option "box" requires a value'; got '%s'`, err.Error())
|
||||
}
|
||||
testIntArrayOptKo(t, n, args, `option "box" requires a value`)
|
||||
|
||||
n++
|
||||
args = []string{
|
||||
"TestIntArrayOpt",
|
||||
"--box", "100,200,150",
|
||||
"--box", "450",
|
||||
}
|
||||
testIntArrayOptOk(t, n, args)
|
||||
|
||||
n++
|
||||
args = []string{
|
||||
"TestIntArrayOpt",
|
||||
"--box", "100,200,150",
|
||||
"--box", "450,12",
|
||||
}
|
||||
testIntArrayOptKo(t, n, args, "--box option requires exactly 4 items, 5 provided")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user