cmd

package
v0.0.0-...-cd938e1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 License: MIT Imports: 7 Imported by: 0

README

cmd

父子命令形式的flag封装.

go内置的flag包封装的足够简单,我们并不希望因为父子命令的需求而引入比较复杂的第三方包, 简单封装一下flag.FlagSet,就足以达到很不错的效果:

父子命令帮助:

$ cd examples/no_inherit
$ ./docker -h
Usage: docker [FLAGS] SUB_COMMAND [FLAGS] [ARGS]

Flags:
  -debug
        Enable debug mode
  -log-level string
        Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "debug")

Commands:
  build Build an image from a Dockerfile
  ps List containers



Run 'docker -help' for more information on a command.

子命令执行

$ ./docker ps mysql_1
ps: [mysql_1] 


以上范例只有一层父子命令,实际可以多层嵌套,比如:

$ ./docker image inspect mysql_1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	Init func(flagSet *flag.FlagSet) error

	Run func(args []string) error

	// 命令名
	Name string

	// 一行形式的Usage
	UsageLine string

	// 短描述.
	Short string

	// 长描述.
	Long string
	// contains filtered or unexported fields
}

一个命令结构体,命令可以是类似docker container build 这样的父子命令.

func Run

func Run(root Cmder, args ...string) (*Cmd, error)

运行命令

docker 必须是跟命令
args 应该传入os.Args, 例: cmd.Run(root,os.Args...)

err 可能为 flag.ErrHelp , err已处理并打印相关信息,外层代码可以不处理

func (*Cmd) Add

func (x *Cmd) Add(cmder Cmder) Cmder

Add

return cmder arg

func (*Cmd) Args

func (x *Cmd) Args() []string

func (*Cmd) DumpNodes

func (x *Cmd) DumpNodes() string

func (*Cmd) DumpTree

func (x *Cmd) DumpTree() string

func (*Cmd) Root

func (x *Cmd) Root() *Cmd

func (*Cmd) String

func (x *Cmd) String() string

type Cmder

type Cmder interface {
	// contains filtered or unexported methods
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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