提交 5f3945c9 作者: Jeromy Johnson

Merge pull request #2443 from ipfs/feat/default-option-vals

add default option value support to commands lib
package commands package commands
import ( import (
"fmt"
"reflect" "reflect"
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
...@@ -21,12 +22,15 @@ type Option interface { ...@@ -21,12 +22,15 @@ type Option interface {
Names() []string // a list of unique names matched with user-provided flags Names() []string // a list of unique names matched with user-provided flags
Type() reflect.Kind // value must be this type Type() reflect.Kind // value must be this type
Description() string // a short string that describes this option Description() string // a short string that describes this option
Default(interface{}) Option // sets the default value of the option
DefaultVal() interface{}
} }
type option struct { type option struct {
names []string names []string
kind reflect.Kind kind reflect.Kind
description string description string
defaultVal interface{}
} }
func (o *option) Names() []string { func (o *option) Names() []string {
...@@ -38,6 +42,13 @@ func (o *option) Type() reflect.Kind { ...@@ -38,6 +42,13 @@ func (o *option) Type() reflect.Kind {
} }
func (o *option) Description() string { func (o *option) Description() string {
if o.description[len(o.description)-1] != '.' {
o.description += "."
}
if o.defaultVal != nil {
return fmt.Sprintf("%s Default: %v.", o.description, o.defaultVal)
}
return o.description return o.description
} }
...@@ -58,6 +69,15 @@ func NewOption(kind reflect.Kind, names ...string) Option { ...@@ -58,6 +69,15 @@ func NewOption(kind reflect.Kind, names ...string) Option {
} }
} }
func (o *option) Default(v interface{}) Option {
o.defaultVal = v
return o
}
func (o *option) DefaultVal() interface{} {
return o.defaultVal
}
// TODO handle description separately. this will take care of the panic case in // TODO handle description separately. this will take care of the panic case in
// NewOption // NewOption
...@@ -98,7 +118,7 @@ func (ov OptionValue) Definition() Option { ...@@ -98,7 +118,7 @@ func (ov OptionValue) Definition() Option {
// value accessor methods, gets the value as a certain type // value accessor methods, gets the value as a certain type
func (ov OptionValue) Bool() (value bool, found bool, err error) { func (ov OptionValue) Bool() (value bool, found bool, err error) {
if !ov.found { if !ov.found && ov.value == nil {
return false, false, nil return false, false, nil
} }
val, ok := ov.value.(bool) val, ok := ov.value.(bool)
...@@ -109,7 +129,7 @@ func (ov OptionValue) Bool() (value bool, found bool, err error) { ...@@ -109,7 +129,7 @@ func (ov OptionValue) Bool() (value bool, found bool, err error) {
} }
func (ov OptionValue) Int() (value int, found bool, err error) { func (ov OptionValue) Int() (value int, found bool, err error) {
if !ov.found { if !ov.found && ov.value == nil {
return 0, false, nil return 0, false, nil
} }
val, ok := ov.value.(int) val, ok := ov.value.(int)
...@@ -120,7 +140,7 @@ func (ov OptionValue) Int() (value int, found bool, err error) { ...@@ -120,7 +140,7 @@ func (ov OptionValue) Int() (value int, found bool, err error) {
} }
func (ov OptionValue) Uint() (value uint, found bool, err error) { func (ov OptionValue) Uint() (value uint, found bool, err error) {
if !ov.found { if !ov.found && ov.value == nil {
return 0, false, nil return 0, false, nil
} }
val, ok := ov.value.(uint) val, ok := ov.value.(uint)
...@@ -131,7 +151,7 @@ func (ov OptionValue) Uint() (value uint, found bool, err error) { ...@@ -131,7 +151,7 @@ func (ov OptionValue) Uint() (value uint, found bool, err error) {
} }
func (ov OptionValue) Float() (value float64, found bool, err error) { func (ov OptionValue) Float() (value float64, found bool, err error) {
if !ov.found { if !ov.found && ov.value == nil {
return 0, false, nil return 0, false, nil
} }
val, ok := ov.value.(float64) val, ok := ov.value.(float64)
...@@ -142,7 +162,7 @@ func (ov OptionValue) Float() (value float64, found bool, err error) { ...@@ -142,7 +162,7 @@ func (ov OptionValue) Float() (value float64, found bool, err error) {
} }
func (ov OptionValue) String() (value string, found bool, err error) { func (ov OptionValue) String() (value string, found bool, err error) {
if !ov.found { if !ov.found && ov.value == nil {
return "", false, nil return "", false, nil
} }
val, ok := ov.value.(string) val, ok := ov.value.(string)
......
...@@ -9,13 +9,6 @@ func TestOptionValueExtractBoolNotFound(t *testing.T) { ...@@ -9,13 +9,6 @@ func TestOptionValueExtractBoolNotFound(t *testing.T) {
if err != nil { if err != nil {
t.Fatal("Found was false. Err should have been nil") t.Fatal("Found was false. Err should have been nil")
} }
t.Log("ensure that no error is returned when value is not found (even if value exists)")
optval = &OptionValue{value: "wrong type: a string", found: false}
_, _, err = optval.Bool()
if err != nil {
t.Fatal("Found was false. Err should have been nil")
}
} }
func TestOptionValueExtractWrongType(t *testing.T) { func TestOptionValueExtractWrongType(t *testing.T) {
......
...@@ -118,8 +118,7 @@ func (r *request) Option(name string) *OptionValue { ...@@ -118,8 +118,7 @@ func (r *request) Option(name string) *OptionValue {
} }
} }
// MAYBE_TODO: use default value instead of nil return &OptionValue{option.DefaultVal(), false, option}
return &OptionValue{nil, false, option}
} }
// Options returns a copy of the option map // Options returns a copy of the option map
......
...@@ -41,7 +41,7 @@ var addPinCmd = &cmds.Command{ ...@@ -41,7 +41,7 @@ var addPinCmd = &cmds.Command{
cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be pinned.").EnableStdin(), cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be pinned.").EnableStdin(),
}, },
Options: []cmds.Option{ Options: []cmds.Option{
cmds.BoolOption("recursive", "r", "Recursively pin the object linked to by the specified object(s)."), cmds.BoolOption("recursive", "r", "Recursively pin the object linked to by the specified object(s).").Default(true),
}, },
Type: PinOutput{}, Type: PinOutput{},
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
...@@ -54,14 +54,11 @@ var addPinCmd = &cmds.Command{ ...@@ -54,14 +54,11 @@ var addPinCmd = &cmds.Command{
defer n.Blockstore.PinLock().Unlock() defer n.Blockstore.PinLock().Unlock()
// set recursive flag // set recursive flag
recursive, found, err := req.Option("recursive").Bool() recursive, _, err := req.Option("recursive").Bool()
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
if !found {
recursive = true
}
added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive) added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive)
if err != nil { if err != nil {
...@@ -108,7 +105,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins) ...@@ -108,7 +105,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned.").EnableStdin(), cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned.").EnableStdin(),
}, },
Options: []cmds.Option{ Options: []cmds.Option{
cmds.BoolOption("recursive", "r", "Recursively unpin the object linked to by the specified object(s)."), cmds.BoolOption("recursive", "r", "Recursively unpin the object linked to by the specified object(s).").Default(true),
}, },
Type: PinOutput{}, Type: PinOutput{},
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
...@@ -119,14 +116,11 @@ collected if needed. (By default, recursively. Use -r=false for direct pins) ...@@ -119,14 +116,11 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
} }
// set recursive flag // set recursive flag
recursive, found, err := req.Option("recursive").Bool() recursive, _, err := req.Option("recursive").Bool()
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
if !found {
recursive = true // default
}
removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive) removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
if err != nil { if err != nil {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论