Minimize struct copying

by using pointer func receivers and slices of struct pointers where possible.
This commit is contained in:
Dan Buch
2016-05-22 15:20:52 -04:00
parent b35c8a92d2
commit cd10b49473
15 changed files with 452 additions and 372 deletions

View File

@@ -3,7 +3,6 @@ package cli
import (
"fmt"
"io/ioutil"
"reflect"
"sort"
"strings"
)
@@ -37,7 +36,7 @@ type Command struct {
// Execute this function if a usage error occurs.
OnUsageError OnUsageErrorFunc
// List of child commands
Subcommands Commands
Subcommands []*Command
// List of flags to parse
Flags []Flag
// Treat all flags as normal arguments if true
@@ -54,32 +53,26 @@ type Command struct {
// FullName returns the full name of the command.
// For subcommands this ensures that parent commands are part of the command path
func (c Command) FullName() string {
func (c *Command) FullName() string {
if c.commandNamePath == nil {
return c.Name
}
return strings.Join(c.commandNamePath, " ")
}
// Commands is a slice of Command
type Commands []Command
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c Command) Run(ctx *Context) (err error) {
func (c *Command) Run(ctx *Context) (err error) {
if len(c.Subcommands) > 0 {
return c.startApp(ctx)
}
if !c.HideHelp && !reflect.DeepEqual(HelpFlag, BoolFlag{}) {
if !c.HideHelp && HelpFlag != nil {
// append help to flags
c.Flags = append(
c.Flags,
HelpFlag,
)
c.appendFlag(HelpFlag)
}
if ctx.App.EnableBashCompletion {
c.Flags = append(c.Flags, BashCompletionFlag)
c.appendFlag(BashCompletionFlag)
}
set := flagSet(c.Name, c.Flags)
@@ -156,13 +149,12 @@ func (c Command) Run(ctx *Context) (err error) {
}
// Names returns the names including short names and aliases.
func (c Command) Names() []string {
names := []string{c.Name}
return append(names, c.Aliases...)
func (c *Command) Names() []string {
return append([]string{c.Name}, c.Aliases...)
}
// HasName returns true if Command.Name matches given name
func (c Command) HasName(name string) bool {
func (c *Command) HasName(name string) bool {
for _, n := range c.Names() {
if n == name {
return true
@@ -171,7 +163,7 @@ func (c Command) HasName(name string) bool {
return false
}
func (c Command) startApp(ctx *Context) error {
func (c *Command) startApp(ctx *Context) error {
app := NewApp()
app.Metadata = ctx.App.Metadata
// set the name and usage
@@ -201,7 +193,7 @@ func (c Command) startApp(ctx *Context) error {
app.Compiled = ctx.App.Compiled
app.Writer = ctx.App.Writer
app.categories = CommandCategories{}
app.categories = NewCommandCategories()
for _, command := range c.Subcommands {
app.categories = app.categories.AddCommand(command.Category, command)
}
@@ -231,6 +223,22 @@ func (c Command) startApp(ctx *Context) error {
}
// VisibleFlags returns a slice of the Flags with Hidden=false
func (c Command) VisibleFlags() []Flag {
func (c *Command) VisibleFlags() []Flag {
return visibleFlags(c.Flags)
}
func (c *Command) appendFlag(fl Flag) {
if !hasFlag(c.Flags, fl) {
c.Flags = append(c.Flags, fl)
}
}
func hasCommand(commands []*Command, command *Command) bool {
for _, existing := range commands {
if command == existing {
return true
}
}
return false
}