示例#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
// 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
}