bonzai/cmd_test.go

123 lines
2.3 KiB
Go
Raw Normal View History

2022-02-20 00:22:03 +00:00
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
2022-02-17 23:09:19 +00:00
package bonzai_test
import (
"fmt"
"github.com/rwxrob/bonzai"
2022-02-18 02:59:27 +00:00
"github.com/rwxrob/bonzai/cmd"
2022-02-17 23:09:19 +00:00
)
func ExampleCmd_Seek() {
hello := &bonzai.Cmd{
Name: `hello`,
Params: []string{"there"},
2022-02-24 13:06:10 +00:00
Call: func(_ *bonzai.Cmd, args ...string) error {
2022-02-17 23:09:19 +00:00
if len(args) > 0 {
fmt.Printf("hello %v\n", args[0])
return nil
}
fmt.Println("hello")
return nil
},
}
hi := &bonzai.Cmd{
Name: `hi`,
Params: []string{"there", "ya"},
2022-02-24 13:06:10 +00:00
Call: func(_ *bonzai.Cmd, args ...string) error {
2022-02-17 23:09:19 +00:00
if len(args) > 0 {
fmt.Printf("hi %v\n", args[0])
return nil
}
fmt.Println("hi")
return nil
},
}
yo := &bonzai.Cmd{
Name: `yo`,
2022-02-24 13:06:10 +00:00
Call: func(_ *bonzai.Cmd, args ...string) error {
2022-02-17 23:09:19 +00:00
fmt.Println("yo")
return nil
},
}
salut := &bonzai.Cmd{
Name: `salut`,
Params: []string{"la"},
2022-02-24 13:06:10 +00:00
Call: func(_ *bonzai.Cmd, args ...string) error {
2022-02-17 23:09:19 +00:00
if len(args) > 0 {
fmt.Printf("salut %v\n", args[0])
return nil
}
fmt.Println("salut")
return nil
},
}
french := &bonzai.Cmd{
Name: `french`,
Aliases: []string{"fr"},
2022-02-18 02:59:27 +00:00
Commands: []*bonzai.Cmd{cmd.Help, salut},
2022-02-17 23:09:19 +00:00
}
greet := &bonzai.Cmd{
Name: `greet`,
2022-02-18 02:59:27 +00:00
Commands: []*bonzai.Cmd{cmd.Help, yo, hi, hello, french},
2022-02-17 23:09:19 +00:00
}
cmd, args := greet.Seek(bonzai.ArgsFrom(`hi there`))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french salut`))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french salut `))
fmt.Printf("%v %q\n", cmd.Name, args)
2022-02-18 02:59:27 +00:00
cmd, args = greet.Seek(bonzai.ArgsFrom(`french h`))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french help`))
fmt.Printf("%v %q\n", cmd.Name, args)
2022-02-17 23:09:19 +00:00
// Output:
// hi ["there"]
// salut []
// salut [""]
2022-02-18 02:59:27 +00:00
// french ["h"]
// help []
2022-02-17 23:09:19 +00:00
}
func ExampleCmd_CmdNames() {
foo := new(bonzai.Cmd)
foo.Add("bar")
foo.Add("blah")
foo.Add("other")
fmt.Println(foo.CmdNames())
// Output:
// [bar blah other]
}
func ExampleCmd_GetCommands() {
foo := new(bonzai.Cmd)
foo.Add("bar")
foo.Add("blah")
foo.Add("other")
fmt.Println(foo.GetCommands())
// Output:
// [bar blah other]
}
func ExampleCmd_GetParams() {
foo := new(bonzai.Cmd)
foo.Params = []string{"box", "bing", "and"}
fmt.Println(foo.GetParams())
// Output:
// [box bing and]
}