make categories private

This commit is contained in:
Ajitem Sahasrabuddhe 2019-10-19 10:13:03 +05:30
parent 3415a1aade
commit 3bd997859c
No known key found for this signature in database
GPG Key ID: 782DEBC01D3967A5
4 changed files with 12 additions and 12 deletions

12
app.go
View File

@ -47,8 +47,8 @@ type App struct {
HideHelp bool HideHelp bool
// Boolean to hide built-in version flag and the VERSION section of help // Boolean to hide built-in version flag and the VERSION section of help
HideVersion bool HideVersion bool
// Categories contains the categorized commands and is populated on app startup // categories contains the categorized commands and is populated on app startup
Categories CommandCategories categories CommandCategories
// An action to execute when the shell completion flag is set // An action to execute when the shell completion flag is set
BashComplete BashCompleteFunc BashComplete BashCompleteFunc
// An action to execute before any subcommands are run, but after the context is ready // An action to execute before any subcommands are run, but after the context is ready
@ -182,11 +182,11 @@ func (a *App) Setup() {
a.appendFlag(VersionFlag) a.appendFlag(VersionFlag)
} }
a.Categories = newCommandCategories() a.categories = newCommandCategories()
for _, command := range a.Commands { for _, command := range a.Commands {
a.Categories.AddCommand(command.Category, command) a.categories.AddCommand(command.Category, command)
} }
sort.Sort(a.Categories.(*commandCategories)) sort.Sort(a.categories.(*commandCategories))
if a.Metadata == nil { if a.Metadata == nil {
a.Metadata = make(map[string]interface{}) a.Metadata = make(map[string]interface{})
@ -449,7 +449,7 @@ func (a *App) Command(name string) *Command {
// Hidden=false // Hidden=false
func (a *App) VisibleCategories() []CommandCategory { func (a *App) VisibleCategories() []CommandCategory {
ret := []CommandCategory{} ret := []CommandCategory{}
for _, category := range a.Categories.Categories() { for _, category := range a.categories.Categories() {
if visible := func() CommandCategory { if visible := func() CommandCategory {
if len(category.VisibleCommands()) > 0 { if len(category.VisibleCommands()) > 0 {
return category return category

View File

@ -1719,8 +1719,8 @@ func TestApp_Run_Categories(t *testing.T) {
}, },
}) })
if !reflect.DeepEqual(app.Categories, &expect) { if !reflect.DeepEqual(app.categories, &expect) {
t.Fatalf("expected categories %#v, to equal %#v", app.Categories, &expect) t.Fatalf("expected categories %#v, to equal %#v", app.categories, &expect)
} }
output := buf.String() output := buf.String()

View File

@ -3,7 +3,7 @@ package cli
type CommandCategories interface { type CommandCategories interface {
// AddCommand adds a command to a category, creating a new category if necessary. // AddCommand adds a command to a category, creating a new category if necessary.
AddCommand(category string, command *Command) AddCommand(category string, command *Command)
// Categories returns a copy of the category slice // categories returns a copy of the category slice
Categories() []CommandCategory Categories() []CommandCategory
} }

View File

@ -245,12 +245,12 @@ func (c *Command) startApp(ctx *Context) error {
app.ExitErrHandler = ctx.App.ExitErrHandler app.ExitErrHandler = ctx.App.ExitErrHandler
app.UseShortOptionHandling = ctx.App.UseShortOptionHandling app.UseShortOptionHandling = ctx.App.UseShortOptionHandling
app.Categories = newCommandCategories() app.categories = newCommandCategories()
for _, command := range c.Subcommands { for _, command := range c.Subcommands {
app.Categories.AddCommand(command.Category, command) app.categories.AddCommand(command.Category, command)
} }
sort.Sort(app.Categories.(*commandCategories)) sort.Sort(app.categories.(*commandCategories))
// bash completion // bash completion
app.EnableBashCompletion = ctx.App.EnableBashCompletion app.EnableBashCompletion = ctx.App.EnableBashCompletion