Skip to content

Commit

Permalink
Merge pull request #4176 from hashicorp/i-cli-error-help
Browse files Browse the repository at this point in the history
Improve help text when invalid arguments are given
  • Loading branch information
nickethier committed Apr 19, 2018
2 parents a516dcd + f1364fb commit 73b94f5
Show file tree
Hide file tree
Showing 120 changed files with 431 additions and 195 deletions.
2 changes: 2 additions & 0 deletions command/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (f *ACLCommand) Synopsis() string {
return "Interact with ACL policies and tokens"
}

func (f *ACLCommand) Name() string { return "acl" }

func (f *ACLCommand) Run(args []string) int {
return cli.RunResultHelp
}
7 changes: 5 additions & 2 deletions command/acl_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ func (c *ACLBootstrapCommand) Synopsis() string {
return "Bootstrap the ACL system for initial token"
}

func (c *ACLBootstrapCommand) Name() string { return "acl bootstrap" }

func (c *ACLBootstrapCommand) Run(args []string) int {
flags := c.Meta.FlagSet("acl bootstrap", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
Expand All @@ -49,7 +51,8 @@ func (c *ACLBootstrapCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes no arguments")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
2 changes: 2 additions & 0 deletions command/acl_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (f *ACLPolicyCommand) Synopsis() string {
return "Interact with ACL policies"
}

func (f *ACLPolicyCommand) Name() string { return "acl policy" }

func (f *ACLPolicyCommand) Run(args []string) int {
return cli.RunResultHelp
}
7 changes: 5 additions & 2 deletions command/acl_policy_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ func (c *ACLPolicyApplyCommand) Synopsis() string {
return "Create or update an ACL policy"
}

func (c *ACLPolicyApplyCommand) Name() string { return "acl policy apply" }

func (c *ACLPolicyApplyCommand) Run(args []string) int {
var description string
flags := c.Meta.FlagSet("acl policy apply", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&description, "description", "", "")
if err := flags.Parse(args); err != nil {
Expand All @@ -59,7 +61,8 @@ func (c *ACLPolicyApplyCommand) Run(args []string) int {
// Check that we got two arguments
args = flags.Args()
if l := len(args); l != 2 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes two arguments: <name> and <path>")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
7 changes: 5 additions & 2 deletions command/acl_policy_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func (c *ACLPolicyDeleteCommand) Synopsis() string {
return "Delete an existing ACL policy"
}

func (c *ACLPolicyDeleteCommand) Name() string { return "acl policy delete" }

func (c *ACLPolicyDeleteCommand) Run(args []string) int {
flags := c.Meta.FlagSet("acl policy delete", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
Expand All @@ -47,7 +49,8 @@ func (c *ACLPolicyDeleteCommand) Run(args []string) int {
// Check that we got exactly one argument
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes one argument: <name>")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
7 changes: 5 additions & 2 deletions command/acl_policy_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func (c *ACLPolicyInfoCommand) Synopsis() string {
return "Fetch info on an existing ACL policy"
}

func (c *ACLPolicyInfoCommand) Name() string { return "acl policy info" }

func (c *ACLPolicyInfoCommand) Run(args []string) int {
flags := c.Meta.FlagSet("acl policy info", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
Expand All @@ -47,7 +49,8 @@ func (c *ACLPolicyInfoCommand) Run(args []string) int {
// Check that we got exactly one argument
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes one argument: <name>")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
7 changes: 5 additions & 2 deletions command/acl_policy_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ func (c *ACLPolicyListCommand) Synopsis() string {
return "List ACL policies"
}

func (c *ACLPolicyListCommand) Name() string { return "acl policy list" }

func (c *ACLPolicyListCommand) Run(args []string) int {
var json bool
var tmpl string

flags := c.Meta.FlagSet("acl policy list", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
Expand All @@ -66,7 +68,8 @@ func (c *ACLPolicyListCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes no arguments")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
2 changes: 2 additions & 0 deletions command/acl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (f *ACLTokenCommand) Synopsis() string {
return "Interact with ACL tokens"
}

func (f *ACLTokenCommand) Name() string { return "acl token" }

func (f *ACLTokenCommand) Run(args []string) int {
return cli.RunResultHelp
}
7 changes: 5 additions & 2 deletions command/acl_token_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ func (c *ACLTokenCreateCommand) Synopsis() string {
return "Create a new ACL token"
}

func (c *ACLTokenCreateCommand) Name() string { return "acl token create" }

func (c *ACLTokenCreateCommand) Run(args []string) int {
var name, tokenType string
var global bool
var policies []string
flags := c.Meta.FlagSet("acl token create", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&name, "name", "", "")
flags.StringVar(&tokenType, "type", "client", "")
Expand All @@ -78,7 +80,8 @@ func (c *ACLTokenCreateCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes no arguments")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
7 changes: 5 additions & 2 deletions command/acl_token_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func (c *ACLTokenDeleteCommand) Synopsis() string {
return "Delete an existing ACL token"
}

func (c *ACLTokenDeleteCommand) Name() string { return "acl token delete" }

func (c *ACLTokenDeleteCommand) Run(args []string) int {
flags := c.Meta.FlagSet("acl token delete", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }

if err := flags.Parse(args); err != nil {
Expand All @@ -49,7 +51,8 @@ func (c *ACLTokenDeleteCommand) Run(args []string) int {
// such token was provided.
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes one argument: <token_accessor_id>")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
7 changes: 5 additions & 2 deletions command/acl_token_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func (c *ACLTokenInfoCommand) Synopsis() string {
return "Fetch information on an existing ACL token"
}

func (c *ACLTokenInfoCommand) Name() string { return "acl token info" }

func (c *ACLTokenInfoCommand) Run(args []string) int {
flags := c.Meta.FlagSet("acl token info", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
Expand All @@ -47,7 +49,8 @@ func (c *ACLTokenInfoCommand) Run(args []string) int {
// Check that we have exactly one argument
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes one argument: <token_accessor_id>")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
9 changes: 6 additions & 3 deletions command/acl_token_self.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ func (c *ACLTokenSelfCommand) Synopsis() string {
return "Lookup self ACL token"
}

func (c *ACLTokenSelfCommand) Name() string { return "acl token self" }

func (c *ACLTokenSelfCommand) Run(args []string) int {
flags := c.Meta.FlagSet("acl token self", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}

// Check that we have exactly one argument
// Check that we have no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes no arguments")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
7 changes: 5 additions & 2 deletions command/acl_token_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ func (c *ACLTokenUpdateCommand) Synopsis() string {
return "Update an existing ACL token"
}

func (*ACLTokenUpdateCommand) Name() string { return "acl token update" }

func (c *ACLTokenUpdateCommand) Run(args []string) int {
var name, tokenType string
var global bool
var policies []string
flags := c.Meta.FlagSet("acl token update", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&name, "name", "", "")
flags.StringVar(&tokenType, "type", "client", "")
Expand All @@ -78,7 +80,8 @@ func (c *ACLTokenUpdateCommand) Run(args []string) int {
// Check that we got exactly one argument
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes one argument: <token_accessor_id>")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
9 changes: 6 additions & 3 deletions command/agent_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ func (c *AgentInfoCommand) Synopsis() string {
return "Display status information about the local agent"
}

func (c *AgentInfoCommand) Name() string { return "agent-info" }

func (c *AgentInfoCommand) Run(args []string) int {
flags := c.Meta.FlagSet("agent-info", FlagSetClient)
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}

// Check that we either got no jobs or exactly one.
// Check that we got no arguments
args = flags.Args()
if len(args) > 0 {
c.Ui.Error(c.Help())
c.Ui.Error("This command takes no arguments")
c.Ui.Error(commandErrorText(c))
return 1
}

Expand Down
2 changes: 1 addition & 1 deletion command/agent_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAgentInfoCommand_Fails(t *testing.T) {
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
t.Fatalf("expected help output, got: %s", out)
}
ui.ErrorWriter.Reset()
Expand Down
2 changes: 2 additions & 0 deletions command/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func (f *AllocCommand) Synopsis() string {
return "Interact with allocations"
}

func (f *AllocCommand) Name() string { return "alloc" }

func (f *AllocCommand) Run(args []string) int {
return cli.RunResultHelp
}
12 changes: 8 additions & 4 deletions command/alloc_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ func (f *AllocFSCommand) AutocompleteArgs() complete.Predictor {
})
}

func (f *AllocFSCommand) Name() string { return "alloc fs" }

func (f *AllocFSCommand) Run(args []string) int {
var verbose, machine, job, stat, tail, follow bool
var numLines, numBytes int64

flags := f.Meta.FlagSet("alloc fs", FlagSetClient)
flags := f.Meta.FlagSet(f.Name(), FlagSetClient)
flags.Usage = func() { f.Ui.Output(f.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&machine, "H", false, "")
Expand All @@ -131,15 +133,17 @@ func (f *AllocFSCommand) Run(args []string) int {

if len(args) < 1 {
if job {
f.Ui.Error("job ID is required")
f.Ui.Error("A job ID is required")
} else {
f.Ui.Error("allocation ID is required")
f.Ui.Error("An allocation ID is required")
}
f.Ui.Error(commandErrorText(f))
return 1
}

if len(args) > 2 {
f.Ui.Error(f.Help())
f.Ui.Error("This command takes one or two arguments: <allocation> [<path>]")
f.Ui.Error(commandErrorText(f))
return 1
}

Expand Down
2 changes: 1 addition & 1 deletion command/alloc_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestFSCommand_Fails(t *testing.T) {
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
t.Fatalf("expected help output, got: %s", out)
}
ui.ErrorWriter.Reset()
Expand Down
13 changes: 8 additions & 5 deletions command/alloc_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ func (l *AllocLogsCommand) AutocompleteArgs() complete.Predictor {
})
}

func (l *AllocLogsCommand) Name() string { return "alloc logs" }

func (l *AllocLogsCommand) Run(args []string) int {
var verbose, job, tail, stderr, follow bool
var numLines, numBytes int64

flags := l.Meta.FlagSet("alloc logs", FlagSetClient)
flags := l.Meta.FlagSet(l.Name(), FlagSetClient)
flags.Usage = func() { l.Ui.Output(l.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&job, "job", false, "")
Expand All @@ -111,15 +113,16 @@ func (l *AllocLogsCommand) Run(args []string) int {

if numArgs := len(args); numArgs < 1 {
if job {
l.Ui.Error("Job ID required. See help:\n")
l.Ui.Error("A job ID is required")
} else {
l.Ui.Error("Allocation ID required. See help:\n")
l.Ui.Error("An allocation ID is required")
}

l.Ui.Error(l.Help())
l.Ui.Error(commandErrorText(l))
return 1
} else if numArgs > 2 {
l.Ui.Error(l.Help())
l.Ui.Error("This command takes one or two arguments")
l.Ui.Error(commandErrorText(l))
return 1
}

Expand Down
2 changes: 1 addition & 1 deletion command/alloc_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestLogsCommand_Fails(t *testing.T) {
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
t.Fatalf("expected help output, got: %s", out)
}
ui.ErrorWriter.Reset()
Expand Down
Loading

0 comments on commit 73b94f5

Please sign in to comment.