Skip to content

Instantly share code, notes, and snippets.

@tucnak
Created November 11, 2015 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tucnak/ef17042fa1b5a9d2363b to your computer and use it in GitHub Desktop.
Save tucnak/ef17042fa1b5a9d2363b to your computer and use it in GitHub Desktop.
diff --git a/application.go b/application.go
index 3bf7d8f..6226dd2 100644
--- a/application.go
+++ b/application.go
@@ -3,7 +3,6 @@ package climax
import (
"fmt"
"os"
- "strings"
)
// Application is a main CLI instance.
@@ -16,16 +15,17 @@ type Application struct {
Brief string // `Go is a tool for managing Go source code.`
Version string // `1.5`
- Commands []Command
- Topics []Topic
- Categories [][]Command
+ Commands []Command
+ Topics []Topic
+ Groups []Group
+
+ ungroupedCmdsCount int
}
-func newApplication(name string) *Application {
- return &Application{
- Name: name,
- Categories: make([][]Command, 0),
- }
+// Group is smth
+type Group struct {
+ Name string
+ Commands []*Command
}
func (a *Application) commandByName(name string) *Command {
@@ -48,6 +48,16 @@ func (a *Application) topicByName(name string) *Topic {
return nil
}
+func (a *Application) groupByName(name string) *Group {
+ for i, group := range a.Groups {
+ if group.Name == name {
+ return &a.Groups[i]
+ }
+ }
+
+ return nil
+}
+
func (a Application) isNameAvailable(name string) bool {
hypo, jypo := a.commandByName(name), a.topicByName(name)
if hypo != nil || jypo != nil {
@@ -57,23 +67,26 @@ func (a Application) isNameAvailable(name string) bool {
return true
}
+// AddGroup adds a group.
+func (a *Application) AddGroup(name string) string {
+ a.Groups = append(a.Groups, Group{Name: name})
+ return name
+}
+
// AddCommand does literally what its name says.
func (a *Application) AddCommand(command Command) {
a.Commands = append(a.Commands, command)
- found := false
- command.Category = strings.ToUpper(command.Category)
-
- for idx, list := range a.Categories {
- if strings.ToUpper(list[0].Category) == command.Category {
- a.Categories[idx] = append(a.Categories[idx], command)
- found = true
- break
+ newCmd := &a.Commands[len(a.Commands)-1]
+ if newCmd.Group != "" {
+ group := a.groupByName(newCmd.Group)
+ if group == nil {
+ panic("group doesn't exist")
}
- }
- if !found {
- a.Categories = append(a.Categories, []Command{command})
+ group.Commands = append(group.Commands, newCmd)
+ } else {
+ a.ungroupedCmdsCount++
}
}
diff --git a/climax.go b/climax.go
index d414758..3298843 100644
--- a/climax.go
+++ b/climax.go
@@ -84,5 +84,5 @@ func New(name string) *Application {
panic("can't construct an app without a name")
}
- return newApplication(name)
+ return &Application{Name: name}
}
diff --git a/command.go b/command.go
index 0322972..dedaa25 100644
--- a/command.go
+++ b/command.go
@@ -40,13 +40,8 @@ type Command struct {
// in the split terminal window.
Help string
- // Category is an optional header that might be displayed
- // over a block of commands. Commands with the the same
- // Category share the same block.
- //
- // Example: "misc" commands would include: help, version, config
- // Note that categories are uppercased automatically
- Category string
+ // Group is smth.
+ Group string
// Handling, I bet it's pretty straight-forward.
Handle CmdHandler
diff --git a/help.go b/help.go
index 766a4fb..3e09813 100644
--- a/help.go
+++ b/help.go
@@ -12,12 +12,13 @@ Usage:
{{.Name}} {{if .Commands}}command [arguments]{{end}}
-{{if .Commands}}{{if (index (index .Categories 0) 0).Category}}{{else}}The commands are:{{end}}
-
-{{range $_, $commands := $.Categories}}
-{{ $cat := (index $commands 0).Category}}{{if $cat}}{{$cat}} COMMANDS:{{end}}
-
- {{range $commands}}{{.Name | printf "%-11s"}} {{.Brief}}
+{{if .Commands}}The commands are:
+ {{if .UngroupedCount}}{{range .Commands}}
+ {{if not .Group}}{{.Name | printf "%-11s"}} {{.Brief}}{{end}}{{end}}
+ {{end}}{{range .Groups}}{{if .Commands}}
+ # {{.Name}}
+ {{range .Commands}}
+ {{.Name | printf "%-11s"}} {{.Brief}}{{end}}
{{end}}{{end}}
Use "{{.Name}} help [command]" for more information about a command.{{end}}
{{if .Topics}}
@@ -103,7 +104,13 @@ func flagUsage(flag Flag) string {
}
func (a *Application) globalHelp() string {
- return templated(globalHelpTemplate, *a)
+ return templated(globalHelpTemplate, struct {
+ Application
+ UngroupedCount int
+ }{
+ *a,
+ a.ungroupedCmdsCount,
+ })
}
func (a *Application) commandHelp(command *Command) string {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment