代码拉取完成,页面将自动刷新
package scli
import (
"github.com/urfave/cli/v2"
)
type (
Options struct {
// flags will set value which following --xxx to parameter
flags []cli.Flag
// commands will be trigger by flag xxx
commands []*cli.Command
}
BaseCfg struct {
//Name is the command full name
Name string
//Short is the command aliases name
Short string
//Usage is the description of command
Usage string
//Required is required
Required bool
}
//FlagCfg is the config of parameter
FlagCfg struct {
*BaseCfg
//Default value
Default string
//Dest is pointer of value
Dest interface{}
}
//CommandCfg is the config of command
CommandCfg struct {
*BaseCfg
Action func([]string) error
}
Option func(options *Options)
)
//WithStringFlag will create string flag
func WithStringFlag(flag *FlagCfg) Option {
if flag == nil {
panic("invalid flag config ")
}
if len(flag.Name) == 0 && len(flag.Short) == 0 {
panic("flag's name or short name must set value")
}
return func(o *Options) {
sf := &cli.StringFlag{
Name: flag.Name,
Aliases: []string{flag.Short},
Value: flag.Default,
Usage: flag.Usage,
Required: flag.Required,
Destination: flag.Dest.(*string),
}
o.flags = append(o.flags, sf)
}
}
func WithFlag(flag *FlagCfg) Option {
var opt Option
switch flag.Dest.(type) {
case *string:
opt = func(o *Options) {
sf := &cli.StringFlag{
Name: flag.Name,
Aliases: []string{flag.Short},
Value: flag.Default,
Usage: flag.Usage,
Required: flag.Required,
Destination: flag.Dest.(*string),
}
o.flags = append(o.flags, sf)
}
case *bool:
opt = func(o *Options) {
sf := &cli.BoolFlag{
Name: flag.Name,
Aliases: []string{flag.Short},
Value: false,
Usage: flag.Usage,
Required: flag.Required,
Destination: flag.Dest.(*bool),
}
o.flags = append(o.flags, sf)
}
default:
panic("unsupported flag type !")
}
return opt
}
func WithCommand(command *CommandCfg) Option {
if command == nil {
panic("invalid command config ")
}
if len(command.Name) == 0 && len(command.Short) == 0 {
panic("command's name or short name must set value")
}
return func(o *Options) {
cmd := &cli.Command{
Name: command.Name,
Usage: command.Usage,
Aliases: []string{command.Short},
}
act := command.Action
if act != nil {
cmd.Action = func(ctx *cli.Context) error {
return act(ctx.Args().Slice())
}
}
o.commands = append(o.commands, cmd)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。