Skip to content

Commit 1d47add

Browse files
committed
Merge pull request #401 from mattfarina/cleanup
Code cleanup from Go Report Card
2 parents 63ed8b0 + 07ce8bf commit 1d47add

12 files changed

Lines changed: 146 additions & 110 deletions

app.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type App struct {
5151
HideHelp bool
5252
// Boolean to hide built-in version flag and the VERSION section of help
5353
HideVersion bool
54-
// Populate on app startup, only gettable throught method Categories()
54+
// Populate on app startup, only gettable through method Categories()
5555
categories CommandCategories
5656
// An action to execute when the bash-completion flag is set
5757
BashComplete BashCompleteFunc
@@ -100,7 +100,8 @@ func compileTime() time.Time {
100100
return info.ModTime()
101101
}
102102

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

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

@@ -187,11 +189,10 @@ func (a *App) Run(arguments []string) (err error) {
187189
err := a.OnUsageError(context, err, false)
188190
HandleExitCoder(err)
189191
return err
190-
} else {
191-
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
192-
ShowAppHelp(context)
193-
return err
194192
}
193+
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
194+
ShowAppHelp(context)
195+
return err
195196
}
196197

197198
if !a.HideHelp && checkHelp(context) {
@@ -254,7 +255,8 @@ func (a *App) RunAndExitOnError() {
254255
}
255256
}
256257

257-
// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
258+
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
259+
// generate command-specific flags
258260
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
259261
// append help to commands
260262
if len(a.Commands) > 0 {
@@ -307,11 +309,10 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
307309
err = a.OnUsageError(context, err, true)
308310
HandleExitCoder(err)
309311
return err
310-
} else {
311-
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
312-
ShowSubcommandHelp(context)
313-
return err
314312
}
313+
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
314+
ShowSubcommandHelp(context)
315+
return err
315316
}
316317

317318
if len(a.Commands) > 0 {
@@ -363,7 +364,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
363364
return err
364365
}
365366

366-
// Returns the named command on App. Returns nil if the command does not exist
367+
// Command returns the named command on App. Returns nil if the command does not exist
367368
func (a *App) Command(name string) *Command {
368369
for _, c := range a.Commands {
369370
if c.HasName(name) {

app_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func ExampleApp_Run() {
3333
app.UsageText = "app [first_arg] [second_arg]"
3434
app.Author = "Harrison"
3535
app.Email = "harrison@lolwut.com"
36-
app.Authors = []Author{Author{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
36+
app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
3737
app.Run(os.Args)
3838
// Output:
3939
// Hello Jeremy
@@ -307,12 +307,12 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
307307
func TestApp_VisibleCommands(t *testing.T) {
308308
app := NewApp()
309309
app.Commands = []Command{
310-
Command{
310+
{
311311
Name: "frob",
312312
HelpName: "foo frob",
313313
Action: func(_ *Context) error { return nil },
314314
},
315-
Command{
315+
{
316316
Name: "frib",
317317
HelpName: "foo frib",
318318
Hidden: true,
@@ -517,7 +517,7 @@ func TestApp_BeforeFunc(t *testing.T) {
517517
}
518518

519519
app.Commands = []Command{
520-
Command{
520+
{
521521
Name: "sub",
522522
Action: func(c *Context) error {
523523
counts.Total++
@@ -609,7 +609,7 @@ func TestApp_AfterFunc(t *testing.T) {
609609
}
610610

611611
app.Commands = []Command{
612-
Command{
612+
{
613613
Name: "sub",
614614
Action: func(c *Context) error {
615615
counts.Total++
@@ -724,7 +724,7 @@ func TestApp_CommandNotFound(t *testing.T) {
724724
}
725725

726726
app.Commands = []Command{
727-
Command{
727+
{
728728
Name: "bar",
729729
Action: func(c *Context) error {
730730
counts.Total++
@@ -791,7 +791,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
791791

792792
app.After = afterNoError
793793
app.Commands = []Command{
794-
Command{
794+
{
795795
Name: "bar",
796796
Action: func(c *Context) error {
797797
counts.Total++
@@ -1126,15 +1126,15 @@ func TestApp_Run_Categories(t *testing.T) {
11261126
app := NewApp()
11271127
app.Name = "categories"
11281128
app.Commands = []Command{
1129-
Command{
1129+
{
11301130
Name: "command1",
11311131
Category: "1",
11321132
},
1133-
Command{
1133+
{
11341134
Name: "command2",
11351135
Category: "1",
11361136
},
1137-
Command{
1137+
{
11381138
Name: "command3",
11391139
Category: "2",
11401140
},
@@ -1175,32 +1175,32 @@ func TestApp_VisibleCategories(t *testing.T) {
11751175
app := NewApp()
11761176
app.Name = "visible-categories"
11771177
app.Commands = []Command{
1178-
Command{
1178+
{
11791179
Name: "command1",
11801180
Category: "1",
11811181
HelpName: "foo command1",
11821182
Hidden: true,
11831183
},
1184-
Command{
1184+
{
11851185
Name: "command2",
11861186
Category: "2",
11871187
HelpName: "foo command2",
11881188
},
1189-
Command{
1189+
{
11901190
Name: "command3",
11911191
Category: "3",
11921192
HelpName: "foo command3",
11931193
},
11941194
}
11951195

11961196
expected := []*CommandCategory{
1197-
&CommandCategory{
1197+
{
11981198
Name: "2",
11991199
Commands: []Command{
12001200
app.Commands[1],
12011201
},
12021202
},
1203-
&CommandCategory{
1203+
{
12041204
Name: "3",
12051205
Commands: []Command{
12061206
app.Commands[2],
@@ -1214,27 +1214,27 @@ func TestApp_VisibleCategories(t *testing.T) {
12141214
app = NewApp()
12151215
app.Name = "visible-categories"
12161216
app.Commands = []Command{
1217-
Command{
1217+
{
12181218
Name: "command1",
12191219
Category: "1",
12201220
HelpName: "foo command1",
12211221
Hidden: true,
12221222
},
1223-
Command{
1223+
{
12241224
Name: "command2",
12251225
Category: "2",
12261226
HelpName: "foo command2",
12271227
Hidden: true,
12281228
},
1229-
Command{
1229+
{
12301230
Name: "command3",
12311231
Category: "3",
12321232
HelpName: "foo command3",
12331233
},
12341234
}
12351235

12361236
expected = []*CommandCategory{
1237-
&CommandCategory{
1237+
{
12381238
Name: "3",
12391239
Commands: []Command{
12401240
app.Commands[2],
@@ -1248,19 +1248,19 @@ func TestApp_VisibleCategories(t *testing.T) {
12481248
app = NewApp()
12491249
app.Name = "visible-categories"
12501250
app.Commands = []Command{
1251-
Command{
1251+
{
12521252
Name: "command1",
12531253
Category: "1",
12541254
HelpName: "foo command1",
12551255
Hidden: true,
12561256
},
1257-
Command{
1257+
{
12581258
Name: "command2",
12591259
Category: "2",
12601260
HelpName: "foo command2",
12611261
Hidden: true,
12621262
},
1263-
Command{
1263+
{
12641264
Name: "command3",
12651265
Category: "3",
12661266
HelpName: "foo command3",
@@ -1296,9 +1296,9 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
12961296
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
12971297
app := NewApp()
12981298
app.Commands = []Command{
1299-
Command{
1299+
{
13001300
Subcommands: []Command{
1301-
Command{
1301+
{
13021302
Name: "sub",
13031303
},
13041304
},
@@ -1336,7 +1336,7 @@ func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
13361336
return errors.New("intercepted: " + err.Error())
13371337
}
13381338
app.Commands = []Command{
1339-
Command{
1339+
{
13401340
Name: "bar",
13411341
},
13421342
}
@@ -1366,7 +1366,7 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
13661366
return errors.New("intercepted: " + err.Error())
13671367
}
13681368
app.Commands = []Command{
1369-
Command{
1369+
{
13701370
Name: "bar",
13711371
},
13721372
}

category.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cli
22

3+
// CommandCategories is a slice of *CommandCategory.
34
type CommandCategories []*CommandCategory
45

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

24+
// AddCommand adds a command to a category.
2225
func (c CommandCategories) AddCommand(category string, command Command) CommandCategories {
2326
for _, commandCategory := range c {
2427
if commandCategory.Name == category {

command.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type Command struct {
5656
commandNamePath []string
5757
}
5858

59-
// Returns the full name of the command.
59+
// FullName returns the full name of the command.
6060
// For subcommands this ensures that parent commands are part of the command path
6161
func (c Command) FullName() string {
6262
if c.commandNamePath == nil {
@@ -65,9 +65,10 @@ func (c Command) FullName() string {
6565
return strings.Join(c.commandNamePath, " ")
6666
}
6767

68+
// Commands is a slice of Command
6869
type Commands []Command
6970

70-
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
71+
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
7172
func (c Command) Run(ctx *Context) (err error) {
7273
if len(c.Subcommands) > 0 {
7374
return c.startApp(ctx)
@@ -131,12 +132,11 @@ func (c Command) Run(ctx *Context) (err error) {
131132
err := c.OnUsageError(ctx, err, false)
132133
HandleExitCoder(err)
133134
return err
134-
} else {
135-
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
136-
fmt.Fprintln(ctx.App.Writer)
137-
ShowCommandHelp(ctx, c.Name)
138-
return err
139135
}
136+
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
137+
fmt.Fprintln(ctx.App.Writer)
138+
ShowCommandHelp(ctx, c.Name)
139+
return err
140140
}
141141

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

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

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

204-
// Returns true if Command.Name or Command.ShortName matches given name
205+
// HasName returns true if Command.Name or Command.ShortName matches given name
205206
func (c Command) HasName(name string) bool {
206207
for _, n := range c.Names() {
207208
if n == name {

command_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestCommandFlagParsing(t *testing.T) {
4949
func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
5050
app := NewApp()
5151
app.Commands = []Command{
52-
Command{
52+
{
5353
Name: "bar",
5454
Before: func(c *Context) error {
5555
return fmt.Errorf("before error")
@@ -76,7 +76,7 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
7676
func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
7777
app := NewApp()
7878
app.Commands = []Command{
79-
Command{
79+
{
8080
Name: "bar",
8181
Flags: []Flag{
8282
IntFlag{Name: "flag"},

0 commit comments

Comments
 (0)