From ac9a5ca6d976ab7323232be479e6da173abba9e7 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Sat, 6 Nov 2021 12:30:58 +0100 Subject: [PATCH] Add flags to example --- example_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/example_test.go b/example_test.go index c94f621..e95fed1 100644 --- a/example_test.go +++ b/example_test.go @@ -2,6 +2,7 @@ package acmd_test import ( "context" + "flag" "fmt" "net/http" "os" @@ -14,7 +15,7 @@ var nopFunc = func(context.Context, []string) error { return nil } func ExampleRunner() { testOut := os.Stdout - testArgs := []string{"now"} + testArgs := []string{"now", "--times", "3"} const format = "15:04:05" now, _ := time.Parse("15:04:05", "10:20:30") @@ -24,7 +25,15 @@ func ExampleRunner() { Name: "now", Description: "prints current time", Do: func(ctx context.Context, args []string) error { - fmt.Printf("now: %s\n", now.Format(format)) + fs := flag.NewFlagSet("some name for help", flag.ContinueOnError) + times := fs.Int("times", 1, "how many times to print time") + if err := fs.Parse(args); err != nil { + return err + } + + for i := 0; i < *times; i++ { + fmt.Printf("now: %s\n", now.Format(format)) + } return nil }, }, @@ -38,7 +47,7 @@ func ExampleRunner() { return err } defer resp.Body.Close() - fmt.Print() + // TODO: parse response, I don't know return nil }, }, @@ -56,7 +65,10 @@ func ExampleRunner() { panic(err) } - // Output: now: 10:20:30 + // Output: + // now: 10:20:30 + // now: 10:20:30 + // now: 10:20:30 } func ExampleHelp() {