提交 1f8230c9 作者: Juan Batiz-Benet

Merge pull request #1263 from ipfs/stdin-parsing-optional-arg

Stdin parsing optional arg
...@@ -219,9 +219,11 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi ...@@ -219,9 +219,11 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
} }
} }
// count number of values provided by user // count number of values provided by user.
// if there is at least one ArgDef, we can safely trigger the inputs loop
// below to parse stdin.
numInputs := len(inputs) numInputs := len(inputs)
if stdin != nil { if len(argDefs) > 0 && argDefs[len(argDefs)-1].SupportsStdin && stdin != nil {
numInputs += 1 numInputs += 1
} }
......
...@@ -144,6 +144,12 @@ func TestArgumentParsing(t *testing.T) { ...@@ -144,6 +144,12 @@ func TestArgumentParsing(t *testing.T) {
commands.StringArg("b", false, true, "another arg"), commands.StringArg("b", false, true, "another arg"),
}, },
}, },
"optionalsecond": {
Arguments: []commands.Argument{
commands.StringArg("a", true, false, "some arg"),
commands.StringArg("b", false, false, "another arg"),
},
},
"reversedoptional": { "reversedoptional": {
Arguments: []commands.Argument{ Arguments: []commands.Argument{
commands.StringArg("a", false, false, "some arg"), commands.StringArg("a", false, false, "some arg"),
...@@ -213,6 +219,12 @@ func TestArgumentParsing(t *testing.T) { ...@@ -213,6 +219,12 @@ func TestArgumentParsing(t *testing.T) {
test([]string{"optional", "value!"}, nil, []string{"value!"}) test([]string{"optional", "value!"}, nil, []string{"value!"})
test([]string{"optional"}, nil, []string{}) test([]string{"optional"}, nil, []string{})
test([]string{"optional", "value1", "value2"}, nil, []string{"value1", "value2"})
test([]string{"optionalsecond", "value!"}, nil, []string{"value!"})
test([]string{"optionalsecond", "value1", "value2"}, nil, []string{"value1", "value2"})
testFail([]string{"optionalsecond"}, "didn't provide any args, 1 required")
testFail([]string{"optionalsecond", "value1", "value2", "value3"}, "provided too many args, takes 2 maximum")
test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"}) test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"})
test([]string{"reversedoptional", "value!"}, nil, []string{"value!"}) test([]string{"reversedoptional", "value!"}, nil, []string{"value!"})
...@@ -268,4 +280,10 @@ func TestArgumentParsing(t *testing.T) { ...@@ -268,4 +280,10 @@ func TestArgumentParsing(t *testing.T) {
fstdin = fileToSimulateStdin(t, "stdin1") fstdin = fileToSimulateStdin(t, "stdin1")
test([]string{"stdinenablednotvariadic2args", "value1"}, fstdin, []string{"value1", "stdin1"}) test([]string{"stdinenablednotvariadic2args", "value1"}, fstdin, []string{"value1", "stdin1"})
test([]string{"stdinenablednotvariadic2args", "value1", "value2"}, fstdin, []string{"value1", "value2"}) test([]string{"stdinenablednotvariadic2args", "value1", "value2"}, fstdin, []string{"value1", "value2"})
fstdin = fileToSimulateStdin(t, "stdin1")
test([]string{"noarg"}, fstdin, []string{})
fstdin = fileToSimulateStdin(t, "stdin1")
test([]string{"optionalsecond", "value1", "value2"}, fstdin, []string{"value1", "value2"})
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论