本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
781B

  1. // Copyright 2018 Frank Schroeder. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package properties
  5. import "flag"
  6. // MustFlag sets flags that are skipped by dst.Parse when p contains
  7. // the respective key for flag.Flag.Name.
  8. //
  9. // It's use is recommended with command line arguments as in:
  10. // flag.Parse()
  11. // p.MustFlag(flag.CommandLine)
  12. func (p *Properties) MustFlag(dst *flag.FlagSet) {
  13. m := make(map[string]*flag.Flag)
  14. dst.VisitAll(func(f *flag.Flag) {
  15. m[f.Name] = f
  16. })
  17. dst.Visit(func(f *flag.Flag) {
  18. delete(m, f.Name) // overridden
  19. })
  20. for name, f := range m {
  21. v, ok := p.Get(name)
  22. if !ok {
  23. continue
  24. }
  25. if err := f.Value.Set(v); err != nil {
  26. ErrorHandler(err)
  27. }
  28. }
  29. }
上海开阖软件有限公司 沪ICP备12045867号-1