1 Star 0 Fork 0

k3x/go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
command.go 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
k3x 提交于 2022-01-05 06:05 +08:00 . init
package flagx
import (
"context"
"flag"
"fmt"
"os"
"strings"
)
func New(args ...string) *App {
if len(args) == 0 {
args = os.Args
}
return &App{name: args[0], args: args[1:], commands: map[string]Command{}}
}
type App struct {
args []string
name string
description string
commands map[string]Command
}
func (app *App) AddCommand(name string, command Command) *App {
app.commands[name] = command
return app
}
func (app *App) Usage() {
if app.description != "" {
fmt.Fprintf(os.Stderr, "%s\n", app.description)
}
fmt.Fprintf(os.Stderr, "命令格式: %s [命令] [...参数]\n", app.name)
fmt.Fprintf(os.Stderr, "命令说明:\n")
max := 0
for name, command := range app.commands {
if !isHidden(command) {
if l := len(name); l > max {
max = l
}
}
}
for name, command := range app.commands {
if !isHidden(command) {
fmt.Fprintf(os.Stderr, " %*s %s\n", -max, name, getUsage(command))
}
}
fmt.Fprintln(os.Stderr)
}
func (app *App) Run(ctx context.Context) {
if len(app.args) == 1 && app.args[0] == "" {
app.Usage()
os.Exit(1)
}
if len(app.args) == 0 || strings.HasPrefix(app.args[0], "-") {
app.Usage()
os.Exit(1)
}
commandName := app.args[0]
command, find := app.commands[commandName]
if !find {
app.Usage()
os.Exit(1)
}
args := app.args
flagOpt := func(flagSet *flag.FlagSet) { flagSet.Init(commandName, flag.ContinueOnError) }
var err error
if args, err = Parse(command, args[1:], flagOpt); err != nil {
os.Exit(1)
}
if err := command.Run(ctx, args); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
}
type Command interface {
Run(ctx context.Context, args []string) error
}
func isHidden(c Command) bool {
if x, ok := c.(hiddenable); ok {
return x.Hidden()
}
return false
}
func getUsage(c Command) string {
if x, ok := c.(usageable); ok {
return x.Usage()
}
return ""
}
type (
usageable interface{ Usage() string }
hiddenable interface{ Hidden() bool }
)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/k3x/go.git
git@gitee.com:k3x/go.git
k3x
go
go
82f96e922e7b

搜索帮助