从urfave/cli v2中获取go命令行标志值

huangapple go评论74阅读模式
英文:

Retrieving go command-line Flag values from urfave/cli v2

问题

我正在构建一个用于将一些文档上传到Web服务器的Golang应用程序。该应用程序是一个名为deploy_docs.go的单个Go文件(运行make build来构建应用程序)。

我对Go还比较新手,所以如果这是一个愚蠢的问题,请谅解。

应用程序应该像这样调用:

bin/deploy_docs --dochost foo.bar.local

我正在buildAppInstance()函数中解析CLI参数,该函数应该将--dochost的CLI值分配给buildAppInstance()中的&dochost

问题是我从未得到任何值分配给&dochost,我不确定为什么...我不认为这是作用域问题,但我不知道urfave/cli应该如何工作。

有人能解释一下为什么--dochost没有填充&dochost吗?

英文:

I'm building an golang app to upload some docs to a webserver. The app is a single go file called deploy_docs.go (run make build to build the application).

I'm somewhat new to go, so please keep that in mind if this is a dumb question ;-).

The app should be called like this:

bin/deploy_docs --dochost foo.bar.local

I am parsing CLI arguments in buildAppInstance(), which should assign the CLI value of --dochost to &dochost in buildAppInstance().

The problem is that I never get anything assigned to &dochost; I'm not sure why... I don't think this is scope problem but I don't know how urfave/cli should work otherwise.

Can someone explain why &dochost never gets populated by --dochost?

答案1

得分: 1

你应该调用(*App).Run来解析参数。以下是示例代码:

package main

import (
	"log"
	"os"
	"time"

	"github.com/urfave/cli/v2"
)

var dochost string = ""

func buildAppInstance() (appInst *cli.App) {
	appInst = &cli.App{
		Name:     "deploy_docs",
		Version:  "0.0.2",
		Compiled: time.Now(),
		Authors: []*cli.Author{
			{
				Name:  "Mike Pennington",
				Email: "mike@pennington.net",
			},
		},
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:        "dochost",
				Value:       "127.0.0.1",
				Usage:       "FQDN or IPv4 of the documentation host",
				Destination: &dochost,
			},
		},
		Action: func(cCtx *cli.Context) (err error) {
			log.Println("Starting cli.Context action")
			if cCtx.NArg() == 0 {
				log.Fatal("No CLI arguments detected!")
			}
			log.Printf("args: %+v", cCtx.Args())
			return nil
		},
	}
	return appInst
}

func main() {
	app := buildAppInstance()
	log.Printf("dochost before Run: %q", dochost)
	if err := app.Run(os.Args); err != nil {
		panic(err)
	}
	log.Printf("dochost after Run: %q", dochost)
}
$ go run . --dochost foo.bar.local other args
2023/06/26 23:37:43 dochost before Run: ""
2023/06/26 23:37:43 Starting cli.Context action
2023/06/26 23:37:43 args: &[other args]
2023/06/26 23:37:43 dochost after Run: "foo.bar.local"
英文:

You should call (*App).Run to make it parse arguments. See the demo below:

package main

import (
	"log"
	"os"
	"time"

	"github.com/urfave/cli/v2"
)

var dochost string = ""

func buildAppInstance() (appInst *cli.App) {
	appInst = &cli.App{
		Name:     "deploy_docs",
		Version:  "0.0.2",
		Compiled: time.Now(),
		Authors: []*cli.Author{
			{
				Name:  "Mike Pennington",
				Email: "mike@pennington.net",
			},
		},
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:        "dochost",
				Value:       "127.0.0.1",
				Usage:       "FQDN or IPv4 of the documentation host",
				Destination: &dochost,
			},
		},
		Action: func(cCtx *cli.Context) (err error) {
			log.Println("Starting cli.Context action")
			if cCtx.NArg() == 0 {
				log.Fatal("No CLI arguments detected!")
			}
			log.Printf("args: %+v", cCtx.Args())
			return nil
		},
	}
	return appInst
}

func main() {
	app := buildAppInstance()
	log.Printf("dochost before Run: %q", dochost)
	if err := app.Run(os.Args); err != nil {
		panic(err)
	}
	log.Printf("dochost after Run: %q", dochost)
}
$ go run . --dochost foo.bar.local other args
2023/06/26 23:37:43 dochost before Run: ""
2023/06/26 23:37:43 Starting cli.Context action
2023/06/26 23:37:43 args: &[other args]
2023/06/26 23:37:43 dochost after Run: "foo.bar.local"

huangapple
  • 本文由 发表于 2023年6月26日 23:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定