I have a program that requires either 1 or 2 arguments depending on what the users wants to run
var (
clientSet = tools.NewClientSet(os.Args[2])
)
func main {
if os.Args[1] == "validate" {
// run validate function, no need for user to have os.Args[2]
}else if os.Args[1] == "sync" {
// run sync function that requires os.Args[2]
}
}
func foo{
tools.Manage(clientSet)
}
I need the clientSet
variable to be global, but I dont need the users to have os.Args[2] if the users only wants to use the validate function. Putting the clientSet
function inside main()
will make my foo()
function broken and I can't declare a variable with an empty value.
So I want my users to be able to run go run main.go validate
and go run main.go sync production
smoothly.
*production is an arbitrary value
I could have my users to run go run main.go validate _
to plug this problem, but that would be inelegant.What's the best way to tackle this problem?