This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
option.go
181 lines (155 loc) · 4.01 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
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
package commands
import (
"reflect"
"github.com/ipfs/go-commands/util"
)
// Types of Command options
const (
Invalid = reflect.Invalid
Bool = reflect.Bool
Int = reflect.Int
Uint = reflect.Uint
Float = reflect.Float64
String = reflect.String
)
// Option is used to specify a field that will be provided by a consumer
type Option interface {
Names() []string // a list of unique names matched with user-provided flags
Type() reflect.Kind // value must be this type
Description() string // a short string that describes this option
}
type option struct {
names []string
kind reflect.Kind
description string
}
func (o *option) Names() []string {
return o.names
}
func (o *option) Type() reflect.Kind {
return o.kind
}
func (o *option) Description() string {
return o.description
}
// constructor helper functions
func NewOption(kind reflect.Kind, names ...string) Option {
if len(names) < 2 {
// FIXME(btc) don't panic (fix_before_merge)
panic("Options require at least two string values (name and description)")
}
desc := names[len(names)-1]
names = names[:len(names)-1]
return &option{
names: names,
kind: kind,
description: desc,
}
}
// TODO handle description separately. this will take care of the panic case in
// NewOption
// For all func {Type}Option(...string) functions, the last variadic argument
// is treated as the description field.
func BoolOption(names ...string) Option {
return NewOption(Bool, names...)
}
func IntOption(names ...string) Option {
return NewOption(Int, names...)
}
func UintOption(names ...string) Option {
return NewOption(Uint, names...)
}
func FloatOption(names ...string) Option {
return NewOption(Float, names...)
}
func StringOption(names ...string) Option {
return NewOption(String, names...)
}
type OptionValue struct {
value interface{}
found bool
def Option
}
// Found returns true if the option value was provided by the user (not a default value)
func (ov OptionValue) Found() bool {
return ov.found
}
// Definition returns the option definition for the provided value
func (ov OptionValue) Definition() Option {
return ov.def
}
// value accessor methods, gets the value as a certain type
func (ov OptionValue) Bool() (value bool, found bool, err error) {
if !ov.found {
return false, false, nil
}
val, ok := ov.value.(bool)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
func (ov OptionValue) Int() (value int, found bool, err error) {
if !ov.found {
return 0, false, nil
}
val, ok := ov.value.(int)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
func (ov OptionValue) Uint() (value uint, found bool, err error) {
if !ov.found {
return 0, false, nil
}
val, ok := ov.value.(uint)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
func (ov OptionValue) Float() (value float64, found bool, err error) {
if !ov.found {
return 0, false, nil
}
val, ok := ov.value.(float64)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
func (ov OptionValue) String() (value string, found bool, err error) {
if !ov.found {
return "", false, nil
}
val, ok := ov.value.(string)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
// Flag names
const (
EncShort = "enc"
EncLong = "encoding"
RecShort = "r"
RecLong = "recursive"
ChanOpt = "stream-channels"
TimeoutOpt = "timeout"
)
// options that are used by this package
var OptionEncodingType = StringOption(EncShort, EncLong, "The encoding type the output should be encoded with (json, xml, or text)")
var OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively")
var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output")
var OptionTimeout = StringOption(TimeoutOpt, "set a global timeout on the command")
// global options, added to every command
var globalOptions = []Option{
OptionEncodingType,
OptionStreamChannels,
OptionTimeout,
}
// the above array of Options, wrapped in a Command
var globalCommand = &Command{
Options: globalOptions,
}