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

https://github.com/hashibuto/artillery

An interactive CLI and command parser for golang
https://github.com/hashibuto/artillery

cli command-line go golang interactive repl shell

Last synced: 9 months ago
JSON representation

An interactive CLI and command parser for golang

Awesome Lists containing this project

README

          

This repository has been archived, please consider the project's successor: https://github.com/hashibuto/commander

# artillery
An interactive shell/REPL and CLI parser for golang

Artillery can function both as an interactive shell or REPL, as well as a single command CLI parser. In interactive mode, it uses [NilShell](https://github.com/hashibuto/nilshell) for the command line editor, completion, and history.

## A basic interactive shell/REPL

```
import (
"github.com/hashibuto/artillery"
)

processor := artillery.NewProcessor()

cmds := []*artillery.Command{
&artillery.Command{
Name: "hello",
Description: "prints hello to the console",
Arguments: []*artillery.Argument{
{
Name: "name",
Description: "name of the person to which to say hello",
},
},
Options: []*artillery.Option{
{
Name: "shout",
Description: "shout mode",
ShortName: 's',
Type: artillary.Bool
Value: true,
},
},
OnExecute: func(ns artillery.Namespace, processor *artillery.Processor) error {
var args struct {
Name string
Shout bool
}
err := artillery.Reflect(ns, &args)
if err != nil {
return err
}

message := fmt.Sprintf("hello %s!", args.Name)
if args.Shout {
message = strings.ToUpper(message)
}
return nil
},
},
}

for _, c := range cmds {
err := processor.AddCommand(c)
if err != nil {
log.Fatal(err)
}
}

processor.Shell().ReadUntilTerm()
```

## Parse a single CLI command (non-interactive)

```
import (
"github.com/hashibuto/artillery"
"os"
)

helloCmd := &artillery.Command{
Name: "hello",
Description: "prints hello to the console",
Arguments: []*artillery.Argument{
{
Name: "name",
Description: "name of the person to which to say hello",
},
},
Options: []*artillery.Option{
{
Name: "shout",
Description: "shout mode",
ShortName: 's',
Type: artillary.Bool
Value: true,
},
},
OnExecute: func(ns artillery.Namespace, processor *artillery.Processor) error {
var args struct {
Name string
Shout bool
}
err := artillery.Reflect(ns, &args)
if err != nil {
return err
}

message := fmt.Sprintf("hello %s!", args.Name)
if args.Shout {
message = strings.ToUpper(message)
}
return nil
},
}

processor := artillery.NewProcessor()
processor.RemoveBuiltins(false)
err := processor.AddCommands(helloCmd)
if err != nil {
panic(err)
}

err = processor.Process(os.Args[1:])
```

## Special commands / keystrokes
- `clear` clears the terminal
- `!` execs the command ie `!cat /home/user/something` for bash do `!bash -c "cat /home/user/something | grep whatever"`
- `exit` exits
- `` reverse search
- `` move up backwards through the command history
- `` move forwards through the command history
- `` exit
- `` autocomplete

## Example
[Example showcasing most features](https://github.com/hashibuto/artillery/blob/master/example/main.go)