| 1 | // Copyright 2013 Google Inc. 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 | |
| 5 | package getoptscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 10 | ) |
| 11 | |
| 12 | // An Option can be either a Flag or a Value |
| 13 | type Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# interface { |
| 14 | // Name returns the name of the option. If the option has been seen |
| 15 | // then the last way it was referenced (short or long) is returned |
| 16 | // otherwise if there is a short name then this will be the short name |
| 17 | // as a string, else it will be the long name. |
| 18 | Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.() string |
| 19 | |
| 20 | // IsFlag returns true if Option is a flag. |
| 21 | IsFlagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#IsFlag.() bool |
| 22 | |
| 23 | // Seen returns true if the flag was seen. |
| 24 | Seenscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Seen.() bool |
| 25 | |
| 26 | // Count returns the number of times the flag was seen. |
| 27 | Countscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Count.() int |
| 28 | |
| 29 | // String returns the last value the option was set to. |
| 30 | Stringscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#String.() string |
| 31 | |
| 32 | // Value returns the Value of the option. |
| 33 | Valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Value.() Valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Value# |
| 34 | |
| 35 | // SetOptional makes the value optional. The option and value are |
| 36 | // always a single argument. Either --option or --option=value. In |
| 37 | // the former case the value of the option does not change but the Set() |
| 38 | // will return true and the value returned by Count() is incremented. |
| 39 | // The short form is either -o or -ovalue. SetOptional returns |
| 40 | // the Option |
| 41 | SetOptionalscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#SetOptional.() Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# |
| 42 | |
| 43 | // SetFlag makes the value a flag. Flags are boolean values and |
| 44 | // normally do not taken a value. They are set to true when seen. |
| 45 | // If a value is passed in the long form then it must be on, case |
| 46 | // insensitive, one of "true", "false", "t", "f", "on", "off", "1", "0". |
| 47 | // SetFlag returns the Option |
| 48 | SetFlagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#SetFlag.() Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# |
| 49 | |
| 50 | // Reset resets the state of the option so it appears it has not |
| 51 | // yet been seen, including resetting the value of the option |
| 52 | // to its original default state. |
| 53 | Resetscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Reset.() |
| 54 | } |
| 55 | |
| 56 | type optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option# struct { |
| 57 | shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. rune // 0 means no short name |
| 58 | longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. string // "" means no long name |
| 59 | isLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#isLong. bool // True if they used the long name |
| 60 | flagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#flag. bool // true if a boolean flag |
| 61 | defvalscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#defval. string // default value |
| 62 | optionalscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#optional. bool // true if we take an optional value |
| 63 | helpscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#help. string // help message |
| 64 | wherescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#where. string // file where the option was defined |
| 65 | valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#value. Valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Value# // current value of option |
| 66 | countscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#count. int // number of times we have seen this option |
| 67 | namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#name. string // name of the value (for usage) |
| 68 | unamescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#uname. string // name of the option (for usage) |
| 69 | } |
| 70 | |
| 71 | // usageName returns the name of the option for printing usage lines in one |
| 72 | // of the following forms: |
| 73 | // |
| 74 | // -f |
| 75 | // --flag |
| 76 | // -f, --flag |
| 77 | // -s value |
| 78 | // --set=value |
| 79 | // -s, --set=value |
| 80 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) usageNamescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#usageName().() string { |
| 81 | // Don't print help messages if we have none and there is only one |
| 82 | // way to specify the option. |
| 83 | if o.helpscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#help. == "" && (o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. == 0 || o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. == "") { |
| 84 | return "" |
| 85 | } |
| 86 | n := "" |
| 87 | |
| 88 | switch { |
| 89 | case o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. != 0 && o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. == "": |
| 90 | n = "-" + string(o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short.) |
| 91 | case o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. == 0 && o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. != "": |
| 92 | n = " --" + o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. |
| 93 | case o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. != 0 && o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. != "": |
| 94 | n = "-" + string(o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short.) + ", --" + o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. |
| 95 | } |
| 96 | |
| 97 | switch { |
| 98 | case o.flagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#flag.: |
| 99 | return n |
| 100 | case o.optionalscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#optional.: |
| 101 | return n + "[=" + o.namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#name. + "]" |
| 102 | case o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. != "": |
| 103 | return n + "=" + o.namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#name. |
| 104 | } |
| 105 | return n + " " + o.namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#name. |
| 106 | } |
| 107 | |
| 108 | // sortName returns the name to sort the option on. |
| 109 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) sortNamescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#sortName().() string { |
| 110 | if o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. != 0 { |
| 111 | return string(o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short.) + o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. |
| 112 | } |
| 113 | return o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long.[:1] + o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. |
| 114 | } |
| 115 | |
| 116 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) Seenscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#Seen().() bool { return o.countscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#count. > 0 } |
| 117 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) Countscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#Count().() int { return o.countscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#count. } |
| 118 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) IsFlagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#IsFlag().() bool { return o.flagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#flag. } |
| 119 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) Stringscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#String().() string { return o.valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#value..Stringscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Value#String.() } |
| 120 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) SetOptionalscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#SetOptional().() Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# { o.optionalscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#optional. = true; return o } |
| 121 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) SetFlagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#SetFlag().() Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# { o.flagscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#flag. = true; return o } |
| 122 | |
| 123 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) Valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#Value().() Valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Value# { |
| 124 | if o == nil { |
| 125 | return nil |
| 126 | } |
| 127 | return o.valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#value. |
| 128 | } |
| 129 | |
| 130 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#Name().() string { |
| 131 | if !o.isLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#isLong. && o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. != 0 { |
| 132 | return "-" + string(o.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short.) |
| 133 | } |
| 134 | return "--" + o.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. |
| 135 | } |
| 136 | |
| 137 | // Reset rests an option so that it appears it has not yet been seen. |
| 138 | func (o *optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) Resetscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#Reset().() { |
| 139 | o.isLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#isLong. = false |
| 140 | o.countscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#count. = 0 |
| 141 | o.valuescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#value..Setscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Value#Set.(o.defvalscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#defval., o) |
| 142 | } |
| 143 | |
| 144 | type optionListscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/optionList# []*optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option# |
| 145 | |
| 146 | func (ol optionListscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/optionList#) Lenscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/optionList#Len().() int { return len(ol) } |
| 147 | func (ol optionListscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/optionList#) Swapscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/optionList#Swap().(i, j int) { ol[i], ol[j] = ol[j], ol[i] } |
| 148 | func (ol optionListscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/optionList#) Lessscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/optionList#Less().(i, j int) bool { |
| 149 | // first check the short names (or the first letter of the long name) |
| 150 | // If they are not equal (case insensitive) then we have our answer |
| 151 | n1 := ol[i].sortNamescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#sortName().() |
| 152 | n2 := ol[j].sortNamescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#sortName().() |
| 153 | l1 := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(n1) |
| 154 | l2 := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(n2) |
| 155 | if l1 < l2 { |
| 156 | return true |
| 157 | } |
| 158 | if l2 < l1 { |
| 159 | return false |
| 160 | } |
| 161 | return n1 < n2 |
| 162 | } |
| 163 | |
| 164 | // AddOption add the option o to set CommandLine if o is not already in set |
| 165 | // CommandLine. |
| 166 | func AddOptionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/AddOption().(o Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#) { |
| 167 | CommandLinescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/CommandLine..AddOptionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#AddOption().(o) |
| 168 | } |
| 169 | |
| 170 | // AddOption add the option o to set s if o is not already in set s. |
| 171 | func (s *Setscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#) AddOptionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#AddOption().(o Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#) { |
| 172 | opt := o.(*optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#) |
| 173 | for _, eopt := range s.optionsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#options. { |
| 174 | if opt == eopt { |
| 175 | return |
| 176 | } |
| 177 | } |
| 178 | if opt.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short. != 0 { |
| 179 | if oo, ok := s.shortOptionsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#shortOptions.[opt.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short.]; ok { |
| 180 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(stderrscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/stderr., "%s: -%c already declared at %s\n", opt.wherescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#where., opt.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short., oo.wherescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#where.) |
| 181 | exitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/exit.(1) |
| 182 | } |
| 183 | s.shortOptionsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#shortOptions.[opt.shortscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#short.] = opt |
| 184 | } |
| 185 | if opt.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long. != "" { |
| 186 | if oo, ok := s.longOptionsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#longOptions.[opt.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long.]; ok { |
| 187 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(stderrscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/stderr., "%s: --%s already declared at %s\n", opt.wherescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#where., opt.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long., oo.wherescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#where.) |
| 188 | exitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/exit.(1) |
| 189 | } |
| 190 | s.longOptionsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#longOptions.[opt.longscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/option#long.] = opt |
| 191 | } |
| 192 | s.optionsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#options. = append(s.optionsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#options., opt) |
| 193 | } |