From bed468fb766d264d983dbcceea530350ae28e812 Mon Sep 17 00:00:00 2001 From: Alberts Zemzale Date: Thu, 13 Aug 2020 19:15:35 +0300 Subject: [PATCH 1/3] Fix v1 aliases syntax in v2 docs --- docs/v2/manual.md | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/docs/v2/manual.md b/docs/v2/manual.md index c39bfb9954..fea20ec591 100644 --- a/docs/v2/manual.md +++ b/docs/v2/manual.md @@ -425,14 +425,17 @@ import ( func main() { app := &cli.App{ Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "lang, l", - Value: "english", - Usage: "Language for the greeting", - }, - &cli.StringFlag{ - Name: "config, c", - Usage: "Load configuration from `FILE`", + &cli.StringFlag{ + Name: "lang", + Aliases: []string{"l"}, + Value: "english", + Usage: "Language for the greeting", + }, + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "Load configuration from `FILE`", + }, }, }, Commands: []*cli.Command{ @@ -570,7 +573,8 @@ func main() { app.Flags = []cli.Flag { &cli.StringFlag{ - Name: "password, p", + Name: "password", + Aliases: []string{"p"}, Usage: "password for the mysql database", FilePath: "/etc/mysql/password", }, @@ -1309,7 +1313,8 @@ import ( func main() { cli.HelpFlag = &cli.BoolFlag{ - Name: "haaaaalp", Aliases: []string{"halp"}, + Name: "haaaaalp", + Aliases: []string{"halp"}, Usage: "HALP", EnvVars: []string{"SHOW_HALP", "HALPPLZ"}, } @@ -1344,7 +1349,8 @@ import ( func main() { cli.VersionFlag = &cli.BoolFlag{ - Name: "print-version", Aliases: []string{"V"}, + Name: "print-version", + Aliases: []string{"V"}, Usage: "print only the version", } From d2739475928a1a62b41c45506278ddd700517ddc Mon Sep 17 00:00:00 2001 From: Alberts Zemzale Date: Sun, 23 Aug 2020 13:35:59 +0300 Subject: [PATCH 2/3] Remove incorrect bracket in v2 docs example --- docs/v2/manual.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/v2/manual.md b/docs/v2/manual.md index fea20ec591..266abd537a 100644 --- a/docs/v2/manual.md +++ b/docs/v2/manual.md @@ -436,7 +436,6 @@ func main() { Aliases: []string{"c"}, Usage: "Load configuration from `FILE`", }, - }, }, Commands: []*cli.Command{ { From 342ce5d654ae779f664a81a0d3e31b67cf94573d Mon Sep 17 00:00:00 2001 From: Adam Farden Date: Fri, 2 Oct 2020 12:39:44 +0200 Subject: [PATCH 3/3] feature: Add a App.Reader that defaults to os.Stdin Closes: #1190 --- app.go | 7 +++++++ app_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/app.go b/app.go index 59bf68df95..1767c0e5b0 100644 --- a/app.go +++ b/app.go @@ -72,6 +72,8 @@ type App struct { Authors []*Author // Copyright of the binary if any Copyright string + // Reader reader to write input to (useful for tests) + Reader io.Reader // Writer writer to write output to Writer io.Writer // ErrWriter writes error output @@ -117,6 +119,7 @@ func NewApp() *App { BashComplete: DefaultAppComplete, Action: helpCommand.Action, Compiled: compileTime(), + Reader: os.Stdin, Writer: os.Stdout, ErrWriter: os.Stderr, } @@ -160,6 +163,10 @@ func (a *App) Setup() { a.Compiled = compileTime() } + if a.Reader == nil { + a.Reader = os.Stdin + } + if a.Writer == nil { a.Writer = os.Stdout } diff --git a/app_test.go b/app_test.go index 6c95faa612..57c850d1ed 100644 --- a/app_test.go +++ b/app_test.go @@ -433,6 +433,12 @@ func TestApp_Command(t *testing.T) { } } +func TestApp_Setup_defaultsReader(t *testing.T) { + app := &App{} + app.Setup() + expect(t, app.Reader, os.Stdin) +} + func TestApp_Setup_defaultsWriter(t *testing.T) { app := &App{} app.Setup() @@ -850,6 +856,15 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) { } } +func TestApp_DefaultStdin(t *testing.T) { + app := &App{} + app.Setup() + + if app.Reader != os.Stdin { + t.Error("Default input reader not set.") + } +} + func TestApp_DefaultStdout(t *testing.T) { app := &App{} app.Setup() @@ -859,6 +874,29 @@ func TestApp_DefaultStdout(t *testing.T) { } } +func TestApp_SetStdin(t *testing.T) { + buf := make([]byte, 12) + + app := &App{ + Name: "test", + Reader: strings.NewReader("Hello World!"), + Action: func(c *Context) error { + _, err := c.App.Reader.Read(buf) + return err + }, + } + + err := app.Run([]string{"help"}) + + if err != nil { + t.Fatalf("Run error: %s", err) + } + + if string(buf) != "Hello World!" { + t.Error("App did not read input from desired reader.") + } +} + func TestApp_SetStdout(t *testing.T) { var w bytes.Buffer