https://github.com/lukasl-dev/clark
simple command-lexer for chat systems like discord, teamspeak or slack
https://github.com/lukasl-dev/clark
cli command go golang lexer
Last synced: about 1 year ago
JSON representation
simple command-lexer for chat systems like discord, teamspeak or slack
- Host: GitHub
- URL: https://github.com/lukasl-dev/clark
- Owner: lukasl-dev
- License: apache-2.0
- Created: 2020-12-17T20:46:28.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-05T10:58:16.000Z (over 3 years ago)
- Last Synced: 2024-06-20T22:37:28.535Z (about 2 years ago)
- Topics: cli, command, go, golang, lexer
- Language: Go
- Homepage: https://pkg.go.dev/github.com/lukasl-dev/clark
- Size: 74.2 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# clark
- [clark](#clark)
- [What is `clark`?](#what-is-clark)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Install the package](#install-the-package)
- [Install the CLI](#install-the-cli)
- [Getting started](#getting-started)
- [Create a Reader](#create-a-reader)
- [Create a Lexer](#create-a-lexer)
- [Available Options](#available-options)
- [Start the lexing progress](#start-the-lexing-progress)
- [Collect the lexed Tokens](#collect-the-lexed-tokens)
- [Usage of the CLI](#usage-of-the-cli)
---
## What is `clark`?
`Clark` is a simple command-lexing package designed for chat systems such as Discord, Teamspeak, or Telegram. It is
therefore intended to facilitate the handling of user input and the parsing processes that follow.
---
## Prerequisites
To use a Go package such as `clark`, you must of course have Go installed on your system.
It is assumed that you have already worked with the Go environment. If this is not the case,
see [this page first](https://golang.org/doc/install).
---
## Installation
### Install the package
To use `clark` as a Go package, you must have it installed on your current system. If this is not the case, run the
command below.
```console
go get -u github.com/lukasl-dev/clark
```
### Install the CLI
Otherwise, if you want to use `clark` inside your terminal, you need to have it installed as well. If this is not the
case, run the command below.
If you have no experience in this section,
read [this](https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies).
```console
go get -u github.com/lukasl-dev/clark/cmd/clark
```
---
## Getting started
### Create a [Reader](https://pkg.go.dev/io#Reader)
As an example, we create a `strings.Reader` here.
```go
reader := strings.NewReader("!help")
```
### Create a [Lexer](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#Lexer)
```go
lex, err := clark.NewLexer(reader)
```
You can configure the [lexer](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#Lexer) using the
variadic [options](https://pkg.go.dev/github.com/lukasl-dev/clark#Option).
See [available Options](#available-options).
```go
lex, err := clark.NewLexer(
reader,
lexing.Prefixes("!"), lexing.Labels("help", "play", "help me"),
)
// handle error
```
#### Available [Options](https://pkg.go.dev/github.com/lukasl-dev/clark#Option)
| Function | Description |
| :-------------------------------------------------------------------------------------------------- | -------------------------------------------------------: |
| [`lexing.Prefixes`](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#Prefixes) | Prefixes defines the prefixes used by the lexer. |
| [`lexing.Labels`](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#Labels) | Labels defines the labels used by the lexer. |
| [`lexing.PrefixIgnoreCase`](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#PrefixIgnoreCase) | PrefixIgnoreCase defines the prefixes case sensitivity. |
| [`lexing.LabelIgnoreCase`](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#LabelIgnoreCase) | LabelIgnoreCase defines the labels case sensitivity. |
| [`lexing.SkipPrefix`](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#SkipPrefix) | SkipPrefix defines whether the prefix should be skipped. |
| [`lexing.SkipLabel`](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#SkipLabel) | SkipLabel defines whether the label should be skipped. |
### Start the lexing progress
To execute the [Lexer](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing#Lexer)
, `lex.Lex()` is executed as a goroutine. `lex.Lex()` reads the passed reader lexicographically until `io.EOF` and
returns the lexed [tokens](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing/token#Token).
```go
go lex.Lex()
```
### Collect the lexed [Tokens](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing/token#Token)
During reading [tokens](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing/token#Token) are sent to the token
channel. This channel can be accessed via `lex.Chan()`.
```go
for token := range lex.Chan() {
// handle token
}
```
A [Token](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing/token#Token) has
a [Type](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing/token#Type) and an optional value.
See supported [types](https://pkg.go.dev/github.com/lukasl-dev/clark/lexing/token#Type).
---
## Usage of the CLI