Skip to content

Commit

Permalink
- Fixed issue with command description not being displayed if command…
Browse files Browse the repository at this point in the history
… only has subcommands.
  • Loading branch information
Kristoffer Ahl committed Sep 1, 2018
1 parent b18637e commit c49a29f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion centry.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func centry(osArgs []string) int {

Commands: map[string]cli.CommandFactory{},
Args: args,
HelpFunc: centryHelpFunc(manifest.Config.Name, globalFlags), // TODO: Pass manifest instead of globalFlags to get correct flags order and short flags grouped with long option
HelpFunc: centryHelpFunc(manifest, globalFlags),

// Autocomplete: true,
// AutocompleteInstall: "install-autocomplete",
Expand Down
18 changes: 13 additions & 5 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (
"github.com/kristofferahl/cli"
)

func centryHelpFunc(app string, globalFlags *flag.FlagSet) cli.HelpFunc {
func centryHelpFunc(manifest *manifest, globalFlags *flag.FlagSet) cli.HelpFunc {
return func(commands map[string]cli.CommandFactory) string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("Usage: %s [--version] [--help] <command> [<args>]\n\n", app))
buf.WriteString(fmt.Sprintf("Usage: %s [--version] [--help] <command> [<args>]\n\n", manifest.Config.Name))

writeCommands(&buf, commands)
writeCommands(&buf, commands, manifest)
writeOptions(&buf, globalFlags)

return buf.String()
}
}

func writeCommands(buf *bytes.Buffer, commands map[string]cli.CommandFactory) {
func writeCommands(buf *bytes.Buffer, commands map[string]cli.CommandFactory, manifest *manifest) {
buf.WriteString("Available commands are:\n")

// Get the list of keys so we can sort them, and also get the maximum
Expand Down Expand Up @@ -53,8 +53,16 @@ func writeCommands(buf *bytes.Buffer, commands map[string]cli.CommandFactory) {
continue
}

synopsis := command.Synopsis()
if synopsis == "" {
for _, mc := range manifest.Commands {
if mc.Name == key {
synopsis = mc.Description
}
}
}
key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key)))
buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis()))
buf.WriteString(fmt.Sprintf(" %s %s\n", key, synopsis))
}
}

Expand Down
8 changes: 6 additions & 2 deletions test/data/commands/delete.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env bash

delete () {
echo "delete ($*)"
delete:db () {
echo "delete:db ($*)"
}

delete:files () {
echo "delete:files ($*)"
}

0 comments on commit c49a29f

Please sign in to comment.