| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package getopt |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
type Option interface { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
Name() string |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
ShortName() string |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
LongName() string |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
Help() string |
| 31 |
|
| 32 |
|
| 33 |
IsFlag() bool |
| 34 |
|
| 35 |
|
| 36 |
Seen() bool |
| 37 |
|
| 38 |
|
| 39 |
Count() int |
| 40 |
|
| 41 |
|
| 42 |
String() string |
| 43 |
|
| 44 |
|
| 45 |
Value() Value |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
SetOptional() Option |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
SetFlag() Option |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
Reset() |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
Mandatory() Option |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
SetGroup(string) Option |
| 74 |
} |
| 75 |
|
| 76 |
type option struct { |
| 77 |
short rune |
| 78 |
long string |
| 79 |
isLong bool |
| 80 |
flag bool |
| 81 |
defval string |
| 82 |
optional bool |
| 83 |
help string |
| 84 |
where string |
| 85 |
value Value |
| 86 |
count int |
| 87 |
name string |
| 88 |
uname string |
| 89 |
mandatory bool |
| 90 |
group string |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func (o *option) usageName() string { |
| 103 |
|
| 104 |
|
| 105 |
if o.help == "" && (o.short == 0 || o.long == "") { |
| 106 |
return "" |
| 107 |
} |
| 108 |
n := "" |
| 109 |
|
| 110 |
switch { |
| 111 |
case o.short != 0 && o.long == "": |
| 112 |
n = "-" + string(o.short) |
| 113 |
case o.short == 0 && o.long != "": |
| 114 |
n = " --" + o.long |
| 115 |
case o.short != 0 && o.long != "": |
| 116 |
n = "-" + string(o.short) + ", --" + o.long |
| 117 |
} |
| 118 |
|
| 119 |
switch { |
| 120 |
case o.flag: |
| 121 |
return n |
| 122 |
case o.optional: |
| 123 |
return n + "[=" + o.name + "]" |
| 124 |
case o.long != "": |
| 125 |
return n + "=" + o.name |
| 126 |
} |
| 127 |
return n + " " + o.name |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (o *option) sortName() string { |
| 132 |
if o.short != 0 { |
| 133 |
return string(o.short) + o.long |
| 134 |
} |
| 135 |
return o.long[:1] + o.long |
| 136 |
} |
| 137 |
|
| 138 |
func (o *option) Seen() bool { return o.count > 0 } |
| 139 |
func (o *option) Count() int { return o.count } |
| 140 |
func (o *option) IsFlag() bool { return o.flag } |
| 141 |
func (o *option) String() string { return o.value.String() } |
| 142 |
func (o *option) SetOptional() Option { o.optional = true; return o } |
| 143 |
func (o *option) SetFlag() Option { o.flag = true; return o } |
| 144 |
func (o *option) Mandatory() Option { o.mandatory = true; return o } |
| 145 |
func (o *option) SetGroup(g string) Option { o.group = g; return o } |
| 146 |
|
| 147 |
func (o *option) Value() Value { |
| 148 |
if o == nil { |
| 149 |
return nil |
| 150 |
} |
| 151 |
return o.value |
| 152 |
} |
| 153 |
|
| 154 |
func (o *option) Name() string { |
| 155 |
if !o.isLong && o.short != 0 { |
| 156 |
return "-" + string(o.short) |
| 157 |
} |
| 158 |
return "--" + o.long |
| 159 |
} |
| 160 |
|
| 161 |
func (o *option) ShortName() string { |
| 162 |
if o.short != 0 { |
| 163 |
return string(o.short) |
| 164 |
} |
| 165 |
return "" |
| 166 |
} |
| 167 |
|
| 168 |
func (o *option) LongName() string { |
| 169 |
return o.long |
| 170 |
} |
| 171 |
|
| 172 |
func (o *option) Help() string { |
| 173 |
return o.help |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
func (o *option) Reset() { |
| 178 |
o.isLong = false |
| 179 |
o.count = 0 |
| 180 |
o.value.Set(o.defval, o) |
| 181 |
} |
| 182 |
|
| 183 |
type optionList []*option |
| 184 |
|
| 185 |
func (ol optionList) Len() int { return len(ol) } |
| 186 |
func (ol optionList) Swap(i, j int) { ol[i], ol[j] = ol[j], ol[i] } |
| 187 |
func (ol optionList) Less(i, j int) bool { |
| 188 |
|
| 189 |
|
| 190 |
n1 := ol[i].sortName() |
| 191 |
n2 := ol[j].sortName() |
| 192 |
l1 := strings.ToLower(n1) |
| 193 |
l2 := strings.ToLower(n2) |
| 194 |
if l1 < l2 { |
| 195 |
return true |
| 196 |
} |
| 197 |
if l2 < l1 { |
| 198 |
return false |
| 199 |
} |
| 200 |
return n1 < n2 |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
func AddOption(o Option) { |
| 206 |
CommandLine.AddOption(o) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func (s *Set) AddOption(o Option) { |
| 211 |
opt := o.(*option) |
| 212 |
for _, eopt := range s.options { |
| 213 |
if opt == eopt { |
| 214 |
return |
| 215 |
} |
| 216 |
} |
| 217 |
if opt.short != 0 { |
| 218 |
if oo, ok := s.shortOptions[opt.short]; ok { |
| 219 |
fmt.Fprintf(stderr, "%s: -%c already declared at %s\n", opt.where, opt.short, oo.where) |
| 220 |
exit(1) |
| 221 |
} |
| 222 |
s.shortOptions[opt.short] = opt |
| 223 |
} |
| 224 |
if opt.long != "" { |
| 225 |
if oo, ok := s.longOptions[opt.long]; ok { |
| 226 |
fmt.Fprintf(stderr, "%s: --%s already declared at %s\n", opt.where, opt.long, oo.where) |
| 227 |
exit(1) |
| 228 |
} |
| 229 |
s.longOptions[opt.long] = opt |
| 230 |
} |
| 231 |
s.options = append(s.options, opt) |
| 232 |
} |