From f6919cf18bf38234176a9871c8a8de7c88bea498 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 24 Mar 2013 16:35:33 -0700 Subject: [PATCH] Short-circuit -h and --help to printing help for the top-level cmd --- packer/environment.go | 9 ++++++++- packer/environment_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packer/environment.go b/packer/environment.go index dc3b40125..246424406 100644 --- a/packer/environment.go +++ b/packer/environment.go @@ -14,6 +14,13 @@ import ( // // The mapping of command names to command interfaces is in the // Environment struct. +// +// Run should run the actual command with the given environmet and +// command-line arguments. It should return the exit status when it is +// finished. +// +// Synopsis should return a one-line, short synopsis of the command. +// This should be less than 50 characters ideally. type Command interface { Run(env *Environment, args []string) int Synopsis() string @@ -43,7 +50,7 @@ func NewEnvironment() *Environment { // Executes a command as if it was typed on the command-line interface. // The return value is the exit code of the command. func (e *Environment) Cli(args []string) int { - if len(args) == 0 { + if len(args) == 0 || args[0] == "--help" || args[0] == "-h" { e.PrintHelp() return 1 } diff --git a/packer/environment_test.go b/packer/environment_test.go index f59002dd0..67cc4d373 100644 --- a/packer/environment_test.go +++ b/packer/environment_test.go @@ -14,6 +14,16 @@ func TestEnvironment_DefaultCli_Empty(t *testing.T) { assert.Equal(defaultEnv.Cli([]string{}), 1, "CLI with no args") } +func TestEnvironment_DefaultCli_Help(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + defaultEnv := NewEnvironment() + + // Test the basic version options + assert.Equal(defaultEnv.Cli([]string{"--help"}), 1, "--help should print") + assert.Equal(defaultEnv.Cli([]string{"-h"}), 1, "--help should print") +} + func TestEnvironment_DefaultCli_Version(t *testing.T) { assert := asserts.NewTestingAsserts(t, true)