Can command line flags in Go be set to mandatory?

前端 未结 7 1451
-上瘾入骨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:43

    Or you could docopt, where you only have to write the "usage" text. Docopt interprets the usage text and creates an argument map. This opens up a whole lot of possibilities, all following the POSIX usage text standard. This library is available for about 20 languages already.

    https://github.com/docopt/docopt.go

    package main
    
    import (
            "fmt"
            "github.com/docopt/docopt-go"
    )
    
    const (
            Usage = `Naval Fate.
    
    Usage:
      naval_fate ship new <name>...
      naval_fate ship <name> move <x> <y> [--speed=<kn>]
      naval_fate ship shoot <x> <y>
      naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
      naval_fate -h | --help
      naval_fate --version
    
    Options:
      -h --help     Show this screen.
      --version     Show version.
      --speed=<kn>  Speed in knots [default: 10].
      --moored      Moored (anchored) mine.
      --drifting    Drifting mine.`
    )
    
    func main() {
            args, _ := docopt.ParseDoc(Usage)
            fmt.Println(args)
    }
    
    0 讨论(0)
提交回复
热议问题