Repository navigation
Expand file tree
/
Copy patharguments.go
More file actions
75 lines (68 loc) · 1.99 KB
/
Copy patharguments.go
File metadata and controls
75 lines (68 loc) · 1.99 KB
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
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).
package props
import (
"os"
"strings"
)
// Arguments reads properties from the command line arguments.
// Property arguments are expected to have a common prefix and use key=value
// format. Other arguments are ignored.
//
// For example, the command:
//
// cmd -a -1 -z --prop.1=a --prop.2=b --prop.3 --log=debug
//
// with a prefix of '--prop.' would have properties "1"="a", "2"="b", and
// "3"="".
type Arguments struct {
// Prefix provides the common prefix to use when looking for property
// arguments. If not set, the default of '--' will be used.
Prefix string
}
// Ensure that Arguments implements PropertyGetter
var _ PropertyGetter = &Arguments{}
// Get retrieves the value of a property from the command line arguments. If
// the property does not exist, an empty string will be returned. The bool
// return value indicates whether the property was found.
func (a *Arguments) Get(key string) (string, bool) {
prefix := a.Prefix
if prefix == "" {
prefix = "--"
}
prefix = prefix + key + "="
for _, val := range os.Args {
if strings.HasPrefix(val, prefix) {
return val[len(prefix):], true
}
}
return "", false
}
// GetDefault retrieves the value of a property from the command line arguments.
// If the property does not exist, then the default value will be returned.
func (a *Arguments) GetDefault(key, defVal string) string {
v, ok := a.Get(key)
if !ok {
return defVal
}
return v
}
// Names retrieves the property values set from the command line arguments.
// The returned names don't include the prefix.
// If no values were set, an empty slice is returned.
func (a *Arguments) Names() []string {
result := make([]string, 0, 8)
prefix := a.Prefix
if prefix == "" {
prefix = "--"
}
for _, val := range os.Args {
if strings.HasPrefix(val, prefix) {
val = val[len(prefix):]
if strings.Contains(val, "=") {
val = val[0:strings.Index(val, "=")]
}
result = append(result, val)
}
}
return result
}