Documentation ¶
Overview ¶
Package confu provides a way for doing the configuration just the way we use the command line.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Tokenize ¶
Tokenize prepares a string in a way than can get parsed using a `flag.FlagSet`, from standard Go library. We can have a configuration string with command line argument syntax, anywhere. So essentially there could be a config file with command line argument syntax and we read it all as a single string. Then it can be used with `flag.FlagSet` to be loaded into the target `struct`.
sample usage:
input := `--tag --comment="done" --port=8081 --path '/some/path'` args := Tokenize(input) var conf struct { save bool path string port int tag bool comment string } set := &flag.FlagSet{} set.BoolVar(&conf.save, "save", false, "") set.StringVar(&conf.path, "path", "./", "") set.IntVar(&conf.port, "port", 8080, "") set.BoolVar(&conf.tag, "tag", false, "") set.StringVar(&conf.comment, "comment", "", "") set.Parse(args) log.Printf("%+v", conf)
and the output will be:
{save:false path:/some/path port:8081 tag:true comment:"done"}
this function splits the input string based on spaces & new line char will get replaced by space
Example ¶
// input could come from a file or any other source input := makeInput(`--bool -s %v --int %v`, "txt", 10) // 1 - tokenize input config string args := Tokenize(input) var conf struct { boolArg bool stringArg string intArg int } // 2 - load parsed config into out conf struct set := &flag.FlagSet{} set.BoolVar(&conf.boolArg, "bool", false, "") set.StringVar(&conf.stringArg, "s", "./", "") set.IntVar(&conf.intArg, "int", 1000, "") set.Parse(args) // 3 - use the conf // ...
Output:
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.