cli

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 7 Imported by: 0

README

cli-go

中文帮助
a simple library to build golang command line (cli / cmd)apps

go get github.com/seastart/cli-go

concepts

./app [-main_opt1=1] [command] [-cmd_opt1=1] [-cmd_opt2=2] [subcommands/args]

app is the application
command is sub command, no prefix -
opt is options, start with -
one app may have some main options, such as environment config, like main_opt1 of above example
one app may have some commands, like command of above example
one command may have some command options, like cmd_opt1 cmd_opt2 of above example
one command may have some subcommands(arguments), like subcommands of above example

steps (3 step)

  • app := cli.NewCliApp
  • app.AddCommand
  • app.Run

examples

./main
./main -start=2
./main
./main test -start=2
./main list -page=3
./main
./main test live
./main test -start=2 live
// set default env and then run list command
./main -env=prod list -page=3

default help command

each app have a default help command

./main help

TODO

  • support i18n

Documentation

Index

Constants

View Source
const CODE_ERROR_COMMON = 100
View Source
const CODE_SUCCESS = 0

成功执行

Variables

This section is empty.

Functions

This section is empty.

Types

type CliApp

type CliApp struct {
	*Command // the self root command
}

app

func NewCliApp

func NewCliApp(desc string, opts ...*Option) *CliApp

实例化一个描述为desc,根参数为opts的cli app

func NewCliWholeApp added in v0.0.2

func NewCliWholeApp(desc string, handler Handler, opts ...*Option) *CliApp

实例化一个描述为desc,根参数为opts,且没有子命令的cli app

func (*CliApp) Errorf

func (app *CliApp) Errorf(format string, info ...interface{})

展示错误信息到stderr

func (*CliApp) Exitf

func (app *CliApp) Exitf(code int, format string, info ...interface{})

以状态码code(0代表成功)退出并展示信息

func (*CliApp) Infof

func (app *CliApp) Infof(format string, info ...interface{})

展示提示信息到stdout

func (*CliApp) Run

func (app *CliApp) Run(args ...string) (err error)

解析并运行

func (*CliApp) Successf

func (app *CliApp) Successf(format string, info ...interface{})

展示成功信息到stdout

func (*CliApp) Warningf

func (app *CliApp) Warningf(format string, info ...interface{})

展示告警信息到stdout

type Command

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

command

func (*Command) AddCommand added in v0.0.2

func (cmd *Command) AddCommand(name string, desc string, handler Handler, opts ...*Option) *Command

创建一个command,handler里options为解析后的参数(-开头)

func (*Command) App added in v0.0.2

func (cmd *Command) App() *CliApp

获取app

func (*Command) OptVal added in v0.0.2

func (cmd *Command) OptVal(name string) (val reflect.Value)

获取option值

func (*Command) OptValE added in v0.0.2

func (cmd *Command) OptValE(name string) (val reflect.Value, err error)

获取option值

func (*Command) ParentCommand added in v0.0.2

func (cmd *Command) ParentCommand() *Command

获取父command

func (*Command) ShowHelp added in v0.0.2

func (cmd *Command) ShowHelp()

展示帮助

func (*Command) SubCommand added in v0.0.2

func (cmd *Command) SubCommand(name string) *Command

获取子command

type Handler added in v0.0.2

type Handler func(cmd *Command, remaincmds []string) (err error)

command执行

type Option

type Option struct {
	Name string      // 参数名
	Dft  interface{} // 参数默认值
	Desc string      // 参数说明
	// contains filtered or unexported fields
}

option

func (*Option) GetVal

func (opt *Option) GetVal() reflect.Value

获取解析出的值

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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