Command evaluator and parser
- Matches commands against provided text
- Extracts parameters from matching input
- Provides default values for missing parameters
- Supports String, Integer, Float and Boolean parameters
proper
github.com/shomali11/proper
In this example, we are matching a few strings against a command format, then parsing parameters if found or returning default values.
package main
import (
"fmt"
"github.com/shomali11/commander"
)
func main() {
properties, isMatch := commander.NewCommand("echo <text>").Match("echo hey")
fmt.Println(isMatch) // true
fmt.Println(properties.StringParam("text", "")) // hey
properties, isMatch = commander.NewCommand("repeat <word> <number>").Match("repeat hey 5")
fmt.Println(isMatch) // true
fmt.Println(properties.StringParam("word", "")) // hey
fmt.Println(properties.IntegerParam("number", 0)) // 5
properties, isMatch = commander.NewCommand("repeat <word> <number>").Match("repeat hey")
fmt.Println(isMatch) // true
fmt.Println(properties.StringParam("word", "")) // hey
fmt.Println(properties.IntegerParam("number", 0)) // 0
}
In this example, we are tokenizing the command format and returning each token with a boolean that determines whether it is a parameter or not
package main
import (
"fmt"
"github.com/shomali11/commander"
)
func main() {
tokens := commander.NewCommand("echo <text>").Tokenize()
for _, token := range tokens {
fmt.Println(token)
}
}
Output:
&{echo false}
&{text true}