-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
view.go
46 lines (39 loc) · 1.29 KB
/
view.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package arguments
// View represents the global command-line arguments which configure the view.
type View struct {
// NoColor is used to disable the use of terminal color codes in all
// output.
NoColor bool
// CompactWarnings is used to coalesce duplicate warnings, to reduce the
// level of noise when multiple instances of the same warning are raised
// for a configuration.
CompactWarnings bool
}
// ParseView processes CLI arguments, returning a View value and a
// possibly-modified slice of arguments. If any of the supported flags are
// found, they will be removed from the slice.
func ParseView(args []string) (*View, []string) {
common := &View{}
// Keep track of the length of the returned slice. When we find an
// argument we support, i will not be incremented.
i := 0
for _, v := range args {
switch v {
case "-no-color":
common.NoColor = true
case "-compact-warnings":
common.CompactWarnings = true
default:
// Unsupported argument: move left to the current position, and
// increment the index.
args[i] = v
i++
}
}
// Reduce the slice to the number of unsupported arguments. Any remaining
// to the right of i have already been moved left.
args = args[:i]
return common, args
}