Commit 107409ce authored by ForrestWeston's avatar ForrestWeston

Added ability to specify true/false for bool opts

License: MIT
Signed-off-by: default avatarForrestWeston <Forrest.Weston@gmail.com>
parent bea47c9b
...@@ -85,13 +85,28 @@ func parseOpts(args []string, root *cmds.Command) ( ...@@ -85,13 +85,28 @@ func parseOpts(args []string, root *cmds.Command) (
err = fmt.Errorf("Unrecognized option '%s'", name) err = fmt.Errorf("Unrecognized option '%s'", name)
return false, err return false, err
} }
// mustUse implies that you must use the argument given after the '='
// eg. -r=true means you must take true into consideration
// mustUse == true in the above case
// eg. ipfs -r <file> means disregard <file> since there is no '='
// mustUse == false in the above situation
//arg == nil implies the flag was specified without an argument
if optDef.Type() == cmds.Bool { if optDef.Type() == cmds.Bool {
if mustUse { if arg == nil || !mustUse {
return false, fmt.Errorf("Option '%s' takes no arguments, but was passed '%s'", name, *arg) opts[name] = true
}
opts[name] = ""
return false, nil return false, nil
}
argVal := strings.ToLower(*arg)
switch argVal {
case "true":
opts[name] = true
return true, nil
case "false":
opts[name] = false
return true, nil
default:
return true, fmt.Errorf("Option '%s' takes true/false arguments, but was passed '%s'", name, argVal)
}
} else { } else {
if arg == nil { if arg == nil {
return true, fmt.Errorf("Missing argument for option '%s'", name) return true, fmt.Errorf("Missing argument for option '%s'", name)
......
...@@ -106,17 +106,25 @@ func TestOptionParsing(t *testing.T) { ...@@ -106,17 +106,25 @@ func TestOptionParsing(t *testing.T) {
test("-s foo", kvs{"s": "foo"}, words{}) test("-s foo", kvs{"s": "foo"}, words{})
test("-sfoo", kvs{"s": "foo"}, words{}) test("-sfoo", kvs{"s": "foo"}, words{})
test("-s=foo", kvs{"s": "foo"}, words{}) test("-s=foo", kvs{"s": "foo"}, words{})
test("-b", kvs{"b": ""}, words{}) test("-b", kvs{"b": true}, words{})
test("-bs foo", kvs{"b": "", "s": "foo"}, words{}) test("-bs foo", kvs{"b": true, "s": "foo"}, words{})
test("-sb", kvs{"s": "b"}, words{}) test("-sb", kvs{"s": "b"}, words{})
test("-b foo", kvs{"b": ""}, words{"foo"}) test("-b foo", kvs{"b": true}, words{"foo"})
test("--bool foo", kvs{"bool": ""}, words{"foo"}) test("--bool foo", kvs{"bool": true}, words{"foo"})
testFail("--bool=foo") testFail("--bool=foo")
testFail("--string") testFail("--string")
test("--string foo", kvs{"string": "foo"}, words{}) test("--string foo", kvs{"string": "foo"}, words{})
test("--string=foo", kvs{"string": "foo"}, words{}) test("--string=foo", kvs{"string": "foo"}, words{})
test("-- -b", kvs{}, words{"-b"}) test("-- -b", kvs{}, words{"-b"})
test("foo -b", kvs{"b": ""}, words{"foo"}) test("foo -b", kvs{"b": true}, words{"foo"})
test("-b=false", kvs{"b": false}, words{})
test("-b=true", kvs{"b": true}, words{})
test("-b=false foo", kvs{"b": false}, words{"foo"})
test("-b=true foo", kvs{"b": true}, words{"foo"})
test("--bool=true foo", kvs{"bool": true}, words{"foo"})
test("--bool=false foo", kvs{"bool": false}, words{"foo"})
test("-b=FaLsE foo", kvs{"b": false}, words{"foo"})
test("-b=TrUe foo", kvs{"b": true}, words{"foo"})
} }
func TestArgumentParsing(t *testing.T) { func TestArgumentParsing(t *testing.T) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment