cmdopt

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: MIT Imports: 8 Imported by: 8

README

cmdopt Go license codecov PkgGoDev Go version

cmdopt 命令行选项的增强,可以轻松处理子命令。高度重用 flag 包。

opt := &cmdopt.New(...)

// 子命令 build,为一个 flag.FlagSet 实例
build := opt.New("build", func(output io.Writer)error{
    output.Write([]byte("build"))
})

// 子命令 install
install := opt.New("install", func(output io.Writer)error{
    output.Write([]byte("install"))
})

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package cmdopt 用于创建子命令功能的命令行

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdOpt

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

CmdOpt 带子命令的命令行操作

func New

func New(output io.Writer, errorHandling flag.ErrorHandling, usageTemplate string, cmd CommandFunc, notFound func(string) string) *CmdOpt

New 声明带有子命令的命令行处理对象

output 表示命令行信息的输出通道;

errorHandling 表示出错时的处理方式;

cmd 非子命令的参数设定,可以为空;

usageTemplate 命令行的文字说明模板,包含了以下几个占位符:

  • {{flags}} 参数说明,输出时被参数替换,如果没有可以为空;
  • {{commands}} 子命令说明,输出时被子命令列表替换,如果没有可以为空;

notFound 表示找不到子命令时需要返回的文字说明,若为空,则采用 usageTemplate 处理后的内容;

func (*CmdOpt) Command added in v0.10.0

func (opt *CmdOpt) Command(name string) (title, usage string, found bool)

Command 返回指定的命令的说明

func (*CmdOpt) Commands

func (opt *CmdOpt) Commands() []string

Commands 返回所有的子命令

func (*CmdOpt) Exec

func (opt *CmdOpt) Exec(args []string) error

Exec 执行命令行程序

args 参数列表,不包含应用名称,比如 os.Args[1:]。

func (*CmdOpt) Help

func (opt *CmdOpt) Help(name, title, usage string)

Help 注册 help 子命令

func (*CmdOpt) New

func (opt *CmdOpt) New(name, title, usage string, cmd CommandFunc)

New 注册一条新的子命令

name 为子命令的名称,必须唯一; cmd 为该条子命令执行的函数体; usage 为该条子命令的帮助内容。可以包含 {{flags}} 占位符,表示参数信息。

type CommandFunc added in v0.10.0

type CommandFunc = func(FlagSet) DoFunc

CommandFunc 子命令的初始化方法

FlagSet 可用于绑定各个命令行参数; 返回值 DoFunc 表示实际执行的函数;

type DoFunc

type DoFunc = func(io.Writer) error

type FlagSet added in v0.8.0

type FlagSet interface {
	Arg(i int) string
	Args() []string
	Bool(name string, value bool, usage string) *bool
	BoolVar(p *bool, name string, value bool, usage string)
	Duration(name string, value time.Duration, usage string) *time.Duration
	DurationVar(p *time.Duration, name string, value time.Duration, usage string)
	Float64(name string, value float64, usage string) *float64
	Float64Var(p *float64, name string, value float64, usage string)
	Func(name, usage string, fn func(string) error)
	Int(name string, value int, usage string) *int
	Int64(name string, value int64, usage string) *int64
	Int64Var(p *int64, name string, value int64, usage string)
	IntVar(p *int, name string, value int, usage string)
	Lookup(name string) *flag.Flag
	NArg() int
	NFlag() int
	Name() string
	Output() io.Writer
	Set(name, value string) error
	String(name string, value string, usage string) *string
	StringVar(p *string, name string, value string, usage string)
	TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)
	Uint(name string, value uint, usage string) *uint
	Uint64(name string, value uint64, usage string) *uint64
	Uint64Var(p *uint64, name string, value uint64, usage string)
	UintVar(p *uint, name string, value uint, usage string)
	Var(value flag.Value, name string, usage string)
	Visit(fn func(*flag.Flag))
	VisitAll(fn func(*flag.Flag))
}

FlagSet 这是 flag.FlagSet 的子集

Jump to

Keyboard shortcuts

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