Skip to content

Commit

Permalink
Merge branch 'add-flag-category-support' of ssh://github.com/michaelj…
Browse files Browse the repository at this point in the history
…s1990/cli into michaeljs1990-add-flag-category-support
  • Loading branch information
meatballhat committed Apr 22, 2022
2 parents 6033c00 + 50b52ca commit 4495869
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
15 changes: 15 additions & 0 deletions app.go
Expand Up @@ -52,6 +52,8 @@ type App struct {
HideVersion bool
// categories contains the categorized commands and is populated on app startup
categories CommandCategories
// Populate on app startup, only gettable through method Categories()
flagCategories FlagCategories
// An action to execute when the shell completion flag is set
BashComplete BashCompleteFunc
// An action to execute before any subcommands are run, but after the context is ready
Expand Down Expand Up @@ -181,6 +183,14 @@ func (a *App) Setup() {
if c.HelpName == "" {
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
}

fc := FlagCategories{}
for _, flag := range c.Flags {
fc = fc.AddFlag(flag.GetCategory(), flag)
}

sort.Sort(fc)
c.FlagCategories = fc
newCommands = append(newCommands, c)
}
a.Commands = newCommands
Expand Down Expand Up @@ -481,6 +491,11 @@ func (a *App) VisibleCommands() []*Command {
return ret
}

// Categories returns a slice containing all the categories with the commands they contain
func (a *App) VisibleFlagCategories() FlagCategories {
return a.flagCategories
}

// VisibleFlags returns a slice of the Flags with Hidden=false
func (a *App) VisibleFlags() []Flag {
return visibleFlags(a.Flags)
Expand Down
43 changes: 43 additions & 0 deletions category.go
Expand Up @@ -77,3 +77,46 @@ func (c *commandCategory) VisibleCommands() []*Command {
}
return ret
}

// FlagCategories is a slice of *FlagCategory.
type FlagCategories []*FlagCategory

// FlagCategory is a category containing commands.
type FlagCategory struct {
Name string
Flags Flags
}

func (f FlagCategories) Less(i, j int) bool {
return lexicographicLess(f[i].Name, f[j].Name)
}

func (f FlagCategories) Len() int {
return len(f)
}

func (f FlagCategories) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}

// AddFlags adds a command to a category.
func (f FlagCategories) AddFlag(category string, flag Flag) FlagCategories {
for _, flagCategory := range f {
if flagCategory.Name == category {
flagCategory.Flags = append(flagCategory.Flags, flag)
return f
}
}
return append(f, &FlagCategory{Name: category, Flags: []Flag{flag}})
}

// VisibleFlags returns a slice of the Flags with Hidden=false
func (c *FlagCategory) VisibleFlags() []Flag {
ret := []Flag{}
for _, flag := range c.Flags {
if !flag.GetHidden() {
ret = append(ret, flag)
}
}
return ret
}
7 changes: 7 additions & 0 deletions command.go
Expand Up @@ -39,6 +39,8 @@ type Command struct {
Subcommands []*Command
// List of flags to parse
Flags []Flag
// List of all flag categories
FlagCategories FlagCategories
// Treat all flags as normal arguments if true
SkipFlagParsing bool
// Boolean to hide built-in help command and help flag
Expand Down Expand Up @@ -280,6 +282,11 @@ func (c *Command) startApp(ctx *Context) error {
return app.RunAsSubcommand(ctx)
}

// Categories returns a slice containing all the categories with the commands they contain
func (c Command) VisibleFlagCategories() FlagCategories {
return c.FlagCategories
}

// VisibleFlags returns a slice of the Flags with Hidden=false
func (c *Command) VisibleFlags() []Flag {
return visibleFlags(c.Flags)
Expand Down
2 changes: 2 additions & 0 deletions flag.go
Expand Up @@ -94,6 +94,8 @@ type Flag interface {
Apply(*flag.FlagSet) error
Names() []string
IsSet() bool
GetCategory() string
GetHidden() bool
}

// RequiredFlag is an interface that allows us to mark flags as required
Expand Down
2 changes: 0 additions & 2 deletions go.sum
@@ -1,5 +1,3 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
Expand Down

0 comments on commit 4495869

Please sign in to comment.