commander

package module
v0.0.0-...-45e08fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2019 License: MIT Imports: 3 Imported by: 0

README

commander Build Status Go Report Card GoDoc

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}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command represents the Command object

func NewCommand

func NewCommand(format string) *Command

NewCommand creates a new Command object from the format passed in

func (*Command) Match

func (c *Command) Match(text string) (*proper.Properties, bool)

Match takes in the command and the text received, attempts to find the pattern and extract the parameters

func (*Command) Tokenize

func (c *Command) Tokenize() []*Token

Tokenize returns Command info as tokens

type Token

type Token struct {
	Word string
	Type int
}

Token represents the Token object

func (Token) IsParameter

func (t Token) IsParameter() bool

Directories

Path Synopsis
examples
1 command
2 command
3 command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL