summaryrefslogtreecommitdiff
path: root/vendor/github.com/pborman/getopt/v2/set.go
blob: ce546ed4e2f8f468d436d544edd7f9044c4ddabf (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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// 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 (
	"io"
	"os"
	"sort"
	"sync"
)

// A State is why the Getopt returned.
type State int

const (
	InProgress     = State(iota) // Getopt is still running
	Dash                         // Returned on "-"
	DashDash                     // Returned on "--"
	EndOfOptions                 // End of options reached
	EndOfArguments               // No more arguments
	Terminated                   // Terminated by callback function
	Failure                      // Terminated due to error
	Unknown                      // Indicates internal error
)

type Set struct {
	stateMu sync.Mutex
	state   State

	// args are the parameters remaining after parsing the optoins.
	args []string

	// program is the name of the program for usage and error messages.
	// If not set it will automatically be set to the base name of the
	// first argument passed to parse.
	program string

	// parameters is what is displayed on the usage line after displaying
	// the various options.
	parameters string

	usage func() // usage should print the programs usage and exit.

	shortOptions map[rune]*option
	longOptions  map[string]*option
	options      optionList
}

// New returns a newly created option set.
func New() *Set {
	s := &Set{
		shortOptions: make(map[rune]*option),
		longOptions:  make(map[string]*option),
		parameters:   "[parameters ...]",
	}

	s.usage = func() {
		s.PrintUsage(stderr)
	}
	return s
}

func (s *Set) setState(state State) {
	s.stateMu.Lock()
	s.state = state
	s.stateMu.Unlock()
}

// State returns the current state of the Set s.  The state is normally the
// reason the most recent call to Getopt returned.
func (s *Set) State() State {
	s.stateMu.Lock()
	defer s.stateMu.Unlock()
	return s.state
}

// The default set of command-line options.
var CommandLine = New()

// PrintUsage calls PrintUsage in the default option set.
func PrintUsage(w io.Writer) { CommandLine.PrintUsage(w) }

// Usage calls the usage function in the default option set.
func Usage() { CommandLine.usage() }

// Parse calls Parse in the default option set with the command line arguments
// found in os.Args.
func Parse() { CommandLine.Parse(os.Args) }

// Getops returns the result of calling Getop in the default option set with the
// command line arguments found in os.Args.  The fn function, which may be nil,
// is passed to Getopt.
func Getopt(fn func(Option) bool) error { return CommandLine.Getopt(os.Args, fn) }

// Arg returns the n'th command-line argument. Arg(0) is the first remaining
// argument after options have been processed.
func Arg(n int) string {
	if n >= 0 && n < len(CommandLine.args) {
		return CommandLine.args[n]
	}
	return ""
}

// Arg returns the n'th argument. Arg(0) is the first remaining
// argument after options have been processed.
func (s *Set) Arg(n int) string {
	if n >= 0 && n < len(s.args) {
		return s.args[n]
	}
	return ""
}

// Args returns the non-option command line arguments.
func Args() []string {
	return CommandLine.args
}

// Args returns the non-option arguments.
func (s *Set) Args() []string {
	return s.args
}

// NArgs returns the number of non-option command line arguments.
func NArgs() int {
	return len(CommandLine.args)
}

// NArgs returns the number of non-option arguments.
func (s *Set) NArgs() int {
	return len(s.args)
}

// SetParameters sets the parameters string for printing the command line
// usage.  It defaults to "[parameters ...]"
func SetParameters(parameters string) {
	CommandLine.parameters = parameters
}

// SetParameters sets the parameters string for printing the s's usage.
// It defaults to "[parameters ...]"
func (s *Set) SetParameters(parameters string) {
	s.parameters = parameters
}

// SetProgram sets the program name to program.  Normally it is determined
// from the zeroth command line argument (see os.Args).
func SetProgram(program string) {
	CommandLine.program = program
}

// SetProgram sets s's program name to program.  Normally it is determined
// from the zeroth argument passed to Getopt or Parse.
func (s *Set) SetProgram(program string) {
	s.program = program
}

// SetUsage sets the function used by Parse to display the commands usage
// on error.  It defaults to calling PrintUsage(os.Stderr).
func SetUsage(usage func()) {
	CommandLine.usage = usage
}

// SetUsage sets the function used by Parse to display usage on error.  It
// defaults to calling f.PrintUsage(os.Stderr).
func (s *Set) SetUsage(usage func()) {
	s.usage = usage
}

// Lookup returns the Option associated with name.  Name should either be
// a rune (the short name) or a string (the long name).
func Lookup(name interface{}) Option {
	return CommandLine.Lookup(name)
}

// Lookup returns the Option associated with name in s.  Name should either be
// a rune (the short name) or a string (the long name).
func (s *Set) Lookup(name interface{}) Option {
	switch v := name.(type) {
	case rune:
		return s.shortOptions[v]
	case int:
		return s.shortOptions[rune(v)]
	case string:
		return s.longOptions[v]
	}
	return nil
}

// IsSet returns true if the Option associated with name was seen while
// parsing the command line arguments.  Name should either be a rune (the
// short name) or a string (the long name).
func IsSet(name interface{}) bool {
	return CommandLine.IsSet(name)
}

// IsSet returns true if the Option associated with name was seen while
// parsing s.  Name should either be a rune (the short name) or a string (the
// long name).
func (s *Set) IsSet(name interface{}) bool {
	if opt := s.Lookup(name); opt != nil {
		return opt.Seen()
	}
	return false
}

// GetCount returns the number of times the Option associated with name has been
// seen while parsing the command line arguments.  Name should either be a rune
// (the short name) or a string (the long name).
func GetCount(name interface{}) int {
	return CommandLine.GetCount(name)
}

// GetCount returns the number of times the Option associated with name has been
// seen while parsing s's arguments.  Name should either be a rune (the short
// name) or a string (the long name).
func (s *Set) GetCount(name interface{}) int {
	if opt := s.Lookup(name); opt != nil {
		return opt.Count()
	}
	return 0
}

// GetValue returns the final value set to the command-line Option with name.
// If the option has not been seen while parsing s then the default value is
// returned.  Name should either be a rune (the short name) or a string (the
// long name).
func GetValue(name interface{}) string {
	return CommandLine.GetValue(name)
}

// GetValue returns the final value set to the Option in s associated with name.
// If the option has not been seen while parsing s then the default value is
// returned.  Name should either be a rune (the short name) or a string (the
// long name).
func (s *Set) GetValue(name interface{}) string {
	if opt := s.Lookup(name); opt != nil {
		return opt.String()
	}
	return ""
}

// Visit visits the command-line options in lexicographical order, calling fn
// for each. It visits only those options that have been set.
func Visit(fn func(Option)) { CommandLine.Visit(fn) }

// Visit visits the options in s in lexicographical order, calling fn
// for each. It visits only those options that have been set.
func (s *Set) Visit(fn func(Option)) {
	sort.Sort(s.options)
	for _, opt := range s.options {
		if opt.count > 0 {
			fn(opt)
		}
	}
}

// VisitAll visits the options in s in lexicographical order, calling fn
// for each. It visits all options, even those not set.
func VisitAll(fn func(Option)) { CommandLine.VisitAll(fn) }

// VisitAll visits the command-line flags in lexicographical order, calling fn
// for each. It visits all flags, even those not set.
func (s *Set) VisitAll(fn func(Option)) {
	sort.Sort(s.options)
	for _, opt := range s.options {
		fn(opt)
	}
}

// Reset resets all the command line options to the initial state so it
// appears none of them have been seen.
func Reset() {
	CommandLine.Reset()
}

// Reset resets all the options in s to the initial state so it
// appears none of them have been seen.
func (s *Set) Reset() {
	for _, opt := range s.options {
		opt.Reset()
	}
}