Giter VIP home page Giter VIP logo

commander's Introduction

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
  • Supports "word" {} vs "sentence" <> parameter matching

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("ping").Match("ping")
	fmt.Println(isMatch)            // true
	fmt.Println(properties)         // {}

	properties, isMatch = commander.NewCommand("ping").Match("pong")
	fmt.Println(isMatch)            // false
	fmt.Println(properties)         // nil

	properties, isMatch = commander.NewCommand("echo {word}").Match("echo hello world!")
	fmt.Println(isMatch)                               // true
	fmt.Println(properties.StringParam("word", ""))    // hello

	properties, isMatch = commander.NewCommand("echo <sentence>").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))    // 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

	properties, isMatch = commander.NewCommand("search <stuff> {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

package main

import (
	"fmt"
	"github.com/shomali11/commander"
)

func main() {
	tokens := commander.NewCommand("echo {word} <sentence>").Tokenize()
	for _, token := range tokens {
		fmt.Println(token)
	}
}

Output:

&{echo NOT_PARAMETER}
&{word WORD_PARAMETER}
&{sentence SENTENCE_PARAMETER}

commander's People

Contributors

avbasov avatar k1low avatar raed-shomali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

commander's Issues

Parsing of text with non-breaking space has incorrect results.

I have written a slack bot using slacker, however, during testing we have encountered strange issues with parsing texts with URLs. After a little bit of tinkering, I pinpointed the issue - slack automatically replaces normal space right before URL with a non-breaking one in a command text. I raised a question to slack support and they replied that slack expects the application is able to handle UTF-8 characters. Therefore I would like to ask if it would be possible to replace them either in this library just before command parsing or in slacker before sending the text to this library?

An example which will result in incorrect parsing:

package main

import (
	"fmt"
	"github.com/shomali11/commander"
)

func main() {
	properties, isMatch := commander.NewCommand("set <component> <environment> <xpath> <value>").Match("set be approval xpath-expression\u00A0https://some-url/")
	fmt.Println(isMatch)
	fmt.Println(properties.StringParam("xpath", ""))
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.