Documentation
¶
Index ¶
- func AddCommand(path string, cmd func(), brief string)
- func AddConfig(path string, unmarshaler Unmarshaler)
- func AddRootCommand(cmd func())
- func Alias(name, definition string)
- func Args() []string
- func Description(text string)
- func Launch()
- func Parse(v interface{}, opts ...ParseOption)
- func Title(text string)
- func Usage()
- func Use(opts ...Option)
- type Command
- type Cortana
- func (c *Cortana) AddCommand(path string, cmd func(), brief string)
- func (c *Cortana) AddConfig(path string, unmarshaler Unmarshaler)
- func (c *Cortana) AddEnvUnmarshaler(unmarshaler EnvUnmarshaler)
- func (c *Cortana) AddRootCommand(cmd func())
- func (c *Cortana) Alias(name, definition string)
- func (c *Cortana) Args() []string
- func (c *Cortana) Commands() []*Command
- func (c *Cortana) Description(text string)
- func (c *Cortana) Launch()
- func (c *Cortana) Parse(v interface{}, opts ...ParseOption)
- func (c *Cortana) Title(text string)
- func (c *Cortana) Usage()
- func (c *Cortana) Use(opts ...Option)
- type EnvUnmarshalFunc
- type EnvUnmarshaler
- type Option
- type ParseOption
- type UnmarshalFunc
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCommand ¶
AddCommand 注册一个命令(使用全局默认实例)
Types ¶
type Command ¶
type Command struct {
Path string // 命令路径,如 "say hello"
Proc func() // 命令被匹配时执行的处理函数
Brief string // 命令的简要描述
Alias bool // 是否是别名命令
// contains filtered or unexported fields
}
Command 表示一个命令的执行单元 每个命令有路径(名称)、处理函数和简要描述
type Cortana ¶
type Cortana struct {
// contains filtered or unexported fields
}
Cortana 是命令行解析器的核心结构体 管理命令注册、参数解析、配置加载等功能
func (*Cortana) AddCommand ¶
AddCommand 注册一个命令 参数:
- path: 命令路径,多级命令用空格分隔,如 "say hello cortana"
- cmd: 命令被匹配时执行的处理函数
- brief: 命令的简要描述,用于帮助信息展示
func (*Cortana) AddConfig ¶
func (c *Cortana) AddConfig(path string, unmarshaler Unmarshaler)
AddConfig 添加一个配置文件 参数:
- path: 配置文件路径
- unmarshaler: 配置文件的反序列化器
func (*Cortana) AddEnvUnmarshaler ¶
func (c *Cortana) AddEnvUnmarshaler(unmarshaler EnvUnmarshaler)
AddEnvUnmarshaler 添加一个环境变量反序列化器
func (*Cortana) AddRootCommand ¶
func (c *Cortana) AddRootCommand(cmd func())
AddRootCommand 注册一个根命令(无子路径) 当没有匹配到任何子命令时执行
func (*Cortana) Alias ¶
Alias 给命令创建一个别名 参数:
- name: 别名,如 "cortana"
- definition: 原始命令路径,如 "say hello cortana"
func (*Cortana) Description ¶
Description 设置当前命令的详细描述 描述信息会在帮助文档中展示,用于说明命令的用途和用法
func (*Cortana) Launch ¶
func (c *Cortana) Launch()
Launch 查找并执行匹配的命令 它会解析命令行参数,查找对应的命令,如果找到则执行 如果未找到匹配命令,则显示帮助信息
func (*Cortana) Parse ¶
func (c *Cortana) Parse(v interface{}, opts ...ParseOption)
Parse 将命令行参数解析到结构体中 通过结构体标签声明参数定义,支持配置文件、环境变量和命令行参数三种来源 参数:
- v: 指向结构体的指针,结构体字段需使用 cortana 标签声明
- opts: 可选的解析选项
type EnvUnmarshalFunc ¶
type EnvUnmarshalFunc func(v interface{}) error
EnvUnmarshalFunc 是一个函数类型,实现了 EnvUnmarshaler 接口 方便将普通函数转换为 EnvUnmarshaler
func (EnvUnmarshalFunc) Unmarshal ¶
func (f EnvUnmarshalFunc) Unmarshal(v interface{}) error
Unmarshal 实现 EnvUnmarshaler 接口,直接调用函数本身
type EnvUnmarshaler ¶
type EnvUnmarshaler interface {
Unmarshal(v interface{}) error
}
EnvUnmarshaler 定义环境变量反序列化接口 实现此接口可以从环境变量中读取值并填充到目标结构体中
type Option ¶
type Option func(flags *predefined)
Option 是 Cortana 的配置选项函数类型(函数式选项模式)
func ConfFlag ¶
func ConfFlag(long, short string, unmarshaler Unmarshaler) Option
ConfFlag 添加配置文件路径选项 允许用户通过命令行参数指定配置文件路径 参数:
- long: 长选项名,如 "--config"
- short: 短选项名,如 "-c"
- unmarshaler: 配置文件反序列化器
type ParseOption ¶
type ParseOption func(opt *parseOption)
ParseOption 是 Parse 方法的选项函数类型
func WithArgs ¶
func WithArgs(args []string) ParseOption
WithArgs 指定自定义的参数列表进行解析(替代 os.Args) 主要用于测试场景
type UnmarshalFunc ¶
UnmarshalFunc 是一个函数类型,实现了 Unmarshaler 接口 方便将普通函数转换为 Unmarshaler,例如:
cortana.UnmarshalFunc(json.Unmarshal)
func (UnmarshalFunc) Unmarshal ¶
func (f UnmarshalFunc) Unmarshal(data []byte, v interface{}) error
Unmarshal 实现 Unmarshaler 接口,直接调用函数本身
type Unmarshaler ¶
Unmarshaler 定义配置文件反序列化接口 实现此接口可以将配置文件的原始字节数据解析到目标结构体中
Directories
¶
| Path | Synopsis |
|---|---|
|
say.go 是一个综合示例,展示 Cortana 的主要功能: - 多层次子命令注册 - 结构体标签定义参数(cortana 和 lsdd 两种标签) - 命令标题和描述(用于帮助文档) - 命令别名 - 配置文件支持(JSON 格式) - 通过 --config/-c 选项动态指定配置文件路径
|
say.go 是一个综合示例,展示 Cortana 的主要功能: - 多层次子命令注册 - 结构体标签定义参数(cortana 和 lsdd 两种标签) - 命令标题和描述(用于帮助文档) - 命令别名 - 配置文件支持(JSON 格式) - 通过 --config/-c 选项动态指定配置文件路径 |