Skip to content

Instantly share code, notes, and snippets.

@maplebed
Created September 26, 2016 18:54
Show Gist options
  • Save maplebed/c450d53336a0ecdb10675725b9be9909 to your computer and use it in GitHub Desktop.
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
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