File: flags.go

package info (click to toggle)
golang-gopkg-alecthomas-kingpin.v3 3.0~git20180227.b8d601d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 420 kB
  • sloc: makefile: 6
file content (141 lines) | stat: -rw-r--r-- 3,143 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package kingpin

import "strings"

type flagGroup struct {
	short     map[string]*Clause
	long      map[string]*Clause
	flagOrder []*Clause
}

func newFlagGroup() *flagGroup {
	return &flagGroup{
		short: map[string]*Clause{},
		long:  map[string]*Clause{},
	}
}

// GetFlag gets a flag definition.
//
// This allows existing flags to be modified after definition but before parsing. Useful for
// modular applications.
func (f *flagGroup) GetFlag(name string) *Clause {
	return f.long[name]
}

// Flag defines a new flag with the given long name and help.
func (f *flagGroup) Flag(name, help string) *Clause {
	flag := NewClause(name, help)
	f.long[name] = flag
	f.flagOrder = append(f.flagOrder, flag)
	return flag
}

func (f *flagGroup) init() error {
	if err := f.checkDuplicates(); err != nil {
		return err
	}
	for _, flag := range f.long {
		if err := flag.init(); err != nil {
			return err
		}
		if flag.shorthand != 0 {
			f.short[string(flag.shorthand)] = flag
		}
	}
	return nil
}

func (f *flagGroup) checkDuplicates() error {
	seenShort := map[rune]bool{}
	seenLong := map[string]bool{}
	for _, flag := range f.flagOrder {
		if flag.shorthand != 0 {
			if _, ok := seenShort[flag.shorthand]; ok {
				return TError("duplicate short flag -{{.Arg0}}", V{"Arg0": flag.shorthand})
			}
			seenShort[flag.shorthand] = true
		}
		if _, ok := seenLong[flag.name]; ok {
			return TError("duplicate long flag --{{.Arg0}}", V{"Arg0": flag.name})
		}
		seenLong[flag.name] = true
	}
	return nil
}

func (f *flagGroup) parse(context *ParseContext) (*Clause, error) {
	var token *Token

loop:
	for {
		token = context.Peek()
		switch token.Type {
		case TokenEOL:
			break loop

		case TokenLong, TokenShort:
			flagToken := token
			var flag *Clause
			var ok bool
			invert := false

			name := token.Value
			if token.Type == TokenLong {
				flag, ok = f.long[name]
				if !ok {
					if strings.HasPrefix(name, "no-") {
						name = name[3:]
						invert = true
						flag, ok = f.long[name]
						// Found an inverted flag. Check if the flag supports it.
						if ok {
							bf, bok := flag.value.(BoolFlag)
							ok = bok && bf.BoolFlagIsNegatable()
						}
					}
				} else if strings.HasPrefix(name, "no-") {
					invert = true
				}
				if !ok {
					return nil, TError("unknown long flag '{{.Arg0}}'", V{"Arg0": flagToken})
				}
			} else {
				flag, ok = f.short[name]
				if !ok {
					return nil, TError("unknown short flag '{{.Arg0}}'", V{"Arg0": flagToken})
				}
			}

			context.Next()

			var defaultValue string
			if isBoolFlag(flag.value) {
				if invert {
					defaultValue = "false"
				} else {
					defaultValue = "true"
				}
			} else {
				if invert {
					context.Push(token)
					return nil, TError("unknown long flag '{{.Arg0}}'", V{"Arg0": flagToken})
				}
				token = context.Peek()
				if token.Type != TokenArg {
					context.Push(token)
					return nil, TError("expected argument for flag '{{.Arg0}}'", V{"Arg0": flagToken})
				}
				context.Next()
				defaultValue = token.Value
			}

			context.matchedFlag(flag, defaultValue)
			return flag, nil

		default:
			break loop
		}
	}
	return nil, nil
}