Documentation
¶
Index ¶
- Constants
- Variables
- func FindAncestor[T any](ctx *ValidationContext) *T
- func GetFullUsage(config *ParserConfig) string
- func GetOption[T any](config *ParserConfig) (*T, error)
- func GetTermWidth() int
- func GetUsage[T any](config *ParserConfig) (string, error)
- func Register(parentG interface{}, parentOptName string, optG interface{})
- func Usage(config *ParserConfig) string
- type Alignment
- type Cell
- type Header
- type ParserConfig
- type Row
- type Source
- type Style
- type Table
- type Title
- type ValidationContext
Constants ¶
const ( AliasTagName = "alias" DescriptionShortTagName = "desc" DescriptionFullTagName = "description" RequiredTagName = "required" DefaultTagName = "default" )
Variables ¶
var ErrHelp = errors.New("argStruct: help requested")
ErrHelp 表示用户请求了自动帮助信息(例如传入了 -h/--help)。 GetOption 在触发自动帮助时会返回 ErrHelp,调用方可以根据它决定是否退出程序。
Functions ¶
func FindAncestor ¶
func FindAncestor[T any](ctx *ValidationContext) *T
FindAncestor 查找链上的第一个满足类型 T 的节点
func GetFullUsage ¶ added in v0.9.3
func GetFullUsage(config *ParserConfig) string
GetFullUsage 获取全局所有已注册参数树的帮助信息。 现在是“全局帮助”的实现;后续如果要支持“按树选择”也可以在这里扩展。
func GetOption ¶
func GetOption[T any](config *ParserConfig) (*T, error)
func GetUsage ¶ added in v0.9.3
func GetUsage[T any](config *ParserConfig) (string, error)
GetUsage 获取某个参数结构体 T 的帮助信息(包含其子树)。 T 必须是 struct 类型(与 GetOption 的约束保持一致)。
语义:
- 只渲染 “T 对应的 optionGroup 以及它的子 group” 的帮助信息;
- 如果 T 在 schema 中没有注册,或者注册了多个 group,会 panic 提示。
func Usage ¶
func Usage(config *ParserConfig) string
Usage 是对外的默认帮助入口,相当于“打印全部帮助信息”。 内部直接委托给 GetFullUsage,保持行为一致。
Types ¶
type Cell ¶ added in v0.9.3
type Cell struct {
// contains filtered or unexported fields
}
func NewTableCell ¶ added in v0.9.3
func (*Cell) SetPaddingWithLen ¶ added in v0.9.3
SetPaddingWithLen 设置右侧 padding 的长度(以空格数量计),并更新 maxWidth。
type ParserConfig ¶
type Row ¶ added in v0.9.3
type Row struct {
// contains filtered or unexported fields
}
func (*Row) GetMaxCell ¶ added in v0.9.3
func (*Row) GetRowLen ¶ added in v0.9.3
GetRowLen 返回 JoinCells 的字节长度(调试用)。实际显示宽度请使用 stringDisplayWidth。
type Table ¶ added in v0.9.3
type Table struct {
// contains filtered or unexported fields
}
func InitRenderTable ¶ added in v0.9.3
InitRenderTable 创建一个带指定缩进的表格。
func (*Table) AppendRowWithCells ¶ added in v0.9.3
func (*Table) RemoveRowWithIndex ¶ added in v0.9.3
RemoveRowWithIndex 通过下标移除一行
type ValidationContext ¶
type ValidationContext struct {
// contains filtered or unexported fields
}
ValidationContext 对外暴露的校验上下文
func (*ValidationContext) Parent ¶
func (ctx *ValidationContext) Parent() *ValidationContext
Parent 直接父节点(父 struct 所在的 context),最顶层 root 的 Parent() 为 nil
func (*ValidationContext) Path ¶
func (ctx *ValidationContext) Path() string
Path 当前节点的逻辑路径,比如 "Root.Sub1.Sub2"
func (*ValidationContext) Root ¶
func (ctx *ValidationContext) Root() any
Root 整个 CLI 的 root(通常是 *Root)
func (*ValidationContext) Self ¶
func (ctx *ValidationContext) Self() any
Self 当前这一层(通常是 *YourStruct)
func (*ValidationContext) WalkUp ¶
func (ctx *ValidationContext) WalkUp(fn func(c *ValidationContext) bool)
WalkUp 链式向上遍历,从 Self 开始一路走到 Root。 fn 返回 true 表示“停止遍历”。