console

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: BSD-3-Clause Imports: 10 Imported by: 10

README

Console application

Сreating console application

import "github.com/deweppro/go-app/console"

// creating an instance of the application, 
// specifying its name and description for flag: --help 
root := console.New("tool", "help tool")
// adding one or more commands
root.AddCommand(...)
// launching the app
root.Exec()

Creating a simple command

import "github.com/deweppro/go-app/console"
// creating a new team with settings
console.NewCommand(func(setter console.CommandSetter) {
	// passing the command name and description
    setter.Setup("simple", "first-level command")
    // description of the usage example
    setter.Example("simple aa/bb/cc -a=hello -b=123 --cc=123.456 -e")
    // description of flags
    setter.Flag(func(f console.FlagsSetter) {
    	// you can specify the flag's name, default value, and information about the flag's value.
        f.StringVar("a", "demo", "this is a string argument")
        f.IntVar("b", 1, "this is a int64 argument")
        f.FloatVar("cc", 1e-5, "this is a float64 argument")
        f.Bool("d", "this is a bool argument")
    })
    // argument validation: specifies the number of arguments, 
    // and validation function that should return 
    // value after validation and validation error
    setter.Argument(1, func(s []string) ([]string, error) {
        if !strings.Contains(s[0], "/") {
            return nil, fmt.Errorf("argument must contain /")
        }
        return strings.Split(s[0], "/"), nil
    })
    // command execution function
    // first argument is a slice of arguments from setter.Argument
    // all subsequent arguments must be in the same order and types as listed in setter.Flag
    setter.ExecFunc(func(args []string, a string, b int64, c float64, d bool) {
        fmt.Println(args, a, b, c, d)
    })
}),
example of execution results

go run main.go --help

Usage: 
  tool  [command] [args]

Available Commands:
  simple    first-level command

_____________________________________________________
Use flag --help for more information about a command.

go run main.go simple --help

Usage: 
  tool simple [arg]  -a=demo -b=1 --cc=1e-05 -d

Flags:
  -a     this is a string argument (default: demo)
  -b     this is a int64 argument (default: 1)
  --cc    this is a float64 argument (default: 1e-05)
  -d     this is a bool argument (default: true)


Examples:
  tool simple aa/bb/cc -a=hello -b=123 --cc=123.456 -e

Creating multi-level command tree

To create a multi-level command tree, you need to add the child command to the parent via the AddCommand method.

At the same time, in the parent command, it is enough to specify only the name and description via the Setup method.

root := console.New("tool", "help tool")

simpleCmd := console.NewCommand(func(setter console.CommandSetter) {
    setter.Setup("simple", "third level")
    ....
})

twoCmd := console.NewCommand(func(setter console.CommandSetter) {
    setter.Setup("two", "second level")
    setter.AddCommand(simpleCmd)
})

oneCmd := console.NewCommand(func(setter console.CommandSetter) {
    setter.Setup("one", "first level")
    setter.AddCommand(twoCmd)
})

root.AddCommand(oneCmd)
root.Exec()
example of execution results

go run main.go --help

Usage: 
  tool  [command] [args]

Available Commands:
  one    first level

_____________________________________________________
Use flag --help for more information about a command.

go run main.go one --help

Usage: 
  tool one [command] [args]

Available Commands:
  two    second level

_____________________________________________________
Use flag --help for more information about a command.

go run main.go one two --help

Usage: 
  tool one two [command] [args]

Available Commands:
  simple    third level

_____________________________________________________
Use flag --help for more information about a command.

go run main.go one two simple --help

Usage: 
  tool one two simple [arg]  -a=demo -b=1 --cc=1e-05 -d

Flags:
  -a     this is a string argument (default: demo)
  -b     this is a int64 argument (default: 1)
  --cc    this is a float64 argument (default: 1e-05)
  -d     this is a bool argument (default: true)


Examples:
  tool simple aa/bb/cc -a=hello -b=123 --cc=123.456 -e

Documentation

Index

Constants

View Source
const (
	ANSI_RESET  = "\u001B[0m"
	ANSI_BLACK  = "\u001B[30m"
	ANSI_RED    = "\u001B[31m"
	ANSI_GREEN  = "\u001B[32m"
	ANSI_YELLOW = "\u001B[33m"
	ANSI_BLUE   = "\u001B[34m"
	ANSI_PURPLE = "\u001B[35m"
	ANSI_CYAN   = "\u001B[36m"
)

nolint: golint

Variables

This section is empty.

Functions

func Debugf

func Debugf(msg string, args ...interface{})

func Errorf

func Errorf(msg string, args ...interface{})

func FatalIfErr

func FatalIfErr(err error, msg string, args ...interface{})

func Fatalf

func Fatalf(msg string, args ...interface{})

func Infof

func Infof(msg string, args ...interface{})

func Input

func Input(msg string, vars []string, def string) string

Input console input request

func InputBool

func InputBool(msg string, def bool) bool

InputBool console bool input request

func ShowDebug

func ShowDebug(ok bool)

func Warnf

func Warnf(msg string, args ...interface{})

Types

type Arg

type Arg struct {
	Key   string
	Value string
}

type ArgGetter

type ArgGetter interface {
	Has(name string) bool
	Get(name string) *string
}

type Args

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

func NewArgs

func NewArgs() *Args

func (*Args) Get

func (a *Args) Get(name string) *string

func (*Args) Has

func (a *Args) Has(name string) bool

func (*Args) Next

func (a *Args) Next() []string

func (*Args) Parse

func (a *Args) Parse(list []string) *Args

type Argument

type Argument struct {
	Count     int
	ValidFunc ValidFunc
}

func NewArgument

func NewArgument() *Argument

type Command

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

func (*Command) AddCommand

func (c *Command) AddCommand(getter ...CommandGetter)

func (*Command) ArgCall

func (c *Command) ArgCall(d []string) ([]string, error)

func (*Command) ArgCount

func (c *Command) ArgCount() int

func (*Command) Argument

func (c *Command) Argument(count int, call ValidFunc)

func (*Command) Call

func (c *Command) Call() interface{}

func (*Command) Description

func (c *Command) Description() string

func (*Command) Example

func (c *Command) Example(s string)

func (*Command) Examples

func (c *Command) Examples() []string

func (*Command) ExecFunc

func (c *Command) ExecFunc(i interface{})

func (*Command) Flag

func (c *Command) Flag(cb func(FlagsSetter))

func (*Command) Flags

func (c *Command) Flags() FlagsGetter

func (*Command) Is

func (c *Command) Is(s string) bool

func (*Command) Name

func (c *Command) Name() string

func (*Command) Next

func (c *Command) Next() []CommandGetter

func (*Command) Setup

func (c *Command) Setup(name, description string)

func (*Command) Validate

func (c *Command) Validate() error

type CommandGetter

type CommandGetter interface {
	Next() []CommandGetter
	Validate() error
	Is(string) bool
	Name() string
	Description() string
	Examples() []string
	ArgCount() int
	ArgCall(d []string) ([]string, error)
	Flags() FlagsGetter
	Call() interface{}
	AddCommand(...CommandGetter)
}

func NewCommand

func NewCommand(cb func(CommandSetter)) CommandGetter

type CommandSetter

type CommandSetter interface {
	Setup(string, string)
	Example(string)
	Flag(cb func(FlagsSetter))
	Argument(count int, call ValidFunc)
	ExecFunc(interface{})
	AddCommand(...CommandGetter)
}

type Console

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

func New

func New(name, description string) *Console

func (*Console) AddCommand

func (c *Console) AddCommand(getter ...CommandGetter)

func (*Console) Exec

func (c *Console) Exec()

type FlagItem

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

type Flags

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

func NewFlags

func NewFlags() *Flags

func (*Flags) Bool

func (f *Flags) Bool(name string, usage string)

func (*Flags) Call

func (f *Flags) Call(g IOArgsGetter, cb func(interface{})) error

func (*Flags) Count

func (f *Flags) Count() int

func (*Flags) Float

func (f *Flags) Float(name string, usage string)

func (*Flags) FloatVar

func (f *Flags) FloatVar(name string, value float64, usage string)

func (*Flags) Info

func (f *Flags) Info(cb func(req bool, name string, v interface{}, usage string))

func (*Flags) Int

func (f *Flags) Int(name string, usage string)

func (*Flags) IntVar

func (f *Flags) IntVar(name string, value int64, usage string)

func (*Flags) String

func (f *Flags) String(name string, usage string)

func (*Flags) StringVar

func (f *Flags) StringVar(name string, value string, usage string)

type FlagsGetter

type FlagsGetter interface {
	Info(cb func(bool, string, interface{}, string))
	Call(g IOArgsGetter, cb func(interface{})) error
}

type FlagsSetter

type FlagsSetter interface {
	StringVar(name string, value string, usage string)
	String(name string, usage string)
	IntVar(name string, value int64, usage string)
	Int(name string, usage string)
	FloatVar(name string, value float64, usage string)
	Float(name string, usage string)
	Bool(name string, usage string)
}

type IOArgsGetter

type IOArgsGetter interface {
	Has(name string) bool
	Get(name string) *string
}

type ValidFunc

type ValidFunc func([]string) ([]string, error)

Jump to

Keyboard shortcuts

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