| 1 | package flags |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "unicode/utf8" |
| 7 | ) |
| 8 | |
| 9 | // Option flag information. Contains a description of the option, short and |
| 10 | // long name as well as a default value and whether an argument for this |
| 11 | // flag is optional. |
| 12 | type Option struct { |
| 13 | // The short name of the option (a single character). If not 0, the |
| 14 | // option flag can be 'activated' using -<ShortName>. Either ShortName |
| 15 | // or LongName needs to be non-empty. |
| 16 | ShortName rune |
| 17 | |
| 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 | |
| 23 | // The description of the option flag. This description is shown |
| 24 | // automatically in the builtin help. |
| 25 | Description string |
| 26 | |
| 27 | // The default value of the option. |
| 28 | Default []string |
| 29 | |
| 30 | // If true, specifies that the argument to an option flag is optional. |
| 31 | // When no argument to the flag is specified on the command line, the |
| 32 | // value of Default will be set in the field this option represents. |
| 33 | // This is only valid for non-boolean options. |
| 34 | OptionalArgument bool |
| 35 | |
| 36 | // The optional value of the option. The optional value is used when |
| 37 | // the option flag is marked as having an OptionalArgument. This means |
| 38 | // that when the flag is specified, but no option argument is given, |
| 39 | // the value of the field this option represents will be set to |
| 40 | // OptionalValue. This is only valid for non-boolean options. |
| 41 | OptionalValue []string |
| 42 | |
| 43 | // If true, the option _must_ be specified on the command line. If the |
| 44 | // option is not specified, the parser will generate an ErrRequired type |
| 45 | // error. |
| 46 | Required bool |
| 47 | |
| 48 | // A name for the value of an option shown in the Help as --flag [ValueName] |
| 49 | ValueName string |
| 50 | |
| 51 | // The struct field which the option represents. |
| 52 | Field reflect.StructField |
| 53 | |
| 54 | // The struct field value which the option represents. |
| 55 | Value reflect.Value |
| 56 | |
| 57 | defaultValue reflect.Value |
| 58 | defaultMask string |
| 59 | iniUsedName string |
| 60 | tag multiTag |
| 61 | } |
| 62 | |
| 63 | // Set the value of an option to the specified value. An error will be returned |
| 64 | // if the specified value could not be converted to the corresponding option |
| 65 | // value type. |
| 66 | func (option *Option) Set(value *string) error { |
| 67 | if option.isFunc() { |
| 68 | return option.call(value) |
| 69 | } else if value != nil { |
| 70 | return convert(*value, option.Value, option.tag) |
| 71 | } else { |
| 72 | return convert("", option.Value, option.tag) |
| 73 | } |
| 74 | |
| 75 | return nil |
| 76 | } |
| 77 | |
| 78 | // Convert an option to a human friendly readable string describing the option. |
| 79 | func (option *Option) String() string { |
| 80 | var s string |
| 81 | var short string |
| 82 | |
| 83 | if option.ShortName != 0 { |
| 84 | data := make([]byte, utf8.RuneLen(option.ShortName)) |
| 85 | utf8.EncodeRune(data, option.ShortName) |
| 86 | short = string(data) |
| 87 | |
| 88 | if len(option.LongName) != 0 { |
| 89 | s = fmt.Sprintf("-%s, --%s", short, option.LongName) |
| 90 | } else { |
| 91 | s = fmt.Sprintf("-%s", short) |
| 92 | } |
| 93 | } else if len(option.LongName) != 0 { |
| 94 | s = fmt.Sprintf("--%s", option.LongName) |
| 95 | } |
| 96 | |
| 97 | return s |
| 98 | } |