Пример #1
0
func main() {
	tgt := &Config{}
	c := cstruct.MustNew(tgt, "example")
	configurable.Register(c)

	adaptflag.AdaptWithFunc(func(info adaptflag.Info) {
		flag.Var(info.Value, info.Name, info.Usage)
	})

	flag.Parse()

	err := adaptconf.Load("exampleapp2")
	if err != nil {
		fmt.Printf("%v\n", err)
	}

	fmt.Printf("%#v\n", tgt)
}
Пример #2
0
func Example() {
	type Config struct {
		Bind    string `usage:"Address to bind server to (e.g. :80)" default:":80"`
		Foo     string `usage:"Some flag" default:""`
		Bar     int    `usage:"Some other flag" default:"42"`
		DoStuff bool   `usage:"Do stuff?" default:"false"`
	}

	cfg := &Config{}
	configurable.Register(cstruct.MustNew(cfg, "test"))
	adaptflag.Adapt()
	flag.Parse()

	fmt.Printf("Bind: %s\n", cfg.Bind)
	fmt.Printf("Foo:  %s\n", cfg.Foo)
	fmt.Printf("Bar:  %d\n", cfg.Bar)
	fmt.Printf("Do Stuff: %v\n", cfg.DoStuff)
}
Пример #3
0
// Parse configuration values. tgt should be a pointer to a structure to be
// filled using cstruct. If nil, no structure is registered using cstruct.
func (cfg *Configurator) Parse(tgt interface{}) error {
	if tgt != nil && cfg.ProgramName != "" {
		if exepath.ProgramNameSetter == "default" {
			exepath.ProgramName = cfg.ProgramName
		}

		configurable.Register(cstruct.MustNew(tgt, cfg.ProgramName))
	}

	adaptflag.Adapt()
	adaptenv.Adapt()
	flag.Parse()
	if cfg.ProgramName != "" {
		err := adaptconf.Load(cfg.ProgramName)
		if err != nil {
			return err
		}
	}

	cfg.configFilePath = adaptconf.LastConfPath()
	return nil
}