console

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2022 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 root command
root.RootCommand(...)
// 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.ArgumentFunc(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: false)


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{})

Debugf console message writer for debug level

func Errorf

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

Errorf console message writer for error level

func FatalIfErr

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

FatalIfErr console message writer if err is not nil

func Fatalf

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

Fatalf console message writer with exit code 1

func Infof

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

Infof console message writer for info level

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)

ShowDebug init show debug

func Warnf

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

Warnf console message writer for warning level

Types

type Arg

type Arg struct {
	Key   string
	Value string
}

Arg model

type ArgGetter

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

ArgGetter argument getter interface

type Args

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

Args list model

func NewArgs

func NewArgs() *Args

NewArgs constructor

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 {
	ValidFunc ValidFunc
}

Argument model

func NewArgument

func NewArgument() *Argument

NewArgument constructor

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) ArgumentFunc added in v1.6.0

func (c *Command) ArgumentFunc(call ValidFunc)

func (*Command) AsRoot added in v1.6.0

func (c *Command) AsRoot() CommandGetter

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) IsRoot added in v1.6.0

func (c *Command) IsRoot() bool

func (*Command) List added in v1.6.0

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

func (*Command) Name

func (c *Command) Name() string

func (*Command) Next

func (c *Command) Next(cmd string) 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(string) CommandGetter
	List() []CommandGetter
	Validate() error
	Is(string) bool
	Name() string
	Description() string
	Examples() []string
	ArgCall(d []string) ([]string, error)
	Flags() FlagsGetter
	Call() interface{}
	AddCommand(...CommandGetter)
	AsRoot() CommandGetter
	IsRoot() bool
}

func NewCommand

func NewCommand(cb func(CommandSetter)) CommandGetter

type CommandSetter

type CommandSetter interface {
	Setup(string, string)
	Example(string)
	Flag(cb func(FlagsSetter))
	ArgumentFunc(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()

func (*Console) RootCommand added in v1.6.0

func (c *Console) RootCommand(getter CommandGetter)

type FlagItem

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

FlagItem element of flag model

type Flags

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

Flags model

func NewFlags

func NewFlags() *Flags

NewFlags init new flag

func (*Flags) Bool

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

Bool flag decoder

func (*Flags) Call

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

func (*Flags) Count

func (f *Flags) Count() int

Count of flags

func (*Flags) Float

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

Float flag decoder

func (*Flags) FloatVar

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

FloatVar flag decoder with default value

func (*Flags) Info

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

Info about command

func (*Flags) Int

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

Int flag decoder

func (*Flags) IntVar

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

IntVar flag decoder with default value

func (*Flags) String

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

String flag decoder

func (*Flags) StringVar

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

StringVar flag decoder with default value

type FlagsGetter

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

FlagsGetter getter interface

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)
}

FlagsSetter setter interface

type ValidFunc

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

ValidFunc validate argument interface

Jump to

Keyboard shortcuts

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