| 14 | ) |
| 15 | |
| 16 | func ExampleCommand_Run() { |
| 17 | // Declare a command |
| 18 | cmd := &cli.Command{ |
| 19 | Name: "greet", |
| 20 | Flags: []cli.Flag{ |
| 21 | &cli.StringFlag{Name: "name", Value: "pat", Usage: "a name to say"}, |
| 22 | }, |
| 23 | Action: func(_ context.Context, cmd *cli.Command) error { |
| 24 | fmt.Printf("Hello %[1]v\n", cmd.String("name")) |
| 25 | return nil |
| 26 | }, |
| 27 | Authors: []any{ |
| 28 | &mail.Address{Name: "Oliver Allen", Address: "oliver@toyshop.example.com"}, |
| 29 | "gruffalo@soup-world.example.org", |
| 30 | }, |
| 31 | Version: "v0.13.12", |
| 32 | } |
| 33 | |
| 34 | // Simulate the command line arguments |
| 35 | os.Args = []string{"greet", "--name", "Jeremy"} |
| 36 | |
| 37 | if err := cmd.Run(context.Background(), os.Args); err != nil { |
| 38 | // do something with unhandled errors |
| 39 | fmt.Fprintf(os.Stderr, "Unhandled error: %[1]v\n", err) |
| 40 | os.Exit(86) |
| 41 | } |
| 42 | // Output: |
| 43 | // Hello Jeremy |
| 44 | } |
| 45 | |
| 46 | func ExampleCommand_Run_subcommand() { |
| 47 | cmd := &cli.Command{ |