gocop

package module
v0.0.0-...-1706c58 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2016 License: MIT Imports: 8 Imported by: 3

README

go-cop

Go Report Card Build Status GoDoc

GO - Command Parser

This is a library that tries to make it conveient to add commands to your go-program. It is dependant on the excilent liner library, for handling the tty, history, emacs-keys and more.

What gocop adds on top is a small parser to sugest autocomplete and invoke commands easily. History/autosugest is content aware, so default arguments with the same name will share sugestions, but you can also create custom sugestions. For example, email addresses might be sugested from an address book, or reply might be sugested from messages recived.

Status

Verion 0.0 Still in very early stage. Everything might change.

Usage

The basic usage will be to create a 'world' struct, and hook commands, and arguments onto it. Arguments can be optional or greedy.

Arguments are separated by whitespace, so if you need to send an argument with spaces or tabs, you need to eighter backslash the whitespace, or put the string in single or double quotes.

Bugs / Todo

  • Linebreak during quoted strings does not allow you to continue on next line

  • Make easier to use

  • Create examples

  • Make 'worlds' swappable, so commands can control following states.

  • and more...

Documentation

Overview

GO-COP is a utility to add functions easy to use from the tty. Autocomplete sugestions is context aware, and the command structure is quite liberal, meening that multiple optional or greedy arguments are allowed, though not encurraged.

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultResultHandler = func(in interface{}, err error) {
	if err != nil {
		fmt.Print("\x1b[0;31m")
		fmt.Print(err)
	} else {
		fmt.Print("\x1b[0;35m")
		fmt.Printf("%+v\n", in)
	}
}

Functions

This section is empty.

Types

type AcInvoker

type AcInvoker interface {
	Invoke(assignment *argNodeAssignment, context RunContext)
}

Interface for invoking a command, and populate the arguments

type AcInvokerFn

type AcInvokerFn func(assignment *argNodeAssignment, context RunContext)

A function that registers and applies the value on resultMap

func (AcInvokerFn) Invoke

func (aif AcInvokerFn) Invoke(assignment *argNodeAssignment, context RunContext)

Implement AcInvoker interface

type AcSugestor

type AcSugestor interface {
	Sugest(node *ArgNode, in TokenSet) []string
}

Interface for autocomplete sugestions

type AcSugestorFn

type AcSugestorFn func(node *ArgNode, in TokenSet) []string

Autocomplete sugestion. Returns a slice of possible sugestions

func (AcSugestorFn) Sugest

func (asf AcSugestorFn) Sugest(node *ArgNode, in TokenSet) []string

Implement AcSugestor interface

type ArgNode

type ArgNode struct {
	Name      string
	Descr     string
	Children  []*ArgNode
	TypeFlags NodeTypeFlags
	AcSugestorFn
	AcInvokerFn

	RunHandler
	// contains filtered or unexported fields
}

func NewWorldNode

func NewWorldNode() *ArgNode

func (*ArgNode) AddArgument

func (an *ArgNode) AddArgument(name string) *ArgNode

func (*ArgNode) AddCustomNode

func (an *ArgNode) AddCustomNode(name string, acsFn AcSugestorFn, aciFn AcInvokerFn, apFn acceptPermutationsFn, typeFlags NodeTypeFlags) *ArgNode

func (*ArgNode) AddSubCommand

func (an *ArgNode) AddSubCommand(name string) *ArgNode

func (*ArgNode) Description

func (an *ArgNode) Description(str string) *ArgNode

func (*ArgNode) Handler

func (an *ArgNode) Handler(rhf RunHandlerFunc) *ArgNode

func (*ArgNode) InvokeCommand

func (an *ArgNode) InvokeCommand(input string, rc RunContext) (res interface{}, err error)

func (*ArgNode) Optional

func (an *ArgNode) Optional() *ArgNode

func (*ArgNode) SugestAutoComplete

func (an *ArgNode) SugestAutoComplete(in TokenSet) []string

func (*ArgNode) Times

func (an *ArgNode) Times(min, max uint64) *ArgNode

func (*ArgNode) Usage

func (an *ArgNode) Usage(prfix, descpr string) (ret []string)

func (*ArgNode) Weight

func (an *ArgNode) Weight(ts TokenSet) int

type CommandParser

type CommandParser struct {
	SugestionProvider SugestionProvider
	ResultHandler     ResultHandlerFn
	// contains filtered or unexported fields
}

func NewCommandParser

func NewCommandParser() *CommandParser

func (*CommandParser) AddStandardCommands

func (cp *CommandParser) AddStandardCommands(an *ArgNode)

func (*CommandParser) AutoCompleter

func (cp *CommandParser) AutoCompleter(line string) (c []string)

func (*CommandParser) MainLoop

func (cp *CommandParser) MainLoop() (err error)

func (*CommandParser) NewRunContext

func (cp *CommandParser) NewRunContext() RunContext

func (*CommandParser) NewWorld

func (cp *CommandParser) NewWorld() *ArgNode
Example
package main

import (
	"log"
	"testing"
)

var _ testing.T

func main() {
	cp := NewCommandParser()

	// New world. IE, root node
	world := cp.NewWorld()
	// Add command1 with mandatory argument
	world.AddSubCommand("command1").AddArgument("argument")
	// Add command2 with optional argument
	world.AddSubCommand("command2").AddArgument("argument").Optional()

	// Print usage
	for _, u := range world.Usage("\t", "\t\t") {
		log.Print(u)
	}
}
Output:

type DefaultRunContext

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

func (*DefaultRunContext) Get

func (drc *DefaultRunContext) Get(name string) string

func (*DefaultRunContext) Handler

func (drc *DefaultRunContext) Handler(h RunHandler)

func (*DefaultRunContext) Invoke

func (drc *DefaultRunContext) Invoke() (interface{}, error)

func (*DefaultRunContext) Put

func (drc *DefaultRunContext) Put(name, value string)

func (*DefaultRunContext) SugestionProvider

func (drc *DefaultRunContext) SugestionProvider() SugestionProvider

type InvalidArgument

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

func (*InvalidArgument) Error

func (ia *InvalidArgument) Error() string

type NodeTypeFlags

type NodeTypeFlags uint64
const (
	WorldNode NodeTypeFlags = 1 << iota
	CommandNode
	ArgumentNode
	OptionalNode
	MultiArgNode
)

type ResultHandlerFn

type ResultHandlerFn func(interface{}, error)

type RunContext

type RunContext interface {
	Put(name, value string)
	Get(name string) string

	SugestionProvider() SugestionProvider

	Handler(rh RunHandler)
	Invoke() (interface{}, error)
}

func DefaultRunContextProvider

func DefaultRunContextProvider(cp *CommandParser) RunContext

type RunContextProviderFn

type RunContextProviderFn func(cp *CommandParser) RunContext

type RunHandler

type RunHandler interface {
	HandleCommand(rc RunContext) (interface{}, error)
}

type RunHandlerFunc

type RunHandlerFunc func(rc RunContext) (interface{}, error)

func (RunHandlerFunc) HandleCommand

func (rh RunHandlerFunc) HandleCommand(rc RunContext) (interface{}, error)

type SugestionProvider

type SugestionProvider struct {
}

type Token

type Token struct {
	Type TokenType // Type
	// contains filtered or unexported fields
}

Scanned token

func (*Token) IsWhitespace

func (t *Token) IsWhitespace() bool

func (*Token) ToString

func (t *Token) ToString() string

Returns the string-value. If it is a quoted string, then omit the quotes

type TokenSet

type TokenSet []Token

func Tokenize

func Tokenize(input string) (tokens TokenSet)

func (TokenSet) Filter

func (ts TokenSet) Filter(keep TokenType) TokenSet
Example
tokens := Tokenize("\"A \"'input' string")

log.Print("Initial tokens: ", tokens)

log.Print("Filtered on not whitespace: ", tokens.Filter(TokenNoWhitespace))

log.Print("Filtered on single or double quoted string: ", tokens.Filter(TokenSQuoted|TokenDQuoted))
Output:

func (TokenSet) HasText

func (ts TokenSet) HasText() bool

Checks if the set contains printable characters

func (TokenSet) StartsWithIgnoreCase

func (ts TokenSet) StartsWithIgnoreCase(cmp string) bool

func (TokenSet) String

func (ts TokenSet) String() string

func (TokenSet) Stringify

func (ts TokenSet) Stringify() string

Returns the string, striped of head and trail whitespaces.

func (TokenSet) Trimmed

func (ts TokenSet) Trimmed() TokenSet

Returns a slice without leading or trailing whitespaces

type TokenType

type TokenType uint64
const (
	TokenError TokenType = 1 << iota
	TokenEOF
	TokenString
	TokenDQuoted
	TokenSQuoted
	TokenWhitespace

	TokenNoWhitespace  = TokenString | TokenDQuoted | TokenSQuoted
	TokenAllWhitespace = TokenEOF | TokenWhitespace
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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