Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document about parent command injection in subcommands #473

Open
Hritik14 opened this issue Nov 27, 2024 · 0 comments
Open

Document about parent command injection in subcommands #473

Hritik14 opened this issue Nov 27, 2024 · 0 comments

Comments

@Hritik14
Copy link

I was looking for ways to access parent flags and got confused with the example on the README which uses a Context to achieve this.
Found an issue which states this more clearly: #121 (comment)

Currently: https://github.com/alecthomas/kong?tab=readme-ov-file#attach-a-run-error-method-to-each-command

type Context struct {
  Debug bool
}
// ...
var cli struct {
  Debug bool `help:"Enable debug mode."`

  Rm RmCmd `cmd:"" help:"Remove files."`
  Ls LsCmd `cmd:"" help:"List paths."`
}

func main() {
  ctx := kong.Parse(&cli)
  err := ctx.Run(&Context{Debug: cli.Debug})

Wouldn't it make more sense to put it like mentioned in the linked issue

type CLI struct {
  Debug bool `help:"Enable debug mode."`

  Rm RmCmd `cmd:"" help:"Remove files."`
  Ls LsCmd `cmd:"" help:"List paths."`
}
// ...
func (l *LsCmd) Run(cli *CLI) error {
// use cli.Debug here !!
  fmt.Println("ls", l.Paths)
  return nil
}

func main() {
  var cli CLI
  ctx := kong.Parse(&cli)
  err := ctx.Run()

this highlights that calling ctx.Run() without arguments makes CLI available inside Run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant