From 2d898c83b0cc8b46c0d757c261ef31d0b6a6ff91 Mon Sep 17 00:00:00 2001 From: XvZheChuang Date: Thu, 14 Mar 2024 19:43:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd.go | 21 +++++++++++++++++---- godemo_test.go | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 4a390e0a..a77cbe53 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -35,7 +35,12 @@ type cmd struct { version string } +/* +* +args:参数列表 +*/ func (c *cmd) Execute(args []string) { + //app.run:启动实例 if err := c.app.Run(args); err != nil { fmt.Println(err) os.Exit(1) @@ -43,17 +48,22 @@ func (c *cmd) Execute(args []string) { } func newCmd() *cmd { + //vfox的运行版本 version := internal.RuntimeVersion cli.VersionFlag = &cli.BoolFlag{ - Name: "version", + //当前cli程序的“名字” + Name: "version", + //别名,可以接受多个对象 Aliases: []string{"v", "V"}, - Usage: "print version", + //此程序的简介 + Usage: "print version", + //当未使用子命令时,此程序要进行的动作。其值应当为一个 cli.ActionFunc 函数 Action: func(ctx *cli.Context, b bool) error { println(version) return nil }, } - + //声明app的各种参数及行为 app := &cli.App{} app.EnableBashCompletion = true app.Name = "vfox" @@ -62,7 +72,9 @@ func newCmd() *cmd { app.Copyright = "Copyright 2024 Han Li. All rights reserved." app.Version = version app.Description = "vfox is a cross-platform version manager, extendable via plugins. It allows you to quickly install and switch between different environment you need via the command line." + //启用命令和标志的建议 app.Suggest = true + //设置 shell 完成标志时要执行的操作 app.BashComplete = func(ctx *cli.Context) { for _, command := range ctx.App.Commands { _, _ = fmt.Fprintln(ctx.App.Writer, command.Name) @@ -77,10 +89,11 @@ func newCmd() *cmd { return nil }, } - + //标志列表 app.Flags = []cli.Flag{ debugFlags, } + //可执行的命令/动作/行为列表 app.Commands = []*cli.Command{ commands.Info, commands.Install, diff --git a/godemo_test.go b/godemo_test.go index 4459b57a..c6fa3d3c 100644 --- a/godemo_test.go +++ b/godemo_test.go @@ -9,7 +9,7 @@ func TestOsArgs(t *testing.T) { args := os.Args for i := 0; i < len(args); i++ { arg := args[i] - println("the args on the No." + string(i)) + println("//") println(arg) } }