| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package getopt |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"path" |
| 11 |
"reflect" |
| 12 |
"runtime" |
| 13 |
"strings" |
| 14 |
) |
| 15 |
|
| 16 |
var errorString string |
| 17 |
|
| 18 |
func reset() { |
| 19 |
CommandLine.shortOptions = make(map[rune]*option) |
| 20 |
CommandLine.longOptions = make(map[string]*option) |
| 21 |
CommandLine.options = nil |
| 22 |
CommandLine.args = nil |
| 23 |
CommandLine.program = "" |
| 24 |
CommandLine.requiredGroups = nil |
| 25 |
errorString = "" |
| 26 |
} |
| 27 |
|
| 28 |
func parse(args []string) { |
| 29 |
err := CommandLine.Getopt(args, nil) |
| 30 |
if err != nil { |
| 31 |
b := &bytes.Buffer{} |
| 32 |
|
| 33 |
fmt.Fprintln(b, CommandLine.program+": "+err.Error()) |
| 34 |
CommandLine.PrintUsage(b) |
| 35 |
errorString = b.String() |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func badSlice(a, b []string) bool { |
| 40 |
if len(a) != len(b) { |
| 41 |
return true |
| 42 |
} |
| 43 |
for x, v := range a { |
| 44 |
if b[x] != v { |
| 45 |
return true |
| 46 |
} |
| 47 |
} |
| 48 |
return false |
| 49 |
} |
| 50 |
|
| 51 |
func loc() string { |
| 52 |
_, file, line, _ := runtime.Caller(1) |
| 53 |
return fmt.Sprintf("%s:%d", path.Base(file), line) |
| 54 |
} |
| 55 |
|
| 56 |
func (o *option) Equal(opt *option) bool { |
| 57 |
if o.value != nil && opt.value == nil { |
| 58 |
return false |
| 59 |
} |
| 60 |
if o.value == nil && opt.value != nil { |
| 61 |
return false |
| 62 |
} |
| 63 |
if o.value != nil && o.value.String() != opt.value.String() { |
| 64 |
return false |
| 65 |
} |
| 66 |
|
| 67 |
oc := *o |
| 68 |
optc := *opt |
| 69 |
oc.value = nil |
| 70 |
optc.value = nil |
| 71 |
return reflect.DeepEqual(&oc, &optc) |
| 72 |
} |
| 73 |
|
| 74 |
func checkError(err string) string { |
| 75 |
switch { |
| 76 |
case err == errorString: |
| 77 |
return "" |
| 78 |
case err == "": |
| 79 |
return fmt.Sprintf("unexpected error %q", errorString) |
| 80 |
case errorString == "": |
| 81 |
return fmt.Sprintf("did not get expected error %q", err) |
| 82 |
case !strings.HasPrefix(errorString, err): |
| 83 |
return fmt.Sprintf("got error %q, want %q", errorString, err) |
| 84 |
} |
| 85 |
return "" |
| 86 |
} |