hi,
I'd like to define a couple of global variables in a small cli app. The app has a few files but it's not large.
What I'd like is to use cli flags to define options, and those options should be available for all the functions. I quite like the fact that the content of the variables is checked for correctnes.
Code (using kingpin):
==============kinpingtest.go=====================================
package main
import (
"
gopkg.in/alecthomas/kingpin.v2"
"os"
)
var (
app = kingpin.New("shittytest", "shitty kingpin test")
url = app.Flag("url", "url for app").Required().Short('u').URL()
domain = app.Flag("domain", "domain for app").Short('d').Required().String()
)
func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
whatever()
}
================end kingpintest.go================================
and second file:
=================otherfile.go====================================
package main
import (
"fmt"
)
func whatever() {
u := *url
fmt.Println(u.String() + "/that")
fmt.Println(*domain)
}
===================endotherfile.go===================================
And quit unsurprisingly, running it:
The 'problem' I have is that I cannot use the variables directly and have to convert them to strings first, and as far as I can see I cannot do that globally because the kingpin arguments get parsed in main().
I see one possible solution, and that is to convert the vars to strings in main() and pass those as arguments to funcs called from main().
Are there better options for what I want to achieve (certainly must be, I am just a go newbie)?
Thanks in advance.
Regards,
Natxo