Skip to content

Commit

Permalink
Merge pull request #401 from mattfarina/cleanup
Browse files Browse the repository at this point in the history
Code cleanup from Go Report Card
  • Loading branch information
meatballhat committed May 9, 2016
2 parents 63ed8b0 + 07ce8bf commit 1d47add
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 110 deletions.
27 changes: 14 additions & 13 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type App struct {
HideHelp bool
// Boolean to hide built-in version flag and the VERSION section of help
HideVersion bool
// Populate on app startup, only gettable throught method Categories()
// Populate on app startup, only gettable through method Categories()
categories CommandCategories
// An action to execute when the bash-completion flag is set
BashComplete BashCompleteFunc
Expand Down Expand Up @@ -100,7 +100,8 @@ func compileTime() time.Time {
return info.ModTime()
}

// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
// NewApp creates a new cli Application with some reasonable defaults for Name,
// Usage, Version and Action.
func NewApp() *App {
return &App{
Name: filepath.Base(os.Args[0]),
Expand Down Expand Up @@ -162,7 +163,8 @@ func (a *App) Setup() {
}
}

// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
// Run is the entry point to the cli app. Parses the arguments slice and routes
// to the proper flag/args combination
func (a *App) Run(arguments []string) (err error) {
a.Setup()

Expand All @@ -187,11 +189,10 @@ func (a *App) Run(arguments []string) (err error) {
err := a.OnUsageError(context, err, false)
HandleExitCoder(err)
return err
} else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowAppHelp(context)
return err
}
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowAppHelp(context)
return err
}

if !a.HideHelp && checkHelp(context) {
Expand Down Expand Up @@ -254,7 +255,8 @@ func (a *App) RunAndExitOnError() {
}
}

// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
// generate command-specific flags
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
// append help to commands
if len(a.Commands) > 0 {
Expand Down Expand Up @@ -307,11 +309,10 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
err = a.OnUsageError(context, err, true)
HandleExitCoder(err)
return err
} else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowSubcommandHelp(context)
return err
}
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowSubcommandHelp(context)
return err
}

if len(a.Commands) > 0 {
Expand Down Expand Up @@ -363,7 +364,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
return err
}

// Returns the named command on App. Returns nil if the command does not exist
// Command returns the named command on App. Returns nil if the command does not exist
func (a *App) Command(name string) *Command {
for _, c := range a.Commands {
if c.HasName(name) {
Expand Down
52 changes: 26 additions & 26 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ExampleApp_Run() {
app.UsageText = "app [first_arg] [second_arg]"
app.Author = "Harrison"
app.Email = "[email protected]"
app.Authors = []Author{Author{Name: "Oliver Allen", Email: "[email protected]"}}
app.Authors = []Author{{Name: "Oliver Allen", Email: "[email protected]"}}
app.Run(os.Args)
// Output:
// Hello Jeremy
Expand Down Expand Up @@ -307,12 +307,12 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
func TestApp_VisibleCommands(t *testing.T) {
app := NewApp()
app.Commands = []Command{
Command{
{
Name: "frob",
HelpName: "foo frob",
Action: func(_ *Context) error { return nil },
},
Command{
{
Name: "frib",
HelpName: "foo frib",
Hidden: true,
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestApp_BeforeFunc(t *testing.T) {
}

app.Commands = []Command{
Command{
{
Name: "sub",
Action: func(c *Context) error {
counts.Total++
Expand Down Expand Up @@ -609,7 +609,7 @@ func TestApp_AfterFunc(t *testing.T) {
}

app.Commands = []Command{
Command{
{
Name: "sub",
Action: func(c *Context) error {
counts.Total++
Expand Down Expand Up @@ -724,7 +724,7 @@ func TestApp_CommandNotFound(t *testing.T) {
}

app.Commands = []Command{
Command{
{
Name: "bar",
Action: func(c *Context) error {
counts.Total++
Expand Down Expand Up @@ -791,7 +791,7 @@ func TestApp_OrderOfOperations(t *testing.T) {

app.After = afterNoError
app.Commands = []Command{
Command{
{
Name: "bar",
Action: func(c *Context) error {
counts.Total++
Expand Down Expand Up @@ -1126,15 +1126,15 @@ func TestApp_Run_Categories(t *testing.T) {
app := NewApp()
app.Name = "categories"
app.Commands = []Command{
Command{
{
Name: "command1",
Category: "1",
},
Command{
{
Name: "command2",
Category: "1",
},
Command{
{
Name: "command3",
Category: "2",
},
Expand Down Expand Up @@ -1175,32 +1175,32 @@ func TestApp_VisibleCategories(t *testing.T) {
app := NewApp()
app.Name = "visible-categories"
app.Commands = []Command{
Command{
{
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
},
Command{
{
Name: "command2",
Category: "2",
HelpName: "foo command2",
},
Command{
{
Name: "command3",
Category: "3",
HelpName: "foo command3",
},
}

expected := []*CommandCategory{
&CommandCategory{
{
Name: "2",
Commands: []Command{
app.Commands[1],
},
},
&CommandCategory{
{
Name: "3",
Commands: []Command{
app.Commands[2],
Expand All @@ -1214,27 +1214,27 @@ func TestApp_VisibleCategories(t *testing.T) {
app = NewApp()
app.Name = "visible-categories"
app.Commands = []Command{
Command{
{
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
},
Command{
{
Name: "command2",
Category: "2",
HelpName: "foo command2",
Hidden: true,
},
Command{
{
Name: "command3",
Category: "3",
HelpName: "foo command3",
},
}

expected = []*CommandCategory{
&CommandCategory{
{
Name: "3",
Commands: []Command{
app.Commands[2],
Expand All @@ -1248,19 +1248,19 @@ func TestApp_VisibleCategories(t *testing.T) {
app = NewApp()
app.Name = "visible-categories"
app.Commands = []Command{
Command{
{
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
},
Command{
{
Name: "command2",
Category: "2",
HelpName: "foo command2",
Hidden: true,
},
Command{
{
Name: "command3",
Category: "3",
HelpName: "foo command3",
Expand Down Expand Up @@ -1296,9 +1296,9 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
app := NewApp()
app.Commands = []Command{
Command{
{
Subcommands: []Command{
Command{
{
Name: "sub",
},
},
Expand Down Expand Up @@ -1336,7 +1336,7 @@ func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
return errors.New("intercepted: " + err.Error())
}
app.Commands = []Command{
Command{
{
Name: "bar",
},
}
Expand Down Expand Up @@ -1366,7 +1366,7 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
return errors.New("intercepted: " + err.Error())
}
app.Commands = []Command{
Command{
{
Name: "bar",
},
}
Expand Down
3 changes: 3 additions & 0 deletions category.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli

// CommandCategories is a slice of *CommandCategory.
type CommandCategories []*CommandCategory

// CommandCategory is a category containing commands.
type CommandCategory struct {
Name string
Commands Commands
Expand All @@ -19,6 +21,7 @@ func (c CommandCategories) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}

// AddCommand adds a command to a category.
func (c CommandCategories) AddCommand(category string, command Command) CommandCategories {
for _, commandCategory := range c {
if commandCategory.Name == category {
Expand Down
17 changes: 9 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Command struct {
commandNamePath []string
}

// Returns the full name of the command.
// 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 {
if c.commandNamePath == nil {
Expand All @@ -65,9 +65,10 @@ func (c Command) FullName() string {
return strings.Join(c.commandNamePath, " ")
}

// Commands is a slice of Command
type Commands []Command

// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c Command) Run(ctx *Context) (err error) {
if len(c.Subcommands) > 0 {
return c.startApp(ctx)
Expand Down Expand Up @@ -131,12 +132,11 @@ func (c Command) Run(ctx *Context) (err error) {
err := c.OnUsageError(ctx, err, false)
HandleExitCoder(err)
return err
} else {
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
fmt.Fprintln(ctx.App.Writer)
ShowCommandHelp(ctx, c.Name)
return err
}
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
fmt.Fprintln(ctx.App.Writer)
ShowCommandHelp(ctx, c.Name)
return err
}

nerr := normalizeFlags(c.Flags, set)
Expand Down Expand Up @@ -191,6 +191,7 @@ func (c Command) Run(ctx *Context) (err error) {
return err
}

// Names returns the names including short names and aliases.
func (c Command) Names() []string {
names := []string{c.Name}

Expand All @@ -201,7 +202,7 @@ func (c Command) Names() []string {
return append(names, c.Aliases...)
}

// Returns true if Command.Name or Command.ShortName matches given name
// HasName returns true if Command.Name or Command.ShortName matches given name
func (c Command) HasName(name string) bool {
for _, n := range c.Names() {
if n == name {
Expand Down
4 changes: 2 additions & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCommandFlagParsing(t *testing.T) {
func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
app := NewApp()
app.Commands = []Command{
Command{
{
Name: "bar",
Before: func(c *Context) error {
return fmt.Errorf("before error")
Expand All @@ -76,7 +76,7 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
app := NewApp()
app.Commands = []Command{
Command{
{
Name: "bar",
Flags: []Flag{
IntFlag{Name: "flag"},
Expand Down
Loading

0 comments on commit 1d47add

Please sign in to comment.