Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix panic in cli arg parsing #2354

Merged
merged 1 commit into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,17 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi

var err error
if argDef.Type == cmds.ArgString {
if stdin == nil || !argDef.SupportsStdin {
if stdin == nil {
// add string values
stringArgs, inputs = appendString(stringArgs, inputs)
} else if !argDef.SupportsStdin {
if len(inputs) == 0 {
// failure case, we have stdin, but our current
// argument doesnt want stdin
break
}

stringArgs, inputs = appendString(stringArgs, inputs)
} else {
if len(inputs) > 0 {
// don't use stdin if we have inputs
Expand Down
21 changes: 11 additions & 10 deletions commands/cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,41 +212,41 @@ func TestArgumentParsing(t *testing.T) {
}
}

testFail := func(cmd words, msg string) {
testFail := func(cmd words, fi *os.File, msg string) {
_, _, _, err := Parse(cmd, nil, rootCmd)
if err == nil {
t.Errorf("Should have failed: %v", msg)
}
}

test([]string{"noarg"}, nil, []string{})
testFail([]string{"noarg", "value!"}, "provided an arg, but command didn't define any")
testFail([]string{"noarg", "value!"}, nil, "provided an arg, but command didn't define any")

test([]string{"onearg", "value!"}, nil, []string{"value!"})
testFail([]string{"onearg"}, "didn't provide any args, arg is required")
testFail([]string{"onearg"}, nil, "didn't provide any args, arg is required")

test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"})
testFail([]string{"twoargs", "value!"}, "only provided 1 arg, needs 2")
testFail([]string{"twoargs"}, "didn't provide any args, 2 required")
testFail([]string{"twoargs", "value!"}, nil, "only provided 1 arg, needs 2")
testFail([]string{"twoargs"}, nil, "didn't provide any args, 2 required")

test([]string{"variadic", "value!"}, nil, []string{"value!"})
test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"})
testFail([]string{"variadic"}, "didn't provide any args, 1 required")
testFail([]string{"variadic"}, nil, "didn't provide any args, 1 required")

test([]string{"optional", "value!"}, nil, []string{"value!"})
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")
testFail([]string{"optionalsecond"}, nil, "didn't provide any args, 1 required")
testFail([]string{"optionalsecond", "value1", "value2", "value3"}, nil, "provided too many args, takes 2 maximum")

test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"})
test([]string{"reversedoptional", "value!"}, nil, []string{"value!"})

testFail([]string{"reversedoptional"}, "didn't provide any args, 1 required")
testFail([]string{"reversedoptional", "value1", "value2", "value3"}, "provided too many args, only takes 1")
testFail([]string{"reversedoptional"}, nil, "didn't provide any args, 1 required")
testFail([]string{"reversedoptional", "value1", "value2", "value3"}, nil, "provided too many args, only takes 1")

// Use a temp file to simulate stdin
fileToSimulateStdin := func(t *testing.T, content string) *os.File {
Expand Down Expand Up @@ -296,6 +296,7 @@ func TestArgumentParsing(t *testing.T) {
fstdin = fileToSimulateStdin(t, "stdin1")
test([]string{"stdinenablednotvariadic2args", "value1"}, fstdin, []string{"value1", "stdin1"})
test([]string{"stdinenablednotvariadic2args", "value1", "value2"}, fstdin, []string{"value1", "value2"})
testFail([]string{"stdinenablednotvariadic2args"}, fstdin, "cant use stdin for non stdin arg")

fstdin = fileToSimulateStdin(t, "stdin1")
test([]string{"noarg"}, fstdin, []string{})
Expand Down
2 changes: 1 addition & 1 deletion core/commands/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
notif "github.com/ipfs/go-ipfs/notifications"
path "github.com/ipfs/go-ipfs/path"
ipdht "github.com/ipfs/go-ipfs/routing/dht"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
peer "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/peer"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I know gophers love short var names, but u -> util would be nice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i opted for not making lots of changes to the codebase. we can do a codebase-wide change for that later

)

var ErrNotDHT = errors.New("routing service is not a DHT")
Expand Down