From 74d812a7666e2d31e6712ccd02a36d7344e94de7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 2 Jun 2013 15:03:02 -0700 Subject: [PATCH] packer: Much smarter CLI --- packer/environment.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packer/environment.go b/packer/environment.go index 5be574bf1..645171405 100644 --- a/packer/environment.go +++ b/packer/environment.go @@ -160,6 +160,26 @@ func (e *coreEnvironment) Cli(args []string) (result int, err error) { return 1, nil } + // This variable will track whether or not we're supposed to print + // the help or not. + isHelp := false + for _, arg := range args { + if arg == "-h" || arg == "--help" { + isHelp = true + break + } + } + + // Trim up to the command name + for i, v := range args { + if v[0] != '-' { + args = args[i:] + break + } + } + + log.Printf("command + args: %#v", args) + version := args[0] == "version" if !version { for _, arg := range args { @@ -191,11 +211,9 @@ func (e *coreEnvironment) Cli(args []string) (result int, err error) { // If we're supposed to print help, then print the help of the // command rather than running it. - for _, arg := range args { - if arg == "--help" || arg == "-h" { - e.ui.Say(command.Help()) - return 0, nil - } + if isHelp { + e.ui.Say(command.Help()) + return 0, nil } log.Printf("Executing command: %s\n", args[0])