Skip to content

Commit f983141

Browse files
authored
Merge pull request #1626 from urfave/rm-newapp
Remove `NewApp` func
2 parents 859b1e3 + 7919d3a commit f983141

File tree

5 files changed

+100
-171
lines changed

5 files changed

+100
-171
lines changed

app.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ var (
2424
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
2525
)
2626

27-
// App is the main structure of a cli application. It is recommended that
28-
// an app be created with the cli.NewApp() function
27+
// App is the main structure of a cli application.
2928
type App struct {
3029
// The name of the program. Defaults to path.Base(os.Args[0])
3130
Name string
@@ -126,21 +125,6 @@ type SuggestFlagFunc func(flags []Flag, provided string, hideHelp bool) string
126125

127126
type SuggestCommandFunc func(commands []*Command, provided string) string
128127

129-
// NewApp creates a new cli Application with some reasonable defaults for Name,
130-
// Usage, Version and Action.
131-
func NewApp() *App {
132-
return &App{
133-
Name: filepath.Base(os.Args[0]),
134-
Usage: "A new cli application",
135-
UsageText: "",
136-
ShellComplete: DefaultAppComplete,
137-
Action: helpCommand.Action,
138-
Reader: os.Stdin,
139-
Writer: os.Stdout,
140-
ErrWriter: os.Stderr,
141-
}
142-
}
143-
144128
// Setup runs initialization code to ensure all data structures are ready for
145129
// `Run` or inspection prior to `Run`. It is internally called by `Run`, but
146130
// will return early if setup has already happened.

app_test.go

Lines changed: 97 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,18 @@ func ExampleApp_Run_bashComplete_withShortFlag() {
234234
os.Setenv("SHELL", "bash")
235235
os.Args = []string{"greet", "-", "--generate-shell-completion"}
236236

237-
app := NewApp()
238-
app.Name = "greet"
239-
app.EnableShellCompletion = true
240-
app.Flags = []Flag{
241-
&IntFlag{
242-
Name: "other",
243-
Aliases: []string{"o"},
244-
},
245-
&StringFlag{
246-
Name: "xyz",
247-
Aliases: []string{"x"},
237+
app := &App{
238+
Name: "greet",
239+
EnableShellCompletion: true,
240+
Flags: []Flag{
241+
&IntFlag{
242+
Name: "other",
243+
Aliases: []string{"o"},
244+
},
245+
&StringFlag{
246+
Name: "xyz",
247+
Aliases: []string{"x"},
248+
},
248249
},
249250
}
250251

@@ -262,23 +263,24 @@ func ExampleApp_Run_bashComplete_withLongFlag() {
262263
os.Setenv("SHELL", "bash")
263264
os.Args = []string{"greet", "--s", "--generate-shell-completion"}
264265

265-
app := NewApp()
266-
app.Name = "greet"
267-
app.EnableShellCompletion = true
268-
app.Flags = []Flag{
269-
&IntFlag{
270-
Name: "other",
271-
Aliases: []string{"o"},
272-
},
273-
&StringFlag{
274-
Name: "xyz",
275-
Aliases: []string{"x"},
276-
},
277-
&StringFlag{
278-
Name: "some-flag,s",
279-
},
280-
&StringFlag{
281-
Name: "similar-flag",
266+
app := &App{
267+
Name: "greet",
268+
EnableShellCompletion: true,
269+
Flags: []Flag{
270+
&IntFlag{
271+
Name: "other",
272+
Aliases: []string{"o"},
273+
},
274+
&StringFlag{
275+
Name: "xyz",
276+
Aliases: []string{"x"},
277+
},
278+
&StringFlag{
279+
Name: "some-flag,s",
280+
},
281+
&StringFlag{
282+
Name: "similar-flag",
283+
},
282284
},
283285
}
284286

@@ -292,26 +294,27 @@ func ExampleApp_Run_bashComplete_withMultipleLongFlag() {
292294
os.Setenv("SHELL", "bash")
293295
os.Args = []string{"greet", "--st", "--generate-shell-completion"}
294296

295-
app := NewApp()
296-
app.Name = "greet"
297-
app.EnableShellCompletion = true
298-
app.Flags = []Flag{
299-
&IntFlag{
300-
Name: "int-flag",
301-
Aliases: []string{"i"},
302-
},
303-
&StringFlag{
304-
Name: "string",
305-
Aliases: []string{"s"},
306-
},
307-
&StringFlag{
308-
Name: "string-flag-2",
309-
},
310-
&StringFlag{
311-
Name: "similar-flag",
312-
},
313-
&StringFlag{
314-
Name: "some-flag",
297+
app := &App{
298+
Name: "greet",
299+
EnableShellCompletion: true,
300+
Flags: []Flag{
301+
&IntFlag{
302+
Name: "int-flag",
303+
Aliases: []string{"i"},
304+
},
305+
&StringFlag{
306+
Name: "string",
307+
Aliases: []string{"s"},
308+
},
309+
&StringFlag{
310+
Name: "string-flag-2",
311+
},
312+
&StringFlag{
313+
Name: "similar-flag",
314+
},
315+
&StringFlag{
316+
Name: "some-flag",
317+
},
315318
},
316319
}
317320

@@ -364,26 +367,27 @@ func ExampleApp_Run_zshComplete() {
364367
os.Args = []string{"greet", "--generate-shell-completion"}
365368
_ = os.Setenv("SHELL", "/usr/bin/zsh")
366369

367-
app := NewApp()
368-
app.Name = "greet"
369-
app.EnableShellCompletion = true
370-
app.Commands = []*Command{
371-
{
372-
Name: "describeit",
373-
Aliases: []string{"d"},
374-
Usage: "use it to see a description",
375-
Description: "This is how we describe describeit the function",
376-
Action: func(*Context) error {
377-
fmt.Printf("i like to describe things")
378-
return nil
379-
},
380-
}, {
381-
Name: "next",
382-
Usage: "next example",
383-
Description: "more stuff to see when generating bash completion",
384-
Action: func(*Context) error {
385-
fmt.Printf("the next example")
386-
return nil
370+
app := &App{
371+
Name: "greet",
372+
EnableShellCompletion: true,
373+
Commands: []*Command{
374+
{
375+
Name: "describeit",
376+
Aliases: []string{"d"},
377+
Usage: "use it to see a description",
378+
Description: "This is how we describe describeit the function",
379+
Action: func(*Context) error {
380+
fmt.Printf("i like to describe things")
381+
return nil
382+
},
383+
}, {
384+
Name: "next",
385+
Usage: "next example",
386+
Description: "more stuff to see when generating bash completion",
387+
Action: func(*Context) error {
388+
fmt.Printf("the next example")
389+
return nil
390+
},
387391
},
388392
},
389393
}
@@ -406,13 +410,14 @@ func ExampleApp_Run_sliceValues() {
406410
"--int64Sclice", "13,14", "--int64Sclice", "15,16",
407411
"--intSclice", "13,14", "--intSclice", "15,16",
408412
}
409-
app := NewApp()
410-
app.Name = "multi_values"
411-
app.Flags = []Flag{
412-
&StringSliceFlag{Name: "stringSclice"},
413-
&Float64SliceFlag{Name: "float64Sclice"},
414-
&Int64SliceFlag{Name: "int64Sclice"},
415-
&IntSliceFlag{Name: "intSclice"},
413+
app := &App{
414+
Name: "multi_values",
415+
Flags: []Flag{
416+
&StringSliceFlag{Name: "stringSclice"},
417+
&Float64SliceFlag{Name: "float64Sclice"},
418+
&Int64SliceFlag{Name: "int64Sclice"},
419+
&IntSliceFlag{Name: "intSclice"},
420+
},
416421
}
417422
app.Action = func(ctx *Context) error {
418423
for i, v := range ctx.FlagNames() {
@@ -438,19 +443,20 @@ func ExampleApp_Run_mapValues() {
438443
"multi_values",
439444
"--stringMap", "parsed1=parsed two", "--stringMap", "parsed3=",
440445
}
441-
app := NewApp()
442-
app.Name = "multi_values"
443-
app.Flags = []Flag{
444-
&StringMapFlag{Name: "stringMap"},
445-
}
446-
app.Action = func(ctx *Context) error {
447-
for i, v := range ctx.FlagNames() {
448-
fmt.Printf("%d-%s %#v\n", i, v, ctx.StringMap(v))
449-
}
450-
fmt.Printf("notfound %#v\n", ctx.StringMap("notfound"))
451-
err := ctx.Err()
452-
fmt.Println("error:", err)
453-
return err
446+
app := &App{
447+
Name: "multi_values",
448+
Flags: []Flag{
449+
&StringMapFlag{Name: "stringMap"},
450+
},
451+
Action: func(ctx *Context) error {
452+
for i, v := range ctx.FlagNames() {
453+
fmt.Printf("%d-%s %#v\n", i, v, ctx.StringMap(v))
454+
}
455+
fmt.Printf("notfound %#v\n", ctx.StringMap("notfound"))
456+
err := ctx.Err()
457+
fmt.Println("error:", err)
458+
return err
459+
},
454460
}
455461

456462
_ = app.Run(os.Args)
@@ -2711,8 +2717,9 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
27112717
}
27122718

27132719
func newTestApp() *App {
2714-
a := NewApp()
2715-
a.Writer = io.Discard
2720+
a := &App{
2721+
Writer: io.Discard,
2722+
}
27162723
return a
27172724
}
27182725

godoc-current.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,7 @@ type App struct {
333333

334334
// Has unexported fields.
335335
}
336-
App is the main structure of a cli application. It is recommended that an
337-
app be created with the cli.NewApp() function
338-
339-
func NewApp() *App
340-
NewApp creates a new cli Application with some reasonable defaults for Name,
341-
Usage, Version and Action.
336+
App is the main structure of a cli application.
342337

343338
func (a *App) Command(name string) *Command
344339
Command returns the named command on App. Returns nil if the command does

help_test.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -574,58 +574,6 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
574574
}
575575
}
576576

577-
func TestHelpNameConsistency(t *testing.T) {
578-
// Setup some very basic templates based on actual AppHelp, CommandHelp
579-
// and SubcommandHelp templates to display the help name
580-
// The inconsistency shows up when users use NewApp() as opposed to
581-
// using App{...} directly
582-
SubcommandHelpTemplate = `{{.HelpName}}`
583-
app := NewApp()
584-
app.Name = "bar"
585-
app.CustomAppHelpTemplate = `{{.HelpName}}`
586-
app.Commands = []*Command{
587-
{
588-
Name: "command1",
589-
CustomHelpTemplate: `{{.HelpName}}`,
590-
Commands: []*Command{
591-
{
592-
Name: "subcommand1",
593-
CustomHelpTemplate: `{{.HelpName}}`,
594-
},
595-
},
596-
},
597-
}
598-
599-
tests := []struct {
600-
name string
601-
args []string
602-
}{
603-
{
604-
name: "App help",
605-
args: []string{"foo"},
606-
},
607-
{
608-
name: "Command help",
609-
args: []string{"foo", "command1"},
610-
},
611-
{
612-
name: "Subcommand help",
613-
args: []string{"foo", "command1", "subcommand1"},
614-
},
615-
}
616-
617-
for _, tt := range tests {
618-
output := &bytes.Buffer{}
619-
app.Writer = output
620-
if err := app.Run(tt.args); err != nil {
621-
t.Error(err)
622-
}
623-
if !strings.Contains(output.String(), "bar") {
624-
t.Errorf("expected output to contain bar; got: %q", output.String())
625-
}
626-
}
627-
}
628-
629577
func TestShowSubcommandHelp_CommandAliases(t *testing.T) {
630578
app := &App{
631579
Commands: []*Command{

testdata/godoc-v3.x.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,7 @@ type App struct {
333333

334334
// Has unexported fields.
335335
}
336-
App is the main structure of a cli application. It is recommended that an
337-
app be created with the cli.NewApp() function
338-
339-
func NewApp() *App
340-
NewApp creates a new cli Application with some reasonable defaults for Name,
341-
Usage, Version and Action.
336+
App is the main structure of a cli application.
342337

343338
func (a *App) Command(name string) *Command
344339
Command returns the named command on App. Returns nil if the command does

0 commit comments

Comments
 (0)