Created
September 26, 2016 18:54
-
-
Save maplebed/c450d53336a0ecdb10675725b9be9909 to your computer and use it in GitHub Desktop.
Sample code testing use of an ini file with go-flags - config file is a callback
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
flag "github.com/jessevdk/go-flags" | |
) | |
type Opts struct { | |
TestFlag string `short:"t" description:"just here to aid in testing ini files and defaults" default:"default"` | |
// ConfigFile func(s string) error `short:"c" long:"config" description:"config file for in ini format" no-ini:"true"` | |
ConfigFile func(s string) error `short:"c" long:"config" description:"config file for in ini format" no-ini:"true" default:"ft.ini"` | |
} | |
func main() { | |
var opts Opts | |
fp := flag.NewParser(&opts, flag.PrintErrors) | |
// read the config file if present | |
opts.ConfigFile = func(s string) error { | |
ini := flag.NewIniParser(fp) | |
ini.ParseAsDefaults = true | |
return ini.ParseFile(s) | |
} | |
fp.Parse() | |
fmt.Printf("options: %+v\n", opts) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment