Skip to content

Commit b7e8c55

Browse files
committed
Add descriptions for commands
1 parent 10271db commit b7e8c55

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

commands.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ type Command struct {
2424
// NewCommand creates a command that is triggered by the given name in the command line
2525
// all flags are defined by a struct that is parsed and filled with real values
2626
// this struct is then set as argument for the function that is executed if the name matches
27-
func NewCommand(name string, config interface{}, f CommandFunc, subCommands ...*Command) *Command {
27+
func NewCommand(name string, description string, config interface{}, f CommandFunc, subCommands ...*Command) *Command {
2828
fs := flag.NewFlagSet(name, flag.ExitOnError)
2929
fs.Usage = func() {
30+
fmt.Fprint(fs.Output(), description+"\n\n")
3031
if name == "" {
3132
fmt.Fprintf(fs.Output(), "Usage:\n")
3233
} else {
@@ -35,7 +36,7 @@ func NewCommand(name string, config interface{}, f CommandFunc, subCommands ...*
3536
fs.PrintDefaults()
3637

3738
if len(subCommands) > 0 {
38-
fmt.Fprintf(fs.Output(), "Available Subcommands: ")
39+
fmt.Fprintf(fs.Output(), "\nAvailable Commands:\n")
3940
for i := range subCommands {
4041
fmt.Fprintf(fs.Output(), subCommands[i].fs.Name()+" ")
4142
}

commands_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ func TestCommand_ParseAndRun(t *testing.T) {
2626
var rootConfig rootCmdConfig
2727
var countConfig subCmdConfig
2828

29-
countCmd := NewCommand("count", &countConfig, func(cmd *Command, cfg interface{}) error {
29+
countCmd := NewCommand("count", "Count numbers", &countConfig, func(cmd *Command, cfg interface{}) error {
3030
cfgValues := cfg.(*subCmdConfig)
3131
t.Log("count command", cfgValues.Number)
3232
return nil
3333
})
3434

35-
mathCmd := NewCommand("math", nil, nil, countCmd)
35+
mathCmd := NewCommand("math", "Mathematical functions", nil, nil, countCmd)
3636

37-
cmd := NewCommand("", &rootConfig, func(cmd *Command, cfg interface{}) error {
37+
cmd := NewCommand("", "Test CLI", &rootConfig, func(cmd *Command, cfg interface{}) error {
3838
cfgValues := cfg.(*rootCmdConfig)
3939
t.Log("root command", cfgValues.Hostname)
4040
return nil
@@ -47,7 +47,7 @@ func TestCommand_ParseAndRun(t *testing.T) {
4747
}
4848

4949
func TestCommand_Dependencies(t *testing.T) {
50-
c := NewCommand("testCmd", nil, nil)
50+
c := NewCommand("testCmd", "Test CLI", nil, nil)
5151

5252
test := &testStruct{testValue: "test"}
5353

0 commit comments

Comments
 (0)