summaryrefslogtreecommitdiff
path: root/vendor/github.com/pborman/getopt/v2/util_test.go
blob: d46e19842b7b04ffaa2207a65046a7cd7e1b34fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2017 Google Inc.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package getopt

import (
	"bytes"
	"fmt"
	"path"
	"reflect"
	"runtime"
	"strings"
)

var errorString string

func reset() {
	CommandLine.shortOptions = make(map[rune]*option)
	CommandLine.longOptions = make(map[string]*option)
	CommandLine.options = nil
	CommandLine.args = nil
	CommandLine.program = ""
	errorString = ""
}

func parse(args []string) {
	err := CommandLine.Getopt(args, nil)
	if err != nil {
		b := &bytes.Buffer{}

		fmt.Fprintln(b, CommandLine.program+": "+err.Error())
		CommandLine.PrintUsage(b)
		errorString = b.String()
	}
}

func badSlice(a, b []string) bool {
	if len(a) != len(b) {
		return true
	}
	for x, v := range a {
		if b[x] != v {
			return true
		}
	}
	return false
}

func loc() string {
	_, file, line, _ := runtime.Caller(1)
	return fmt.Sprintf("%s:%d", path.Base(file), line)
}

func (o *option) Equal(opt *option) bool {
	if o.value != nil && opt.value == nil {
		return false
	}
	if o.value == nil && opt.value != nil {
		return false
	}
	if o.value != nil && o.value.String() != opt.value.String() {
		return false
	}

	oc := *o
	optc := *opt
	oc.value = nil
	optc.value = nil
	return reflect.DeepEqual(&oc, &optc)
}

func checkError(err string) string {
	switch {
	case err == errorString:
		return ""
	case err == "":
		return fmt.Sprintf("unexpected error %q", errorString)
	case errorString == "":
		return fmt.Sprintf("did not get expected error %q", err)
	case !strings.HasPrefix(errorString, err):
		return fmt.Sprintf("got error %q, want %q", errorString, err)
	}
	return ""
}