-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption.go
86 lines (68 loc) · 1.65 KB
/
option.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
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
// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt
package getopt
import "strings"
const (
Required = 1 << iota
Optional
Flag
NoLongOpt
ExampleIsDefault
IsArg
Argument
Usage
Help
IsPassThrough
IsConfigFile
NoEnvHelp
IsSubCommand
)
type Option struct {
OptionDefinition string
Description string
Flags int
DefaultValue interface{}
}
func (option Option) eq(other Option) bool {
return option.OptionDefinition == other.OptionDefinition &&
option.Description == other.Description &&
option.Flags == other.Flags &&
option.DefaultValue == other.DefaultValue
}
func (option Option) neq(other Option) bool {
return !option.eq(other)
}
func (option Option) Key() (key string) {
return strings.Split(option.OptionDefinition, "|")[0]
}
func (option Option) LongOpt() (longOpt string) {
if option.Flags&NoLongOpt == 0 {
longOpt = option.Key()
}
return longOpt
}
func (option Option) HasLongOpt() (result bool) {
return option.LongOpt() != ""
}
func (option Option) ShortOpt() (shortOpt string) {
token := strings.Split(option.OptionDefinition, "|")
if len(token) > 1 {
shortOpt = token[1]
}
return shortOpt
}
func (option Option) HasShortOpt() (result bool) {
return option.ShortOpt() != ""
}
func (option Option) EnvVar() (envVar string) {
token := strings.Split(option.OptionDefinition, "|")
if len(token) > 2 {
envVar = token[2]
}
return envVar
}
func (option Option) HasEnvVar() (result bool) {
return option.EnvVar() != ""
}