Skip to content

logzio/commander

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

commander Build Status Go Report Card GoDoc License: MIT

Command evaluator and parser

Features

  • Matches commands against provided text
  • Extracts parameters from matching input
  • Provides default values for missing parameters
  • Supports String, Integer, Float and Boolean parameters

Dependencies

Examples

Example 1

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
}

Example 2

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}

About

Command evaluator and parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%