Reputation: 34954
I am writing a CLI tool in go and have chosen github.com/jessevdk/go-flags for the CLI arg parsing. I am trying to figure out the best way to make a positional arg mandatory. Currently, I have the following:
func main() {
args, err := flags.Parse(&opts)
if err != nil {
panic(err)
}
if len(args) < 1 {
panic("An s3 bucket is required")
}
}
This works, but it doesn't result in help output being displayed, as would be the case with a flag being marked "required:true"
. Is there a way to replicate that behavior by manually calling a "print help" function or setting a required number of positional arguments?
Upvotes: 3
Views: 1870
Reputation: 7081
Would using os.Args help? Eg:
package main
import (
"fmt"
"os"
)
const Usage = `Usage:
%s one two
`
func main() {
if len(os.Args) != 3 {
fmt.Printf(Usage, os.Args[0])
os.Exit(-1)
}
//run program
}
os.Args hold the command-line arguments, starting with the program name.
https://play.golang.org/p/Le9EMxmw9k
Upvotes: 2