cl

package module
v0.0.0-...-4637ae4 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2018 License: MIT Imports: 5 Imported by: 0

README

CL

GoDoc

This small and simple library allows the creation of commands and execution of those commands when a line of input matches the command.

An simple example:

package main

import (
	"bufio"
	"fmt"
	"os"

	"bitbucket.org/antizealot1337/cl"
)

func main() {
	// Create a command set
	cs := cl.NewCommandSet()

	// Add a command
	cs.Add("hello", ExecFn(func(args []string) error {
		fmt.Println("Hello, world!")
		return nil
	}))

	// Create a scanner from standard input
	scanner := bufio.NewScanner(os.Stdin)

  // Scan the input
  for scanner.Scan() {
    // Print something so the user knowns the program expects input
    fmt.Print("> ")

    // Get the line
    line := scanner.Text()

    // Check for a command
    found, err := cs.CheckLine(line)

    // Check for an error
    if err != nil {
      panic(err)
    } //if

    // Check if the command was found
    if !found {
      fmt.Printf("\"%s\" does not contain a command.\n", line)
    } //if
  } //for
} //main

The output would be:

> nocmd
"nocmd" does not contain a command.
> hello
Hello, World!
>

With the new Process function it can be simplified to:

package main

import (
	"fmt"
	"os"

	"bitbucket.org/antizealot1337/cl"
)

func main() {
	// Create a command set
	cs := cl.NewCommandSet()

	// Add a command
	cs.Add("hello", ExecFn(func(args []string) error {
		fmt.Println("Hello, world!")
		return nil
	}))

	// Process the line
	if err := cl.Process("> ", cs, os.Stdin); err != nil {
		painc(err)
	} //if
} //main

A few things to note

  • Commands are case sensitive. The commands "go", "Go", "gO", and "GO" are all different commands. This may change in the future.
  • Commands must be typed in full. A command named "compile" cannot be called using input "comp". This may change in the future.
  • It is up to the cl.Executor to check for args. The cl.CommandSet does not do any pre-checking for specific arguments.

Licensing

All code in this repository is licensed under the terms of the MIT license unless other wise specified. View the LICENSE file for terms of the license.

Copyright 2016, 2017

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Process

func Process(prompt string, c CommandSet, in io.Reader) error

Process is a utility method that will read lines from a Reader and provices it to a CommandSet. Errors reported from the Executors in the CommandSet are expected to be fatal.

Types

type CommandSet

type CommandSet struct {
	// contains filtered or unexported fields

} //strutct

CommandSet is a set of commands.

func NewCommandSet

func NewCommandSet(partial bool) *CommandSet

NewCommandSet creates a new CommandSet. The commands can either be determined strictly or partially from the command line via the provided bool.

func (*CommandSet) Add

func (c *CommandSet) Add(command string, action Executor)

Add an Executor to the CommandSet.

func (CommandSet) CheckLine

func (c CommandSet) CheckLine(line string) (bool, error)

CheckLine checks if the can be executed. If not false is returned. If the line is associated with a command the command will be executed and true returned. If the command encounters an error or parsing the line results in and error, it will be returned.

type ExecFn

type ExecFn func(args []string) error

ExecFn is a function that implements the Executor interface.

func (ExecFn) Exec

func (e ExecFn) Exec(args []string) error

Exec executes the function.

type Executor

type Executor interface {
	Exec(args []string) error

} //interface

Executor represents a command that can be executed.

Jump to

Keyboard shortcuts

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