Documentation
¶
Index ¶
- type ActionFunc
- type Command
- func (c *Command) Bool(name string) bool
- func (c *Command) Duration(name string) time.Duration
- func (c *Command) Float64(name string) float64
- func (c *Command) Int(name string) int
- func (c *Command) Int64(name string) int64
- func (c *Command) Output() io.Writer
- func (c *Command) PrintUsage() error
- func (c *Command) Program() *Program
- func (c *Command) Run(args []string) error
- func (c *Command) RunContext(ctx context.Context, args []string) error
- func (c *Command) SetOutput(w io.Writer)
- func (c *Command) SetProgram(p *Program)
- func (c *Command) String(name string) string
- func (c *Command) Uint(name string) uint
- func (c *Command) Uint64(name string) uint64
- type Program
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActionFunc ¶
ActionFunc 命令执行函数签名
参数:
- ctx: context.Context,用于传递取消信号和超时控制
- cmd: *Command,包含命令的所有信息(标志集、元数据等)
返回:
- error: 执行错误,nil 表示成功
Example:
initCmd.Action = func(ctx context.Context, cmd *cli.Command) error {
path := cmd.String("path")
fmt.Printf("Initializing project at %s\n", path)
return nil
}
type Command ¶
type Command struct {
Name string // 命令名称(如 "init", "migrate")
Usage string // 命令用途简短描述(一行)
Description string // 命令详细描述(多行)
Flags func(*flag.FlagSet) // 命令标志配置函数
Action ActionFunc // 命令执行函数
HideHelpFlag bool // 是否隐藏 -h 帮助标志
// contains filtered or unexported fields
}
Command 命令结构,代表一个 CLI 命令
Command 封装了命令的元数据(名称、描述)、 标志定义和执行逻辑。
func DefaultVersionCommand ¶
func DefaultVersionCommand() *Command
DefaultVersionCommand 创建默认的 version 命令
func NewCommand ¶
NewCommand 创建新命令
Example:
initCmd := cli.NewCommand("init", "Initialize a new project")
initCmd.Description = "Create a new project with default configuration"
initCmd.Flags = func(fs *flag.FlagSet) {
fs.String("path", ".", "Project path")
}
initCmd.Action = func(ctx context.Context, cmd *cli.Command) error {
path := cmd.String("path")
fmt.Printf("Initializing project at %s\n", path)
return nil
}
func (*Command) Bool ¶
Bool 获取布尔类型的 flag 值
从合并后的 flags 中查找(包含全局 flags 和命令 flags)。 如果未找到,返回 false。
Example:
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
verbose := cmd.Bool("verbose")
if verbose {
fmt.Println("Verbose mode enabled")
}
return nil
}
func (*Command) Duration ¶
Duration 获取时间间隔类型的 flag 值
从合并后的 flags 中查找(包含全局 flags 和命令 flags)。 如果未找到或解析失败,返回 0。
Example:
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
timeout := cmd.Duration("timeout")
fmt.Printf("Timeout: %v\n", timeout)
return nil
}
func (*Command) Float64 ¶
Float64 获取浮点数类型的 flag 值
从合并后的 flags 中查找(包含全局 flags 和命令 flags)。 如果未找到或解析失败,返回 0.0。
Example:
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
ratio := cmd.Float64("ratio")
fmt.Printf("Ratio: %.2f\n", ratio)
return nil
}
func (*Command) Int ¶
Int 获取整数类型的 flag 值
从合并后的 flags 中查找(包含全局 flags 和命令 flags)。 如果未找到或解析失败,返回 0。
Example:
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
port := cmd.Int("port")
fmt.Printf("Port: %d\n", port)
return nil
}
func (*Command) Int64 ¶
Int64 获取 int64 类型的 flag 值
从合并后的 flags 中查找(包含全局 flags 和命令 flags)。 如果未找到或解析失败,返回 0。
Example:
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
size := cmd.Int64("size")
fmt.Printf("Size: %d\n", size)
return nil
}
func (*Command) Output ¶
Output 获取输出目标
Example:
cmd := cli.NewCommand("init", "Initialize project")
w := cmd.Output()
fmt.Fprintln(w, "Command output")
func (*Command) PrintUsage ¶
PrintUsage 打印命令使用帮助到指定输出
Example:
cmd := cli.NewCommand("init", "Initialize a new project")
cmd.Description = "Create a new project with default configuration"
cmd.Flags = func(fs *flag.FlagSet) {
fs.String("path", ".", "Project path")
}
cmd.SetProgram(app)
cmd.PrintUsage()
func (*Command) Program ¶
Program 获取所属应用程序
Example:
cmd := cli.NewCommand("init", "Initialize project")
cmd.SetProgram(app)
p := cmd.Program()
fmt.Println(p.Name) // 输出应用名称
func (*Command) Run ¶
Run 执行命令(使用 context.Background())
Example:
cmd := cli.NewCommand("init", "Initialize project")
cmd.Flags = func(fs *flag.FlagSet) {
fs.String("path", ".", "Project path")
}
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
path := cmd.String("path")
fmt.Printf("Initializing at %s\n", path)
return nil
}
if err := cmd.Run([]string{"--path", "./myproject"}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
func (*Command) RunContext ¶
RunContext 使用指定的 context 执行命令
Example:
cmd := cli.NewCommand("init", "Initialize project")
cmd.Flags = func(fs *flag.FlagSet) {
fs.String("path", ".", "Project path")
}
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
path := cmd.String("path")
fmt.Printf("Initializing at %s\n", path)
}
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := cmd.RunContext(ctx, []string{"--path", "./myproject"}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
func (*Command) SetOutput ¶
SetOutput 设置输出目标
Example:
cmd := cli.NewCommand("init", "Initialize project")
cmd.SetOutput(os.Stdout)
func (*Command) SetProgram ¶
SetProgram 设置所属应用程序(用于访问全局信息)
Example:
cmd := cli.NewCommand("init", "Initialize project")
cmd.SetProgram(app)
cmd.PrintUsage() // 会显示 "Usage: myapp init [options]"
func (*Command) String ¶
String 获取字符串类型的 flag 值
从合并后的 flags 中查找(包含全局 flags 和命令 flags)。 如果未找到,返回空字符串。
Example:
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
path := cmd.String("path")
config := cmd.String("config") // 可能来自全局 flags
fmt.Printf("Path: %s, Config: %s\n", path, config)
return nil
}
func (*Command) Uint ¶
Uint 获取无符号整数类型的 flag 值
从合并后的 flags 中查找(包含全局 flags 和命令 flags)。 如果未找到或解析失败,返回 0。
Example:
cmd.Action = func(ctx context.Context, cmd *cli.Command) error {
count := cmd.Uint("count")
fmt.Printf("Count: %d\n", count)
return nil
}
type Program ¶
type Program struct {
Commands []*Command // 命令列表
Flags func(*flag.FlagSet) // 全局标志配置函数
Name string // 应用名称
Usage string // 应用描述
Version string // 应用版本
Banner string // 应用横幅(ASCII 艺术字等)
DefaultCommand string // 默认命令名称(当未指定命令时使用)
HideHelpCommand bool // 隐藏 help 命令
HideVersionCommand bool // 隐藏 version 命令
HideHelpFlag bool // 隐藏 -h/--help 标志
HideVersionFlag bool // 隐藏 -v/--version 标志
HelpCommand *Command // help 命令(可自定义)
VersionCommand *Command // version 命令(可自定义)
// contains filtered or unexported fields
}
Program CLI 应用程序
func NewProgram ¶
NewProgram 创建 CLI 应用程序
Example:
app := cli.NewProgram("myapp", "1.0.0")
app.Usage = "A simple CLI application"
app.Flags = func(fs *flag.FlagSet) {
fs.String("config", "", "Config file path")
}
func (*Program) Get ¶
Get 获取命令并配置其输出和应用程序引用
从已注册的命令和内置命令(help、version)中查找指定名称的命令。 找到命令后会自动设置命令的输出目标和应用程序引用。
Example:
app := cli.NewProgram("myapp", "1.0.0")
initCmd := cli.NewCommand("init", "Initialize project")
app.Commands = []*cli.Command{initCmd}
cmd := app.Get("init")
if cmd != nil {
cmd.Run([]string{})
}
func (*Program) Output ¶
Output 获取输出目标,如果未设置则返回 os.Stderr
Example:
app := cli.NewProgram("myapp", "1.0.0")
w := app.Output()
fmt.Fprintln(w, "Output to default or custom writer")
func (*Program) PrintUsage ¶
PrintUsage 打印总体使用帮助到指定输出
Example:
app := cli.NewProgram("myapp", "1.0.0")
app.Usage = "A simple CLI application"
initCmd := cli.NewCommand("init", "Initialize project")
app.Commands = []*cli.Command{initCmd}
app.PrintUsage()
func (*Program) Run ¶
Run 运行命令(使用 context.Background())
Example:
app := cli.NewProgram("myapp", "1.0.0")
initCmd := cli.NewCommand("init", "Initialize project")
initCmd.Action = func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("Initializing...")
return nil
}
app.Commands = []*cli.Command{initCmd}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
func (*Program) RunContext ¶
RunContext 使用指定的 context 运行命令
Example:
app := cli.NewProgram("myapp", "1.0.0")
initCmd := cli.NewCommand("init", "Initialize project")
initCmd.Action = func(ctx context.Context, cmd *cli.Command) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
fmt.Println("Initializing...")
}
return nil
}
app.Commands = []*cli.Command{initCmd}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := app.RunContext(ctx, os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}