doupeizheng3918 2017-09-23 16:28
浏览 89

Golang:命令行参数未定义:变量

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>
")
        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()
}

Return this error:

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

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

  • 写回答

1条回答 默认 最新

  • dqn48247 2017-09-23 16:42
    关注

    A couple of things wrong in your example:

    • The variables you are trying to use in your usage function are not in scope since all your flag variables are declared inside main (.
    • The flag variables themselves are of the wrong type, you should use types from the flag package
    • Others errors include, adding '-' at the front of flag text (2nd arg) and not dereferencing the flag variables (they will be pointers)

    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

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

    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.

    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:

    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
    ", ArgTest)
        fmt.Printf("ArgSend val: %t
    ", ArgSend)
        fmt.Printf("ArgMacro val: %t
    ", ArgMacro)
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)