新聞中心
基本介紹
大部分場(chǎng)景下,我們通過(guò)?Command?命令行對(duì)象來(lái)管理單個(gè)或多個(gè)命令,并且使用默認(rèn)的命令行解析規(guī)則(不用顯式使用?Parser?解析器)即可。?Command?對(duì)象定義如下:

詳細(xì)請(qǐng)參考接口文檔:https://pkg.GO.dev/github.com/gogf/gf/v2/os/gcmd@master#Command
// Command holds the info about an argument that can handle custom logic.
type Command struct {
Name string // Command name(case-sensitive).
Usage string // A brief line description about its usage, eg: gf build main.go [OPTION]
Brief string // A brief info that describes what this command will do.
Description string // A detailed description.
Arguments []Argument // Argument array, configuring how this command act.
Func Function // Custom function.
FuncWithValue FuncWithValue // Custom function with output parameters that can interact with command caller.
HelpFunc Function // Custom help function
Examples string // Usage examples.
Additional string // Additional info about this command, which will be appended to the end of help info.
Strict bool // Strict parsing options, which means it returns error if invalid option given.
Config string // Config node name, which also retrieves the values from config component along with command line.
parent *Command // Parent command for internal usage.
commands []*Command // Sub commands of this command.
}由于對(duì)象均有詳細(xì)的注釋,這里不再贅述。
回調(diào)方法
?Command?對(duì)象支持3個(gè)回調(diào)方法:
- ?
Func?我們一般需要自定義這個(gè)回調(diào)方法,用于實(shí)現(xiàn)當(dāng)前命令執(zhí)行的操作。 - ?
FuncWithValue?方法同?Func?,只不過(guò)支持返回值,往往用于命令行相互調(diào)用的場(chǎng)景,一般項(xiàng)目用不到。 - ?
HelpFunc?自定義幫助信息,一般來(lái)說(shuō)沒(méi)有太大必要,因?yàn)?Command?對(duì)象能夠自動(dòng)生成幫助信息。
我們主要關(guān)注?Func?回調(diào)方法即可,其他方法大家感興趣可以自行研究。
Func回調(diào)
方法定義:
// Function is a custom command callback function that is bound to a certain argument.
type Function func(ctx context.Context, parser *Parser) (err error)可以看到,在回調(diào)方法內(nèi)部,我們通過(guò)?parser?對(duì)象獲取解析參數(shù)和選項(xiàng),并通過(guò)返回?error?來(lái)告訴上層調(diào)用方法是否執(zhí)行成功。
使用示例:
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gctx"
)
var (
Main = &gcmd.Command{
Name: "main",
Brief: "start http server",
Description: "this is the command entry for starting your http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("Hello world")
})
s.SetPort(8199)
s.Run()
return
},
}
)
func main() {
Main.Run(gctx.New())
}這也是大部分項(xiàng)目的啟動(dòng)命令行對(duì)象的樣子,大部分項(xiàng)目只有一個(gè)啟動(dòng)入口,并且只會(huì)有一個(gè)回調(diào)方法實(shí)現(xiàn)。
幫助信息生成
?Command?對(duì)象雖然可以自定義?HelpFunc?幫助回調(diào)方法,但?Command?對(duì)象可以自動(dòng)生成?Help?使用幫助信息,大部分場(chǎng)景下無(wú)需自定義。并且?gcmd?組件默認(rèn)內(nèi)置了支持了?h/help?選項(xiàng),因此使用?gcmd?組件的程序可以通過(guò)這兩個(gè)選項(xiàng)自動(dòng)生成?Help?幫助信息。
我們來(lái)看一個(gè)例子,我們先通過(guò)?go build main.go?把上面的例子編譯為二進(jìn)制?main?文件,然后來(lái)簡(jiǎn)單看一下只有一個(gè)命令場(chǎng)景下自動(dòng)生成的幫助信息:
$ ./main -h
USAGE
main [OPTION]
DESCRIPTION
this is the command entry for starting your http server 層級(jí)命令管理
父命令與子命令
一個(gè)?Command?命令可以添加子級(jí)命令,當(dāng)?Command?存在子級(jí)命令時(shí),自己便成為了父級(jí)命令。子級(jí)命令也可以添加自己的子級(jí)命令,以此類推,形成層級(jí)命令關(guān)系。父級(jí)命令和子級(jí)命令都可以有自己的回調(diào)方法,不過(guò)大部分場(chǎng)景下,一旦?Command?成為了父級(jí)命令,回調(diào)方法往往都沒(méi)有太大存在的必要。我們通常通過(guò)?AddCommand?方法為?Command?添加子級(jí)命令:
// AddCommand adds one or more sub-commands to current command.
func (c *Command) AddCommand(commands ...*Command) error層級(jí)命令使用示例
我們來(lái)演示一個(gè)多命令管理的示例。我們將上面的例子改進(jìn)一下,增加兩個(gè)子級(jí)命令。
package main
import (
"context"
"fmt"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gctx"
)
var (
Main = &gcmd.Command{
Name: "main",
Brief: "start http server",
Description: "this is the command entry for starting your process",
}
Http = &gcmd.Command{
Name: "http",
Brief: "start http server",
Description: "this is the command entry for starting your http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
fmt.Println("start http server")
return
},
}
Grpc = &gcmd.Command{
Name: "grpc",
Brief: "start grpc server",
Description: "this is the command entry for starting your grpc server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
fmt.Println("start grpc server")
return
},
}
)
func main() {
err := Main.AddCommand(Http, Grpc)
if err != nil {
panic(err)
}
Main.Run(gctx.New())
}可以看到,我們通過(guò)?AddCommand?命令為主命令增加了兩個(gè)子級(jí)命令?http/grpc?,分別用于開(kāi)啟?http/grpc?服務(wù)。當(dāng)存在子級(jí)命令式,父命令往往便沒(méi)有?Func?回調(diào)定義的必要了,因此我們這里去掉了?main?命令的?Func?定義。
我們編譯后來(lái)執(zhí)行一下看看效果:
$ main
USAGE
main COMMAND [OPTION]
COMMAND
http start http server
grpc start grpc server
DESCRIPTION
this is the command entry for starting your process使用?http?命令:
$ main http
start http server使用?grpc?命令:
$ main grpc
start grpc server 標(biāo)題名稱:創(chuàng)新互聯(lián)GoFrame教程:GoFrame命令管理-命令行對(duì)象
鏈接分享:http://m.5511xx.com/article/djoocph.html


咨詢
建站咨詢
