| 1 | package cli |
| 2 | |
| 3 | import "sort" |
| 4 | |
| 5 | // CommandCategories interface allows for category manipulation |
| 6 | type CommandCategories interface { |
| 7 | // AddCommand adds a command to a category, creating a new category if necessary. |
| 8 | AddCommand(category string, command *Command) |
| 9 | // Categories returns a slice of categories sorted by name |
| 10 | Categories() []CommandCategory |
| 11 | } |
| 12 | |
| 13 | type commandCategories []*commandCategory |
| 14 | |
| 15 | func newCommandCategories() CommandCategories { |
| 16 | ret := commandCategories([]*commandCategory{}) |
| 17 | return &ret |
| 18 | } |
| 19 | |
| 20 | func (c *commandCategories) Less(i, j int) bool { |
| 21 | return lexicographicLess((*c)[i].Name(), (*c)[j].Name()) |
| 22 | } |
| 23 | |
| 24 | func (c *commandCategories) Len() int { |
| 25 | return len(*c) |
| 26 | } |
| 27 | |
| 28 | func (c *commandCategories) Swap(i, j int) { |
| 29 | (*c)[i], (*c)[j] = (*c)[j], (*c)[i] |
| 30 | } |
| 31 | |
| 32 | func (c *commandCategories) AddCommand(category string, command *Command) { |
| 33 | for _, commandCategory := range []*commandCategory(*c) { |
| 34 | if commandCategory.name == category { |
| 35 | commandCategory.commands = append(commandCategory.commands, command) |
| 36 | return |
| 37 | } |
| 38 | } |
| 39 | newVal := append(*c, |
| 40 | &commandCategory{name: category, commands: []*Command{command}}) |
| 41 | *c = newVal |
| 42 | } |
| 43 | |
| 44 | func (c *commandCategories) Categories() []CommandCategory { |
| 45 | ret := make([]CommandCategory, len(*c)) |
| 46 | for i, cat := range *c { |
| 47 | ret[i] = cat |
| 48 | } |
| 49 | return ret |
| 50 | } |
| 51 | |
| 52 | // CommandCategory is a category containing commands. |
| 53 | type CommandCategory interface { |
| 54 | // Name returns the category name string |
| 55 | Name() string |
| 56 | // VisibleCommands returns a slice of the Commands with Hidden=false |
| 57 | VisibleCommands() []*Command |
| 58 | } |
| 59 | |
| 60 | type commandCategory struct { |
| 61 | name string |
| 62 | commands []*Command |
| 63 | } |
| 64 | |
| 65 | func (c *commandCategory) Name() string { |
| 66 | return c.name |
| 67 | } |
| 68 | |
| 69 | func (c *commandCategory) VisibleCommands() []*Command { |
| 70 | if c.commands == nil { |
| 71 | c.commands = []*Command{} |
| 72 | } |
| 73 | |
| 74 | var ret []*Command |
| 75 | for _, command := range c.commands { |
| 76 | if !command.Hidden { |
| 77 | ret = append(ret, command) |
| 78 | } |
| 79 | } |
| 80 | return ret |
| 81 | } |
| 82 | |
| 83 | // FlagCategories interface allows for category manipulation |
| 84 | type FlagCategories interface { |
| 85 | // AddFlags adds a flag to a category, creating a new category if necessary. |
| 86 | AddFlag(category string, fl Flag) |
| 87 | // VisibleCategories returns a slice of visible flag categories sorted by name |
| 88 | VisibleCategories() []VisibleFlagCategory |
| 89 | } |
| 90 | |
| 91 | type defaultFlagCategories struct { |
| 92 | m map[string]*defaultVisibleFlagCategory |
| 93 | } |
| 94 | |
| 95 | func newFlagCategories() FlagCategories { |
| 96 | return &defaultFlagCategories{ |
| 97 | m: map[string]*defaultVisibleFlagCategory{}, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func newFlagCategoriesFromFlags(fs []Flag) FlagCategories { |
| 102 | fc := newFlagCategories() |
| 103 | |
| 104 | var categorized bool |
| 105 | |
| 106 | for _, fl := range fs { |
| 107 | if cf, ok := fl.(CategorizableFlag); ok { |
| 108 | visible := false |
| 109 | if vf, ok := fl.(VisibleFlag); ok { |
| 110 | visible = vf.IsVisible() |
| 111 | } |
| 112 | if cat := cf.GetCategory(); cat != "" && visible { |
| 113 | fc.AddFlag(cat, fl) |
| 114 | categorized = true |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if categorized { |
| 120 | for _, fl := range fs { |
| 121 | if cf, ok := fl.(CategorizableFlag); ok { |
| 122 | visible := false |
| 123 | if vf, ok := fl.(VisibleFlag); ok { |
| 124 | visible = vf.IsVisible() |
| 125 | } |
| 126 | if cf.GetCategory() == "" && visible { |
| 127 | fc.AddFlag("", fl) |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return fc |
| 134 | } |
| 135 | |
| 136 | func (f *defaultFlagCategories) AddFlag(category string, fl Flag) { |
| 137 | if _, ok := f.m[category]; !ok { |
| 138 | f.m[category] = &defaultVisibleFlagCategory{name: category, m: map[string]Flag{}} |
| 139 | } |
| 140 | |
| 141 | f.m[category].m[fl.String()] = fl |
| 142 | } |
| 143 | |
| 144 | func (f *defaultFlagCategories) VisibleCategories() []VisibleFlagCategory { |
| 145 | catNames := []string{} |
| 146 | for name := range f.m { |
| 147 | catNames = append(catNames, name) |
| 148 | } |
| 149 | |
| 150 | sort.Strings(catNames) |
| 151 | |
| 152 | ret := make([]VisibleFlagCategory, len(catNames)) |
| 153 | for i, name := range catNames { |
| 154 | ret[i] = f.m[name] |
| 155 | } |
| 156 | |
| 157 | return ret |
| 158 | } |
| 159 | |
| 160 | // VisibleFlagCategory is a category containing flags. |
| 161 | type VisibleFlagCategory interface { |
| 162 | // Name returns the category name string |
| 163 | Name() string |
| 164 | // Flags returns a slice of VisibleFlag sorted by name |
| 165 | Flags() []Flag |
| 166 | } |
| 167 | |
| 168 | type defaultVisibleFlagCategory struct { |
| 169 | name string |
| 170 | m map[string]Flag |
| 171 | } |
| 172 | |
| 173 | func (fc *defaultVisibleFlagCategory) Name() string { |
| 174 | return fc.name |
| 175 | } |
| 176 | |
| 177 | func (fc *defaultVisibleFlagCategory) Flags() []Flag { |
| 178 | vfNames := []string{} |
| 179 | for flName, fl := range fc.m { |
| 180 | if vf, ok := fl.(VisibleFlag); ok { |
| 181 | if vf.IsVisible() { |
| 182 | vfNames = append(vfNames, flName) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | sort.Strings(vfNames) |
| 188 | |
| 189 | ret := make([]Flag, len(vfNames)) |
| 190 | for i, flName := range vfNames { |
| 191 | ret[i] = fc.m[flName] |
| 192 | } |
| 193 | |
| 194 | return ret |
| 195 | } |