Skip to content

Commit 042a325

Browse files
authored
Document about binds with example (#491)
1 parent 8d238c8 commit 042a325

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

README.md

+37-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
- [Command handling](#command-handling)
1414
- [Switch on the command string](#switch-on-the-command-string)
1515
- [Attach a `Run(...) error` method to each command](#attach-a-run-error-method-to-each-command)
16-
- [Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply() and the Bind() option](#hooks-beforereset-beforeresolve-beforeapply-afterapply-and-the-bind-option)
16+
- [Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply()](#hooks-beforereset-beforeresolve-beforeapply-afterapply)
17+
- [The Bind() option](#the-bind-option)
1718
- [Flags](#flags)
1819
- [Commands and sub-commands](#commands-and-sub-commands)
1920
- [Branching positional arguments](#branching-positional-arguments)
@@ -305,7 +306,7 @@ func main() {
305306

306307
```
307308

308-
## Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply() and the Bind() option
309+
## Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply()
309310

310311
If a node in the CLI, or any of its embedded fields, has a `BeforeReset(...) error`, `BeforeResolve
311312
(...) error`, `BeforeApply(...) error` and/or `AfterApply(...) error` method, those
@@ -314,8 +315,6 @@ and after validation/assignment, respectively.
314315

315316
The `--help` flag is implemented with a `BeforeReset` hook.
316317

317-
Arguments to hooks are provided via the `Run(...)` method or `Bind(...)` option. `*Kong`, `*Context` and `*Path` are also bound and finally, hooks can also contribute bindings via `kong.Context.Bind()` and `kong.Context.BindTo()`.
318-
319318
eg.
320319

321320
```go
@@ -341,6 +340,40 @@ func main() {
341340
}
342341
```
343342

343+
## The Bind() option
344+
345+
Arguments to hooks are provided via the `Run(...)` method or `Bind(...)` option. `*Kong`, `*Context`, `*Path` and parent commands are also bound and finally, hooks can also contribute bindings via `kong.Context.Bind()` and `kong.Context.BindTo()`.
346+
347+
eg:
348+
349+
```go
350+
type CLI struct {
351+
Debug bool `help:"Enable debug mode."`
352+
353+
Rm RmCmd `cmd:"" help:"Remove files."`
354+
Ls LsCmd `cmd:"" help:"List paths."`
355+
}
356+
357+
type AuthorName string
358+
359+
// ...
360+
func (l *LsCmd) Run(cli *CLI) error {
361+
// use cli.Debug here !!
362+
return nil
363+
}
364+
365+
func (r *RmCmD) Run(author AuthorName) error{
366+
// use binded author here
367+
return nil
368+
}
369+
370+
func main() {
371+
var cli CLI
372+
373+
ctx := kong.Parse(&cli, Bind(AuthorName("penguin")))
374+
err := ctx.Run()
375+
```
376+
344377
## Flags
345378
346379
Any [mapped](#mapper---customising-how-the-command-line-is-mapped-to-go-values) field in the command structure _not_ tagged with `cmd` or `arg` will be a flag. Flags are optional by default.

0 commit comments

Comments
 (0)