Skip to content

Commit

Permalink
Update Example in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Nov 17, 2024
1 parent fbf5c72 commit 7d00757
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,44 @@ features which are otherwise not used throughout [`urfave/cli/v3`].
### Example

```go
configFiles := []string{
filepath.Join(testdataDir, "config.yaml"),
filepath.Join(testdataDir, "alt-config.yaml"),
}

app := &cli.Command{
Name: "greet",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Sources: altsrc.YAML("greet.name", configFiles...),
configFiles := []string{
filepath.Join(testdataDir, "config.yaml"),
filepath.Join(testdataDir, "alt-config.yaml"),
}

app := &cli.Command{
Name: "greet",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Sources: yaml.YAML("greet.name", configFiles...),
},
&cli.IntFlag{
Name: "enthusiasm",
Aliases: []string{"!"},
Sources: yaml.YAML("greet.enthusiasm", configFiles...),
},
},
&cli.IntFlag{
Name: "enthusiasm",
Aliases: []string{"!"},
Sources: altsrc.YAML("greet.enthusiasm", configFiles...),
},
},
Action: func(cCtx *cli.Context) error {
punct := ""
if cCtx.Int("enthusiasm") > 9000 {
punct = "!"
}
Action: func(ctx context.Context, cmd *cli.Command) error {
punct := ""
if cmd.Int("enthusiasm") > 9000 {
punct = "!"
}

fmt.Fprintf(os.Stdout, "Hello, %[1]v%[2]v\n", cmd.String("name"), punct)

fmt.Fprintf(os.Stdout, "Hello, %[1]v%[2]v\n", cCtx.String("name"), punct)
return nil
},
}

return nil
},
}
// Simulating os.Args
os.Args = []string{"greet"}

// Simulating os.Args
os.Args = []string{"greet"}
if err := app.Run(context.Background(), os.Args); err != nil {
fmt.Fprintf(os.Stdout, "OH NO: %[1]v\n", err)
}

if err := app.Run(context.Background(), os.Args); err != nil {
fmt.Fprintf(os.Stdout, "OH NO: %[1]v\n", err)
}
// Output:
// Hello, Berry!
```

0 comments on commit 7d00757

Please sign in to comment.