This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create staert object | |
s:=staert.NewStaert(command) | |
// Adding sources | |
s.AddSource(toml) | |
s.AddSource(f) | |
s.AddSource(kv) | |
// Parse and merge sources | |
loadedConfig, err := s.LoadConfig() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a TOML document. | |
LogLevel = "DEBUG" | |
Timeout = "2s" | |
[owner] | |
name = "Tom Preston-Werner" | |
DateOfBirth = 1979-05-27T07:32:00-08:00 | |
Rate = 0.75 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TOML source | |
toml:=staert.NewTomlSource("example", []string{"./toml/", "/any/other/path"}) | |
// Flaeg source | |
f:=flaeg.New(command, os.Args[1:]) | |
// Key-Value Store source | |
kv, err :=staert.NewKvSource(backend, options, “prefix”) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./flaegdemo version | |
Version Rebloch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f := flaeg.New(flaegCmd, os.Args[1:]) | |
if err := f.Run(); err != nil { | |
fmt.Println(err.Error()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./flaegdemo | |
{"VersionName":"Rebloch","LogLevel":"INFO","Timeout":1000000000,"Db":null,"Owner":{"Name":"default owner","DateOfBirth":"2016-08-03T17:36:20.85900754+02:00","Rate":0.5}} | |
$ ./flaegdemo --db | |
{"VersionName":"Rebloch","LogLevel":"INFO","Timeout":1000000000,"Db":{"ConnectionMax":100,"ConnectionMax64":6400000000000000000,"Watch":true,"IP":"192.168.0.1"},"Owner":{"Name":"default owner","DateOfBirth":"2016-08-03T17:36:33.683130019+02:00","Rate":0.5}} | |
$ ./flaegdemo --db.ip=127.0.0.1 --owner.dob=1979-05-27T07:32:00Z | |
{"VersionName":"Rebloch","LogLevel":"INFO","Timeout":1000000000,"Db":{"ConnectionMax":100,"ConnectionMax64":6400000000000000000,"Watch":true,"IP":"127.0.0.1"},"Owner":{"Name":"default owner","DateOfBirth":"1979-05-27T07:32:00Z","Rate":0.5}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f.AddCommand(flaegVersionCmd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./flaegdemo --help | |
flaegdemo is a golang program...(here the program description) | |
Usage: flaegdemo [--flag=flag_argument] [-f[flag_argument]] ... set flag_argument to flag(s) | |
or: flaegdemo [--flag[=true|false| ]] [-f[true|false| ]] ... set true/false to boolean flag(s) | |
Available Commands: | |
version Print version | |
Use "flaegdemo [command] --help" for more information about a command. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Configuration is a struct which contains all differents type to field | |
//using parsers on string, time.Duration, pointer, bool, int, int64, time.Time, float64 | |
type Configuration struct { | |
VersionName string //no description struct tag, it will not be flaged | |
LogLevel string `short:"l" description:"Log level"` //string type field, short flag "-l" | |
Timeout time.Duration `description:"Timeout duration"` //time.Duration type field | |
Db *DatabaseInfo `description:"Enable database"` //pointer type field (on DatabaseInfo) | |
Owner *OwnerInfo `description:"Enable Owner description"` //another pointer type field (on OwnerInfo) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flaegCmd := &flaeg.Command{ | |
Name: "flaegdemo", | |
Description: "flaegdemo is a golang program...(here the program description)", | |
Config: &config, | |
DefaultPointersConfig: &defaultConfigOfPointerFields, | |
Run: func() error { | |
printableConfig, _ := json.Marshal(config) | |
fmt.Printf("%s\n", printableConfig) | |
return nil | |
}, |