Golang:命令行参数未定义:变量 [英] Golang : command-line-arguments undefined: variable

查看:55
本文介绍了Golang:命令行参数未定义:变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Golang中的用法显示创建一个外部函数,但是我不知道如何调用flag变量.这是我的实际代码:

I want to create an external function for usage display in Golang, but I don't know how to call the flag variable. This is my actual code:

package main

import (
    "flag"
    "fmt"
    "os"
)

func Usage() {
    if ArgSend {
        fmt.Printf("Usage: SEND")
        flag.PrintDefaults()
        os.Exit(0)

    } else if ArgTest {
        fmt.Printf("Usage: -test")
        flag.PrintDefaults()
        os.Exit(0)

    } else if ArgMacro {
        fmt.Printf("Usage: -macro")
        os.Exit(0)

    } else {
        fmt.Printf("Usage of: <-test|-send|-macro>\n")
        os.Exit(0)
    }
}



func main() {

    //defaults variables
    ArgTest, ArgSend, ArgMacro  := false, false, false

    // Args parse
    flag.BoolVar(&ArgTest, "-test", false, "run test mode")
    flag.BoolVar(&ArgSend, "-send", false, "run send mode")
    flag.BoolVar(&ArgMacro, "-macro", false, "run macro mode")

    flag.Parse()

    Usage()
}

返回此错误:

F:\dev\GoLang\gitlab\EasySend\tmp>go run stackoverflow.go -test
# command-line-arguments
.\stackoverflow.go:10:5: undefined: ArgSend
.\stackoverflow.go:15:12: undefined: ArgTest
.\stackoverflow.go:20:12: undefined: ArgMacro

如果ArgSend是true/false,如何检查标志解析?

How can I check flag parse if ArgSend is true/false?

推荐答案

您的示例中有几处错误:

A couple of things wrong in your example:

  • 您尝试在使用函数中使用的变量不在范围内,因为所有标志变量都在main(.)内部声明.
  • 标志变量本身的类型错误,应使用标志包中的类型
  • 其他错误包括,在标记文本(第二个arg)的前面添加-",并且不取消引用标记变量(它们将是指针)

这里有一个很好的例子: golang标志示例,您应检查标志上的godocs ,特别是对于默认行为和自定义用法,如果您在修改示例时遇到问题,请在此处再次询问

There is a good example here: golang flags example and you should check godocs on flags particularly for default behavior and custom usage functions, if you have trouble modifying the example then ask again here

已更新:抱歉,正如Peter在评论中指出的那样,我的回答有点混乱和错误.

Updated: Sorry, as Peter pointed to in comments my answer is a little muddled and incorrect.

为澄清起见,在给定flag.Bool的"golang标志示例"链接中提供的示例中.使用flag.Bool时,将返回一个指针.

To Clarify, in the example provided in the "golang flags example" link given flag.Bool is used. When using flag.Bool a pointer is returned.

在问题"中使用flag.BoolVar,它允许您引用布尔值.您在问题中使用flag.BoolVar实际上是正确的.

In the Question you use flag.BoolVar which allows you to reference a bool value. your use of flag.BoolVar in the question is in fact correct.

因此,您所需要做的就是解决范围界定问题,并不能真正弄清楚您打算如何使用用法",但这是一个工作示例,应予以澄清:

So all you need to do is address the scoping issue, not really clear what you are trying to do with your Usage but here is a working example that should clarify:

注意:在此示例中,标志vars可以留在main内部,因为在Usage函数中不需要它们

Note: in this example the flag vars could have stayed inside main as they are not required in the Usage function

package main

import (
    "flag"
    "fmt"
    "os"
)

func Usage() {
    // custom usage (help) output here if needed
    fmt.Println("")
    fmt.Println("Application Flags:")
    flag.PrintDefaults()
    fmt.Println("")
}

var ArgTest, ArgSend, ArgMacro bool

func main() {

    // Args parse
    flag.BoolVar(&ArgTest, "test", false, "run test mode")
    flag.BoolVar(&ArgSend, "send", false, "run send mode")
    flag.BoolVar(&ArgMacro, "macro", false, "run macro mode")

    flag.Parse()

    // assign custom usage function (will be shown by default if -h or --help flag is passed)
    flag.Usage = Usage

    // if no flags print usage (not default behaviour)
    if len(os.Args) == 1 {
        Usage()
    }

    fmt.Printf("ArgTest val: %t\n", ArgTest)
    fmt.Printf("ArgSend val: %t\n", ArgSend)
    fmt.Printf("ArgMacro val: %t\n", ArgMacro)

}

这篇关于Golang:命令行参数未定义:变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆