From 3d7183307aaa25628f657a0f6dac4d6dcf331359 Mon Sep 17 00:00:00 2001 From: Harrison Date: Sat, 31 Jan 2015 10:04:52 +1100 Subject: [PATCH 1/3] app, help: add support for multiple authors --- app.go | 25 +++++++++++++++++++------ app_test.go | 4 +++- help.go | 6 ++---- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app.go b/app.go index 6422345..8a6c88e 100644 --- a/app.go +++ b/app.go @@ -40,10 +40,8 @@ type App struct { CommandNotFound func(context *Context, command string) // Compilation date Compiled time.Time - // Author - Author string - // Author e-mail - Email string + // Authors + Authors []Author // Writer writer to write output to Writer io.Writer } @@ -67,8 +65,7 @@ func NewApp() *App { BashComplete: DefaultAppComplete, Action: helpCommand.Action, Compiled: compileTime(), - Author: "Author", - Email: "unknown@email", + Authors: []Author{{"Jim", "jim@corporate.com"}, {"Hank", "hank@indiepalace.com"}}, Writer: os.Stdout, } } @@ -273,3 +270,19 @@ func (a *App) appendFlag(flag Flag) { a.Flags = append(a.Flags, flag) } } + +// Author represents someone who has contributed to a cli project. +type Author struct { + Name string // The Authors name + Email string // The Authors email +} + +// String makes Author comply to the Stringer interface, to allow an easy print in the templating process +func (a Author) String() string { + e := "" + if a.Email != "" { + e = " <" + a.Email + "> " + } + + return fmt.Sprintf("%v %v", a.Name, e) +} diff --git a/app_test.go b/app_test.go index 2cbb0e3..be9b0d4 100644 --- a/app_test.go +++ b/app_test.go @@ -6,7 +6,7 @@ import ( "os" "testing" - "github.com/codegangsta/cli" + "github.com/paked/cli" ) func ExampleApp() { @@ -21,6 +21,8 @@ func ExampleApp() { app.Action = func(c *cli.Context) { fmt.Printf("Hello %v\n", c.String("name")) } + app.Authors = []cli.Author{{"Oliver Allen", "oliver@toyshop.com"}} + app.Run(os.Args) // Output: // Hello Jeremy diff --git a/help.go b/help.go index bfb2788..cc4548c 100644 --- a/help.go +++ b/help.go @@ -12,11 +12,9 @@ USAGE: {{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...] VERSION: - {{.Version}}{{if or .Author .Email}} + {{.Version}} -AUTHOR:{{if .Author}} - {{.Author}}{{if .Email}} - <{{.Email}}>{{end}}{{else}} - {{.Email}}{{end}}{{end}} +AUTHOR(S): {{range .Authors}} {{ . }}{{end}} COMMANDS: {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}} From 05ecd63a95fe0acc44a3d6c4f88fd45356d6c782 Mon Sep 17 00:00:00 2001 From: Harrison Date: Sat, 31 Jan 2015 10:05:45 +1100 Subject: [PATCH 2/3] app_test: prepare for PR --- app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app_test.go b/app_test.go index be9b0d4..28dfc45 100644 --- a/app_test.go +++ b/app_test.go @@ -6,7 +6,7 @@ import ( "os" "testing" - "github.com/paked/cli" + "github.com/codegangsta/cli" ) func ExampleApp() { From c6592bb4878bed542931f20d07f941bc065a4b75 Mon Sep 17 00:00:00 2001 From: Harrison Date: Sat, 21 Feb 2015 10:44:00 +1100 Subject: [PATCH 3/3] app, help: add backwards compatibility for Authors --- app.go | 14 ++++++++++++-- app_test.go | 3 ++- help.go | 3 ++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app.go b/app.go index 8a6c88e..2898356 100644 --- a/app.go +++ b/app.go @@ -40,8 +40,12 @@ type App struct { CommandNotFound func(context *Context, command string) // Compilation date Compiled time.Time - // Authors + // List of all authors who contributed Authors []Author + // Name of Author (Note: Use App.Authors, this is deprecated) + Author string + // Email of Author (Note: Use App.Authors, this is deprecated) + Email string // Writer writer to write output to Writer io.Writer } @@ -65,6 +69,8 @@ func NewApp() *App { BashComplete: DefaultAppComplete, Action: helpCommand.Action, Compiled: compileTime(), + Author: "Dr. James", + Email: "who@gmail.com", Authors: []Author{{"Jim", "jim@corporate.com"}, {"Hank", "hank@indiepalace.com"}}, Writer: os.Stdout, } @@ -72,6 +78,10 @@ func NewApp() *App { // Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination func (a *App) Run(arguments []string) error { + if a.Author != "" && a.Author != "" { + a.Authors = append(a.Authors, Author{a.Author, a.Email}) + } + if HelpPrinter == nil { defer func() { HelpPrinter = nil @@ -281,7 +291,7 @@ type Author struct { func (a Author) String() string { e := "" if a.Email != "" { - e = " <" + a.Email + "> " + e = "<" + a.Email + "> " } return fmt.Sprintf("%v %v", a.Name, e) diff --git a/app_test.go b/app_test.go index 28dfc45..be95845 100644 --- a/app_test.go +++ b/app_test.go @@ -21,8 +21,9 @@ func ExampleApp() { app.Action = func(c *cli.Context) { fmt.Printf("Hello %v\n", c.String("name")) } + app.Author = "Harrison" + app.Email = "harrison@lolwut.com" app.Authors = []cli.Author{{"Oliver Allen", "oliver@toyshop.com"}} - app.Run(os.Args) // Output: // Hello Jeremy diff --git a/help.go b/help.go index cc4548c..27d413a 100644 --- a/help.go +++ b/help.go @@ -14,7 +14,8 @@ USAGE: VERSION: {{.Version}} -AUTHOR(S): {{range .Authors}} {{ . }}{{end}} +AUTHOR(S): + {{range .Authors}}{{ . }} {{end}} COMMANDS: {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}