Skip to content

Commit

Permalink
chore: update example
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Jun 30, 2021
1 parent 15b0721 commit 814d8c9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 57 deletions.
112 changes: 66 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,63 @@ Changes include:
[embedmd]:# (example_test.go /import\ / $)
```go
import (
"context"
"flag"
"fmt"
"log"
"os"

"moul.io/climan"
"context"
"flag"
"fmt"
"log"
"os"

"github.com/peterbourgon/ff/v3"
"moul.io/climan"
)

var opts struct {
debug bool
fooFlag string
}

func Example() {
var opts struct {
Debug bool
}

root := &climan.Command{
Name: "example",
ShortUsage: "example [global flags] <subcommand> [flags] [args...]",
ShortHelp: "example's short help",
LongHelp: "example's longer help.\nwith more details.",
FlagsBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.Debug, "debug", opts.Debug, "debug mode")
},
Exec: func(ctx context.Context, args []string) error {
fmt.Println("args", args)
return nil
},
Subcommands: []*climan.Command{
&climan.Command{
Name: "sub",
},
},
// Options: []climan.Option{climan.WithEnvVarPrefix("EXAMPLE")},
}
if err := root.Parse(os.Args[1:]); err != nil {
log.Fatal(fmt.Errorf("parse error: %w", err))
}

if err := root.Run(context.Background()); err != nil {
log.Fatal(fmt.Errorf("run error: %w", err))
}
root := &climan.Command{
Name: "example",
ShortUsage: "example [global flags] <subcommand> [flags] [args...]",
ShortHelp: "example's short help",
LongHelp: "example's longer help.\nwith more details.",
FlagSetBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.debug, "debug", opts.debug, "debug mode")
},
Exec: doRoot,
Subcommands: []*climan.Command{
&climan.Command{
Name: "foo",
FlagSetBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.debug, "debug", opts.debug, "debug mode")
fs.StringVar(&opts.fooFlag, "flag", opts.fooFlag, "foo's flag")
},
ShortUsage: "foo [flags]",
ShortHelp: "foo things",
Exec: doFoo,
},
},
FFOptions: []ff.Option{ff.WithEnvVarPrefix("EXAMPLE")},
}

if err := root.Parse(os.Args[1:]); err != nil {
log.Fatal(fmt.Errorf("parse error: %w", err))
}

if err := root.Run(context.Background()); err != nil {
log.Fatal(fmt.Errorf("run error: %w", err))
}
}

func doRoot(ctx context.Context, args []string) error {
fmt.Println("args", args)
return nil
}

func doFoo(ctx context.Context, args []string) error {
fmt.Println("flag", opts.fooFlag)
return nil
}
```

Expand All @@ -86,15 +103,18 @@ func Example() {
TYPES
type Command struct {
Name string
Exec func(context.Context, []string) error
FlagsBuilder func(fs *flag.FlagSet)
Subcommands []*Command
ShortUsage string
ShortHelp string
LongHelp string
// Has unexported fields.
Name string
Exec func(context.Context, []string) error
FlagSetBuilder func(fs *flag.FlagSet)
Subcommands []*Command
ShortUsage string
ShortHelp string
LongHelp string
FFOptions []ff.Option
FlagSet *flag.FlagSet
UsageFunc func(c *Command) string
// Has unexported fields.
}
func (c *Command) Parse(args []string) error
Expand Down
39 changes: 28 additions & 11 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,40 @@ import (
"log"
"os"

"github.com/peterbourgon/ff/v3"
"moul.io/climan"
)

func Example() {
var opts struct {
Debug bool
}
var opts struct {
debug bool
fooFlag string
}

func Example() {
root := &climan.Command{
Name: "example",
ShortUsage: "example [global flags] <subcommand> [flags] [args...]",
ShortHelp: "example's short help",
LongHelp: "example's longer help.\nwith more details.",
FlagSetBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.Debug, "debug", opts.Debug, "debug mode")
},
Exec: func(ctx context.Context, args []string) error {
fmt.Println("args", args)
return nil
fs.BoolVar(&opts.debug, "debug", opts.debug, "debug mode")
},
Exec: doRoot,
Subcommands: []*climan.Command{
&climan.Command{
Name: "sub",
Name: "foo",
FlagSetBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.debug, "debug", opts.debug, "debug mode")
fs.StringVar(&opts.fooFlag, "flag", opts.fooFlag, "foo's flag")
},
ShortUsage: "foo [flags]",
ShortHelp: "foo things",
Exec: doFoo,
},
},
// Options: []climan.Option{climan.WithEnvVarPrefix("EXAMPLE")},
FFOptions: []ff.Option{ff.WithEnvVarPrefix("EXAMPLE")},
}

if err := root.Parse(os.Args[1:]); err != nil {
log.Fatal(fmt.Errorf("parse error: %w", err))
}
Expand All @@ -42,3 +49,13 @@ func Example() {
log.Fatal(fmt.Errorf("run error: %w", err))
}
}

func doRoot(ctx context.Context, args []string) error {
fmt.Println("args", args)
return nil
}

func doFoo(ctx context.Context, args []string) error {
fmt.Println("flag", opts.fooFlag)
return nil
}
4 changes: 4 additions & 0 deletions tool/lint/.spelling
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ contributing.md
pre-commit
dev
ffcli
peterbourgon
Apache2
func
subcommands

0 comments on commit 814d8c9

Please sign in to comment.