Commit f6919cf1 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Short-circuit -h and --help to printing help for the top-level cmd

parent 4d10489f
...@@ -14,6 +14,13 @@ import ( ...@@ -14,6 +14,13 @@ import (
// //
// The mapping of command names to command interfaces is in the // The mapping of command names to command interfaces is in the
// Environment struct. // 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 { type Command interface {
Run(env *Environment, args []string) int Run(env *Environment, args []string) int
Synopsis() string Synopsis() string
...@@ -43,7 +50,7 @@ func NewEnvironment() *Environment { ...@@ -43,7 +50,7 @@ func NewEnvironment() *Environment {
// Executes a command as if it was typed on the command-line interface. // Executes a command as if it was typed on the command-line interface.
// The return value is the exit code of the command. // The return value is the exit code of the command.
func (e *Environment) Cli(args []string) int { func (e *Environment) Cli(args []string) int {
if len(args) == 0 { if len(args) == 0 || args[0] == "--help" || args[0] == "-h" {
e.PrintHelp() e.PrintHelp()
return 1 return 1
} }
......
...@@ -14,6 +14,16 @@ func TestEnvironment_DefaultCli_Empty(t *testing.T) { ...@@ -14,6 +14,16 @@ func TestEnvironment_DefaultCli_Empty(t *testing.T) {
assert.Equal(defaultEnv.Cli([]string{}), 1, "CLI with no args") 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) { func TestEnvironment_DefaultCli_Version(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment