add default option value support to commands lib
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Mar 4, 2016 at 10:07 UTC
767ee13ea2ae6f2c351afb92837e86752046e245
4 files changed
+33
-27
commands/option.go
+28
-8
@@ -1,6 +1,7 @@
1
package commands
2
3
import (
4
+ "fmt"
5
"reflect"
6
7
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
@@ -18,15 +19,18 @@ const (
19
20
// Option is used to specify a field that will be provided by a consumer
21
type Option interface {
21
- Names() []string // a list of unique names matched with user-provided flags
22
- Type() reflect.Kind // value must be this type
23
- Description() string // a short string that describes this option
22
+ Names() []string // a list of unique names matched with user-provided flags
23
+ Type() reflect.Kind // value must be this type
24
+ Description() string // a short string that describes this option
25
+ Default(interface{}) Option // sets the default value of the option
26
+ DefaultVal() interface{}
27
}
28
29
type option struct {
30
names []string
31
kind reflect.Kind
32
description string
33
+ defaultVal interface{}
34
}
35
36
func (o *option) Names() []string {
@@ -38,6 +42,13 @@ func (o *option) Type() reflect.Kind {
42
}
43
44
func (o *option) Description() string {
45
+ if o.description[len(o.description)-1] != '.' {
46
+ o.description += "."
47
+ }
48
+
49
+ if o.defaultVal != nil {
50
+ return fmt.Sprintf("%s Default: %v.", o.description, o.defaultVal)
51
+ }
52
return o.description
53
}
54
@@ -58,6 +69,15 @@ func NewOption(kind reflect.Kind, names ...string) Option {
69
}
70
}
71
72
+func (o *option) Default(v interface{}) Option {
73
+ o.defaultVal = v
74
+ return o
75
+}
76
+
77
+func (o *option) DefaultVal() interface{} {
78
+ return o.defaultVal
79
+}
80
+
81
// TODO handle description separately. this will take care of the panic case in
82
// NewOption
83
@@ -98,7 +118,7 @@ func (ov OptionValue) Definition() Option {
118
119
// value accessor methods, gets the value as a certain type
120
func (ov OptionValue) Bool() (value bool, found bool, err error) {
101
- if !ov.found {
121
+ if !ov.found && ov.value == nil {
122
return false, false, nil
123
}
124
val, ok := ov.value.(bool)
@@ -109,7 +129,7 @@ func (ov OptionValue) Bool() (value bool, found bool, err error) {
129
}
130
131
func (ov OptionValue) Int() (value int, found bool, err error) {
112
- if !ov.found {
132
+ if !ov.found && ov.value == nil {
133
return 0, false, nil
134
}
135
val, ok := ov.value.(int)
@@ -120,7 +140,7 @@ func (ov OptionValue) Int() (value int, found bool, err error) {
140
}
141
142
func (ov OptionValue) Uint() (value uint, found bool, err error) {
123
- if !ov.found {
143
+ if !ov.found && ov.value == nil {
144
return 0, false, nil
145
}
146
val, ok := ov.value.(uint)
@@ -131,7 +151,7 @@ func (ov OptionValue) Uint() (value uint, found bool, err error) {
151
}
152
153
func (ov OptionValue) Float() (value float64, found bool, err error) {
134
- if !ov.found {
154
+ if !ov.found && ov.value == nil {
155
return 0, false, nil
156
}
157
val, ok := ov.value.(float64)
@@ -142,7 +162,7 @@ func (ov OptionValue) Float() (value float64, found bool, err error) {
162
}
163
164
func (ov OptionValue) String() (value string, found bool, err error) {
145
- if !ov.found {
165
+ if !ov.found && ov.value == nil {
166
return "", false, nil
167
}
168
val, ok := ov.value.(string)
commands/option_test.go
-7
@@ -9,13 +9,6 @@ func TestOptionValueExtractBoolNotFound(t *testing.T) {
9
if err != nil {
10
t.Fatal("Found was false. Err should have been nil")
11
}
12
-
13
- t.Log("ensure that no error is returned when value is not found (even if value exists)")
14
- optval = &OptionValue{value: "wrong type: a string", found: false}
15
- _, _, err = optval.Bool()
16
- if err != nil {
17
- t.Fatal("Found was false. Err should have been nil")
18
- }
12
}
13
14
func TestOptionValueExtractWrongType(t *testing.T) {
commands/request.go
+1
-2
@@ -118,8 +118,7 @@ func (r *request) Option(name string) *OptionValue {
118
}
119
}
120
121
- // MAYBE_TODO: use default value instead of nil
122
- return &OptionValue{nil, false, option}
121
+ return &OptionValue{option.DefaultVal(), false, option}
122
}
123
124
// Options returns a copy of the option map
core/commands/pin.go
+4
-10
@@ -41,7 +41,7 @@ var addPinCmd = &cmds.Command{
41
cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be pinned.").EnableStdin(),
42
},
43
Options: []cmds.Option{
44
- cmds.BoolOption("recursive", "r", "Recursively pin the object linked to by the specified object(s)."),
44
+ cmds.BoolOption("recursive", "r", "Recursively pin the object linked to by the specified object(s).").Default(true),
45
},
46
Type: PinOutput{},
47
Run: func(req cmds.Request, res cmds.Response) {
@@ -54,14 +54,11 @@ var addPinCmd = &cmds.Command{
54
defer n.Blockstore.PinLock().Unlock()
55
56
// set recursive flag
57
- recursive, found, err := req.Option("recursive").Bool()
57
+ recursive, _, err := req.Option("recursive").Bool()
58
if err != nil {
59
res.SetError(err, cmds.ErrNormal)
60
return
61
}
62
- if !found {
63
- recursive = true
64
- }
62
63
added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive)
64
if err != nil {
@@ -108,7 +105,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
105
cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned.").EnableStdin(),
106
},
107
Options: []cmds.Option{
111
- cmds.BoolOption("recursive", "r", "Recursively unpin the object linked to by the specified object(s)."),
108
+ cmds.BoolOption("recursive", "r", "Recursively unpin the object linked to by the specified object(s).").Default(true),
109
},
110
Type: PinOutput{},
111
Run: func(req cmds.Request, res cmds.Response) {
@@ -119,14 +116,11 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
116
}
117
118
// set recursive flag
122
- recursive, found, err := req.Option("recursive").Bool()
119
+ recursive, _, err := req.Option("recursive").Bool()
120
if err != nil {
121
res.SetError(err, cmds.ErrNormal)
122
return
123
}
127
- if !found {
128
- recursive = true // default
129
- }
124
125
removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
126
if err != nil {