Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mmcquillan/matcher

A library for parsing and matching based on a mask for use with CLI or Bots.
https://github.com/mmcquillan/matcher

bot cli golang

Last synced: 2 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# Matcher

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

## Mask Rules

- `xyz` text 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 [distance]` | `run fast` | true |
| `run [distance]` | `run fast far` | true |
| `run [distance]` | `run fast far forever` | false |
| `run [distance]` | `run fast "far forever"` | true |
| `run [distance] <-j>` | `run fast far -j` | true |
| `run [distance] <-j>` | `run fast far` | false |
| `run [distance] <--jump>` | `run fast far --jump=high` | true |
| `run [distance] <--jump>` | `run fast far` | false |
| `run [distance] [--jump]` | `run fast far --jump=high` | true |
| `run [distance] [--jump]` | `run fast far` | true |
| `run [distance] [--jump]` | `run fast far --skip` | false |
| `run [distance] [--]` | `run fast --jump --skip` | true |
| `run [song...]` | `run Welcome to the Jungle` | true |
| `run ` | `run fast` | true |
| `run ` | `run fast` | true |
| `run ` | `run quickly` | false |
| `run ` | `run 6` | true |
| `run ` | `run 6` | true |
| `run ` | `run 3` | false |
| `run <--jump(bool)>` | `run --jump` | true |
| `run <--jump(bool)>` | `run --jump=false` | true |

## Matcher

```
match, command, values := matcher.Matcher("run [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 [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
```