option.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package flags
  2. import (
  3. "fmt"
  4. "reflect"
  5. "unicode/utf8"
  6. )
  7. // Option flag information. Contains a description of the option, short and
  8. // long name as well as a default value and whether an argument for this
  9. // flag is optional.
  10. type Option struct {
  11. // The description of the option flag. This description is shown
  12. // automatically in the builtin help.
  13. Description string
  14. // The short name of the option (a single character). If not 0, the
  15. // option flag can be 'activated' using -<ShortName>. Either ShortName
  16. // or LongName needs to be non-empty.
  17. ShortName rune
  18. // The long name of the option. If not "", the option flag can be
  19. // activated using --<LongName>. Either ShortName or LongName needs
  20. // to be non-empty.
  21. LongName string
  22. // The default value of the option.
  23. Default []string
  24. // If true, specifies that the argument to an option flag is optional.
  25. // When no argument to the flag is specified on the command line, the
  26. // value of Default will be set in the field this option represents.
  27. // This is only valid for non-boolean options.
  28. OptionalArgument bool
  29. // The optional value of the option. The optional value is used when
  30. // the option flag is marked as having an OptionalArgument. This means
  31. // that when the flag is specified, but no option argument is given,
  32. // the value of the field this option represents will be set to
  33. // OptionalValue. This is only valid for non-boolean options.
  34. OptionalValue []string
  35. // If true, the option _must_ be specified on the command line. If the
  36. // option is not specified, the parser will generate an ErrRequired type
  37. // error.
  38. Required bool
  39. // A name for the value of an option shown in the Help as --flag [ValueName]
  40. ValueName string
  41. // A mask value to show in the help instead of the default value. This
  42. // is useful for hiding sensitive information in the help, such as
  43. // passwords.
  44. DefaultMask string
  45. // The struct field which the option represents.
  46. field reflect.StructField
  47. // The struct field value which the option represents.
  48. value reflect.Value
  49. defaultValue reflect.Value
  50. iniUsedName string
  51. tag multiTag
  52. }
  53. // String converts an option to a human friendly readable string describing the
  54. // option.
  55. func (option *Option) String() string {
  56. var s string
  57. var short string
  58. if option.ShortName != 0 {
  59. data := make([]byte, utf8.RuneLen(option.ShortName))
  60. utf8.EncodeRune(data, option.ShortName)
  61. short = string(data)
  62. if len(option.LongName) != 0 {
  63. s = fmt.Sprintf("%s%s, %s%s",
  64. string(defaultShortOptDelimiter), short,
  65. defaultLongOptDelimiter, option.LongName)
  66. } else {
  67. s = fmt.Sprintf("%s%s", string(defaultShortOptDelimiter), short)
  68. }
  69. } else if len(option.LongName) != 0 {
  70. s = fmt.Sprintf("%s%s", defaultLongOptDelimiter, option.LongName)
  71. }
  72. return s
  73. }
  74. // Value returns the option value as an interface{}.
  75. func (option *Option) Value() interface{} {
  76. return option.value.Interface()
  77. }