Several commands panic when not passed enough arguments. They print an unhelpful index out of range error and a stack trace. This is undesirable for most routine error conditions.
In the same spirit as #12 I added len(args) guard clauses to the remaining commands that require arguments. They now return a helpful error explaining the arguments needed.
Example:
$ dbxcli mkdir
Error: `mkdir` requires a `directory` argument
Thoughts/questions:
Should this be DRY'd into a validateArgs function in root.go? It's only three lines per command, so I wasn't sure if it's worth it.
Should we print a longer error message including the usage text? We already have that text available in the cobra.Command so it would reduce duplication. Many other CLI tools do this. Example from Docker:
$ docker pull
docker: "pull" requires 1 argument.
See 'docker pull --help'.
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Merging as-is, thanks @waits! Regarding your comments, I really wish Cobra had some way to support this directly (e.g. some notion of a "validator" for each command that can check on len(args) or other validations).
Several commands panic when not passed enough arguments. They print an unhelpful
index out of range
error and a stack trace. This is undesirable for most routine error conditions.In the same spirit as #12 I added
len(args)
guard clauses to the remaining commands that require arguments. They now return a helpful error explaining the arguments needed.Example:
Thoughts/questions:
validateArgs
function inroot.go
? It's only three lines per command, so I wasn't sure if it's worth it.cobra.Command
so it would reduce duplication. Many other CLI tools do this. Example from Docker: