Can command line flags in Go be set to mandatory?

前端 未结 7 1521
-上瘾入骨i
-上瘾入骨i 2021-02-05 00:04

Is there a way how to set that certain flags are mandatory, or do I have to check for their presence on my own?

7条回答
  •  面向向阳花
    2021-02-05 00:35

    If you have flag path, simply check if *path contains some value

    var path = flag.String("f", "", "/path/to/access.log")
    flag.Parse()
    if *path == "" {
        usage()
        os.Exit(1)
    }
    

提交回复
热议问题