Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shomali11/commander
Command evaluator and parser
https://github.com/shomali11/commander
command evaluator parameters parser
Last synced: 2 months ago
JSON representation
Command evaluator and parser
- Host: GitHub
- URL: https://github.com/shomali11/commander
- Owner: shomali11
- License: mit
- Created: 2017-06-09T22:35:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-30T02:38:02.000Z (over 1 year ago)
- Last Synced: 2024-10-25T02:46:22.563Z (3 months ago)
- Topics: command, evaluator, parameters, parser
- Language: Go
- Size: 79.1 KB
- Stars: 20
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# commander [![Build Status](https://travis-ci.com/shomali11/commander.svg?branch=master)](https://travis-ci.com/shomali11/commander) [![Go Report Card](https://goreportcard.com/badge/github.com/shomali11/commander)](https://goreportcard.com/report/github.com/shomali11/commander) [![GoDoc](https://godoc.org/github.com/shomali11/commander?status.svg)](https://godoc.org/github.com/shomali11/commander) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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
* Supports "word" {} vs "sentence" <> parameter matching## Dependencies
* `proper` [github.com/shomali11/proper](https://github.com/shomali11/proper)
# 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.
```go
package mainimport (
"fmt"
"github.com/shomali11/commander"
)func main() {
properties, isMatch := commander.NewCommand("ping").Match("ping")
fmt.Println(isMatch) // true
fmt.Println(properties) // {}properties, isMatch = commander.NewCommand("ping").Match("pong")
fmt.Println(isMatch) // false
fmt.Println(properties) // nilproperties, isMatch = commander.NewCommand("echo {word}").Match("echo hello world!")
fmt.Println(isMatch) // true
fmt.Println(properties.StringParam("word", "")) // helloproperties, isMatch = commander.NewCommand("echo ").Match("echo hello world!")
fmt.Println(isMatch) // true
fmt.Println(properties.StringParam("sentence", "")) // hello world!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)) // 5properties, 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)) // 0properties, isMatch = commander.NewCommand("search {size}").Match("search hello there everyone 10")
fmt.Println(isMatch) // true
fmt.Println(properties.StringParam("stuff", "")) // hello there everyone
fmt.Println(properties.IntegerParam("size", 0)) // 10
}
```## Example 2
In this example, we are tokenizing the command format and returning each token with a number that determines whether it is a parameter (word vs sentence) or not
```go
package mainimport (
"fmt"
"github.com/shomali11/commander"
)func main() {
tokens := commander.NewCommand("echo {word} ").Tokenize()
for _, token := range tokens {
fmt.Println(token)
}
}
```Output:
```
&{echo NOT_PARAMETER}
&{word WORD_PARAMETER}
&{sentence SENTENCE_PARAMETER}
```