Skip to content

Commit

Permalink
docs: clarify code example
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Mar 28, 2021
1 parent af2f02e commit a96c199
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ WIP project under **active** development.
- Optional environment variable names for flags.
- Automatic `--help` (`-h`) flag for global flags and commands.
- Automatic `--version` (`-v`) flag with relevant info like app version, Go version, build datetime and OS/Arch.
- POSIX-compliant flags (partial but [WIP](https://github.com/joseluisq/cline/issues/3))

### Work in progress

- POSIX-compliant flags feature is partial. Please see [issue #3](https://github.com/joseluisq/cline/issues/3)
- Subcommands are not supported yet.

## Usage

Expand All @@ -33,9 +37,12 @@ import (
cli "github.com/joseluisq/cline"
)

// App values passed at compile time for --version flag
// See "Makefile > build"
var (
version string = "devel"
buildTime string
version string = "devel"
buildTime string
buildCommit string
)

func main() {
Expand All @@ -44,6 +51,8 @@ func main() {
app.Summary = "Run a program in a modified environment using .env files"
app.Version = version
app.BuildTime = buildTime
app.BuildCommit = buildCommit
// Global App flags
app.Flags = []cli.Flag{
cli.FlagString{
Name: "file",
Expand All @@ -59,6 +68,7 @@ func main() {
EnvVar: "ENV_VERBOSE",
},
}
// App commands
app.Commands = []cli.Cmd{
{
Name: "info",
Expand All @@ -77,6 +87,7 @@ func main() {
Aliases: []string{"d"},
},
},
// Specific command handler for its flags
Handler: func(ctx *cli.CmdContext) error {
fmt.Printf("Cmd `%s` executed!\n", ctx.Cmd.Name)
fmt.Printf("App Flag `file` opted: `%s`\n", ctx.AppContext.Flags.Any("file"))
Expand All @@ -89,7 +100,6 @@ func main() {
if err != nil {
return err
}

fmt.Printf("Cmd Flag `trace` opted: `%d` (%T)\n", i, i)

detailed, err := ctx.Flags.Bool("detailed")
Expand All @@ -100,14 +110,14 @@ func main() {
if err != nil {
return err
}

fmt.Printf("Cmd Flag `detailed` opted: `%v` (%T)\n", d, d)

fmt.Printf("Cmd Tail arguments: %#v\n", ctx.TailArgs)
return nil
},
},
}
// App handler for flags
app.Handler = func(ctx *cli.AppContext) error {
fmt.Printf("App `%s` executed!\n", ctx.App.Name)
fmt.Printf("App Tail arguments: %#v\n", ctx.TailArgs)
Expand Down

0 comments on commit a96c199

Please sign in to comment.