matcher

package module
v0.0.0-...-5b76eec Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2020 License: MIT Imports: 3 Imported by: 5 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Matcher

A library for parsing and matching based on a mask for use with CLI or Bots.

Mask Rules

  • xyz text xyz
  • <xyz> required var named xyz
  • [xyz] optional var named xyz
  • [xyz...] optional var named xyz and captures remaining input
  • [xyz(string:foo,bar)] optional var named xyz that can only be foo or bar
  • <-xyz> required short flag named xyz
  • [-xyz] optional short flag named xyz
  • [-] capture any short flag
  • <--xyz> required long flag named xyz
  • [--xyz] optional long flag named xyz
  • [--] capture any long flag

Examples

Mask Input Match
run run true
run walk false
run <speed> [distance] run fast true
run <speed> [distance] run fast far true
run <speed> [distance] run fast far forever false
run <speed> [distance] run fast "far forever" true
run <speed> [distance] <-j> run fast far -j true
run <speed> [distance] <-j> run fast far false
run <speed> [distance] <--jump> run fast far --jump=high true
run <speed> [distance] <--jump> run fast far false
run <speed> [distance] [--jump] run fast far --jump=high true
run <speed> [distance] [--jump] run fast far true
run <speed> [distance] [--jump] run fast far --skip false
run <speed> [distance] [--] run fast --jump --skip true
run [song...] run Welcome to the Jungle true
run <speed(string)> run fast true
run <speed(string:fast,slow)> run fast true
run <speed(string:fast,slow)> run quickly false
run <speed(int)> run 6 true
run <speed(int:0,2,4,6)> run 6 true
run <speed(int:0,2,4,6)> run 3 false
run <--jump(bool)> run --jump true
run <--jump(bool)> run --jump=false true

Matcher

match, command, values := matcher.Matcher("run <speed> [distance]", "run fast far")
if match {
	fmt.Printf("Command:%v\n", command)
	for k, v := range values {
		fmt.Printf("%v:%v\n", k, v)
	}
	fmt.Print("\n")
}

- - -
Command:run
speed:fast
distance:far

Masker

tokens := matcher.Masker("run <speed> [distance] [--jump]")
for _, t := range tokens {
	fmt.Printf("Name:%v ", t.Name)
	fmt.Printf("Type:%v ", t.Type)
	fmt.Printf("Valid:%v ", t.Valid)
	fmt.Printf("Required:%v ", t.Required)
	fmt.Printf("Short Flag:%v ", t.ShortFlag)
	fmt.Printf("Long Flag:%v ", t.LongFlag)
	fmt.Printf("Remainder:%v\n", t.Remainder)
}

- - -
Name:run Type:text Valid:run Required:true Short Flag:false Long Flag:false Remainder:false
Name:speed Type:string Valid:* Required:true Short Flag:false Long Flag:false Remainder:false
Name:distance Type:string Valid:* Required:false Short Flag:false Long Flag:false Remainder:false
Name:jump Type:string Valid:* Required:false Short Flag:false Long Flag:true Remainder:false

Parser

args, shortFlags, longFlags := matcher.Parser("run far away --jump=high")
for i, arg := range args {
	fmt.Printf("Arg %v: %v\n", i, arg)
}
for _, flag := range longFlags {
	fmt.Printf("%v: %v\n", flag.Name, flag.Value)
}

- - -
Arg 0: run
Arg 1: far
Arg 2: away
jump: high

Tokenize

tokens := matcher.Tokenize(" run far away ")
for _, t := range tokens {
	fmt.Println(t)
}

- - -
run
far
away

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Matcher

func Matcher(mask string, input string) (match bool, command string, values map[string]string)

Matcher func

func Parser

func Parser(input string) (args []string, shortFlags []Flag, longFlags []Flag)

Parser function

func Tokenize

func Tokenize(input string) (tokens []string)

Tokenize func

Types

type Flag

type Flag struct {
	Name  string
	Value string
}

Flag struct

type Mask

type Mask struct {
	Name      string
	Type      string
	Valid     string
	Required  bool
	ShortFlag bool
	LongFlag  bool
	Remainder bool
}

Mask struct

func Masker

func Masker(input string) (tokens []Mask)

Masker function

Jump to

Keyboard shortcuts

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