Skip to content

Commit

Permalink
More helpful resource usage errors (stripe#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe authored Jun 22, 2020
1 parent 940d230 commit 361d78d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
19 changes: 19 additions & 0 deletions pkg/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ func TestHelpFlag(t *testing.T) {
require.Contains(t, output, "Stripe commands:")
require.NoError(t, err)
}

func TestExampleCommands(t *testing.T) {
{
_, err := executeCommand(rootCmd, "foo")
require.Equal(t, err.Error(), "unknown command \"foo\" for \"stripe\"")
}
{
_, err := executeCommand(rootCmd, "listen", "foo")
require.Equal(t, err.Error(), "`stripe listen` does not take any positional arguments. See `stripe listen --help` for supported flags and usage")
}
{
_, err := executeCommand(rootCmd, "post")
require.Equal(t, err.Error(), "`stripe post` requires exactly 1 positional argument. See `stripe post --help` for supported flags and usage")
}
{
_, err := executeCommand(rootCmd, "samples", "create", "foo", "foo", "foo")
require.Equal(t, err.Error(), "`stripe samples create` accepts at maximum 2 positional arguments. See `stripe samples create --help` for supported flags and usage")
}
}
28 changes: 14 additions & 14 deletions pkg/validators/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
// NoArgs is a validator for commands to print an error when an argument is provided
func NoArgs(cmd *cobra.Command, args []string) error {
errorMessage := fmt.Sprintf(
"%s does not take any arguments. See `stripe %s --help` for supported flags and usage",
cmd.Name(),
cmd.Name(),
"`%s` does not take any positional arguments. See `%s --help` for supported flags and usage",
cmd.CommandPath(),
cmd.CommandPath(),
)

if len(args) > 0 {
Expand All @@ -26,17 +26,17 @@ func NoArgs(cmd *cobra.Command, args []string) error {
// is different than the arguments passed in
func ExactArgs(num int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
argument := "argument"
if num > 1 {
argument = "arguments"
argument := "positional argument"
if num != 1 {
argument = "positional arguments"
}

errorMessage := fmt.Sprintf(
"%s only takes %d %s. See `stripe %s --help` for supported flags and usage",
cmd.Name(),
"`%s` requires exactly %d %s. See `%s --help` for supported flags and usage",
cmd.CommandPath(),
num,
argument,
cmd.Name(),
cmd.CommandPath(),
)

if len(args) != num {
Expand All @@ -50,17 +50,17 @@ func ExactArgs(num int) cobra.PositionalArgs {
// args are greater than the maximum amount
func MaximumNArgs(num int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
argument := "argument"
argument := "positional argument"
if num > 1 {
argument = "arguments"
argument = "positional arguments"
}

errorMessage := fmt.Sprintf(
"%s only takes %d %s (or less). See `stripe %s --help` for supported flags and usage",
cmd.Name(),
"`%s` accepts at maximum %d %s. See `%s --help` for supported flags and usage",
cmd.CommandPath(),
num,
argument,
cmd.Name(),
cmd.CommandPath(),
)

if len(args) > num {
Expand Down
6 changes: 3 additions & 3 deletions pkg/validators/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNoArgsWithArgs(t *testing.T) {
args := []string{"foo"}

result := NoArgs(c, args)
require.EqualError(t, result, "c does not take any arguments. See `stripe c --help` for supported flags and usage")
require.EqualError(t, result, "`c` does not take any positional arguments. See `c --help` for supported flags and usage")
}

func TestExactArgs(t *testing.T) {
Expand All @@ -36,13 +36,13 @@ func TestExactArgsTooMany(t *testing.T) {
args := []string{"foo", "bar"}

result := ExactArgs(1)(c, args)
require.EqualError(t, result, "c only takes 1 argument. See `stripe c --help` for supported flags and usage")
require.EqualError(t, result, "`c` requires exactly 1 positional argument. See `c --help` for supported flags and usage")
}

func TestExactArgsTooManyMoreThan1(t *testing.T) {
c := &cobra.Command{Use: "c"}
args := []string{"foo", "bar", "baz"}

result := ExactArgs(2)(c, args)
require.EqualError(t, result, "c only takes 2 arguments. See `stripe c --help` for supported flags and usage")
require.EqualError(t, result, "`c` requires exactly 2 positional arguments. See `c --help` for supported flags and usage")
}

0 comments on commit 361d78d

Please sign in to comment.