From 3d8c3c59de3bcc77490a4a2fc39d81097bd91f49 Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Fri, 18 Aug 2017 13:49:20 +0000 Subject: [PATCH] use existing arg parsing functionality --- command/meta.go | 5 ----- command/status.go | 33 ++++++++++++++++----------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/command/meta.go b/command/meta.go index cf32eeeb4..5f4a60c30 100644 --- a/command/meta.go +++ b/command/meta.go @@ -152,11 +152,6 @@ func (m *Meta) Colorize() *colorstring.Colorize { } } -var ( - // flagOptions is a list of all available flags that can be used via the cli - flagOptions = []string{"address", "region", "no-color", "ca-cert", "ca-path", "client-cert", "client-key", "tls-skip-verify"} -) - // generalOptionsUsage returns the help string for the global options. func generalOptionsUsage() string { helpText := ` diff --git a/command/status.go b/command/status.go index 77d110a9b..54e16636c 100644 --- a/command/status.go +++ b/command/status.go @@ -2,7 +2,6 @@ package command import ( "fmt" - "strings" "github.com/hashicorp/nomad/api/contexts" "github.com/mitchellh/cli" @@ -13,19 +12,6 @@ type StatusCommand struct { Meta } -// Check that the last argument provided is not setting a flag -func lastArgIsFlag(args []string) bool { - // strip leading '-' from what is potentially a flag - lastArg := strings.Replace(args[len(args)-1], "-", "", 1) - - for _, flag := range flagOptions { - if strings.HasPrefix(lastArg, flag) { - return true - } - } - return false -} - func (c *StatusCommand) Run(args []string) int { // Get the HTTP client client, err := c.Meta.Client() @@ -34,10 +20,23 @@ func (c *StatusCommand) Run(args []string) int { return 1 } + // Parsing args is not idempotent + argsCopy := args + + flags := c.Meta.FlagSet("status", FlagSetClient) + + if err := flags.Parse(args); err != nil { + c.Ui.Error(fmt.Sprintf("Error parsing arguments: %s", err)) + return 1 + } + + // Check that we got exactly one evaluation ID + args = flags.Args() + // If no identifier is provided, default to listing jobs - if len(args) == 0 || lastArgIsFlag(args) { + if len(args) == 0 { cmd := &JobStatusCommand{Meta: c.Meta} - return cmd.Run(args) + return cmd.Run(argsCopy) } id := args[len(args)-1] @@ -84,7 +83,7 @@ func (c *StatusCommand) Run(args []string) int { return 1 } - return cmd.Run(args) + return cmd.Run(argsCopy) } func (s *StatusCommand) Help() string {