Skip to content

Commit

Permalink
refactor: update code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Nov 13, 2020
1 parent 9c5b66b commit ea84e2a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ vendor
*.bin
*.sum
.vscode
TODO
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ import (
cli "github.com/joseluisq/cline"
)

// App version and build time values passed at compile time
var (
versionNumber string = "devel"
buildTime string
version string = "devel"
buildTime string
)

func main() {
app := cli.New()
app.Name = "enve"
app.Summary = "Run a program in a modified environment using .env files"
app.Version = versionNumber
app.Version = version
app.BuildTime = buildTime
app.Flags = []cli.Flag{
cli.FlagString{
Expand All @@ -53,7 +52,7 @@ func main() {
Name: "verbose",
Summary: "Enable more verbose info",
Value: false,
Aliases: []string{"v"},
Aliases: []string{"V"},
EnvVar: "ENV_VERBOSE",
},
}
Expand All @@ -63,10 +62,10 @@ func main() {
Summary: "Show command information",
Flags: []cli.Flag{
cli.FlagInt{
Name: "version",
Summary: "Enable more verbose command information",
Name: "trace",
Summary: "Enable tracing mode",
Value: 10,
Aliases: []string{"z"},
Aliases: []string{"t"},
},
cli.FlagBool{
Name: "detailed",
Expand All @@ -77,12 +76,17 @@ func main() {
},
Handler: func(ctx *cli.CmdContext) error {
fmt.Printf("Cmd `%s` executed!\n", ctx.Cmd.Name)
i, err := ctx.Flags.Int("version")
fmt.Printf("App Flag `file` opted: `%s`\n", ctx.AppContext.Flags.StringSlice("file"))

i, err := ctx.Flags.Int("trace")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Cmd Flag `version` opted: `%d` (%T)\n", i, i)
fmt.Printf("Cmd Flag `trace` opted: `%d` (%T)\n", i, i)

d := ctx.Flags.String("detailed")
fmt.Printf("Cmd Flag `detailed` opted: `%s` (%T)\n", d, d)
fmt.Printf("Cmd Tail arguments: %#v\n", ctx.TailArgs)
return nil
},
Expand All @@ -92,7 +96,10 @@ func main() {
fmt.Printf("App `%s` executed!\n", ctx.App.Name)
fmt.Printf("App Tail arguments: %#v\n", ctx.TailArgs)
fmt.Printf("App Flag `file` opted: `%s`\n", ctx.Flags.StringSlice("file"))
fmt.Printf("App Flag `verbose` opted: `%s`\n", ctx.Flags.StringSlice("verbose"))

b, _ := ctx.Flags.Bool("verbose")

fmt.Printf("App Flag `verbose` opted: `%v`\n", b)
return nil
}
if err := app.Run(os.Args); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ func main() {
},
Handler: func(ctx *cli.CmdContext) error {
fmt.Printf("Cmd `%s` executed!\n", ctx.Cmd.Name)
i, err := ctx.Flags.Int("version")
fmt.Printf("App Flag `file` opted: `%s`\n", ctx.AppContext.Flags.StringSlice("file"))

i, err := ctx.Flags.Int("trace")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Cmd Flag `version` opted: `%d` (%T)\n", i, i)
fmt.Printf("Cmd Flag `trace` opted: `%d` (%T)\n", i, i)

d := ctx.Flags.String("detailed")
fmt.Printf("Cmd Flag `detailed` opted: `%s` (%T)\n", d, d)
Expand Down

0 comments on commit ea84e2a

Please sign in to comment.