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

Duplicate flag check and Parse order modification request #456

Closed
fcharlie opened this issue Sep 11, 2024 · 1 comment
Closed

Duplicate flag check and Parse order modification request #456

fcharlie opened this issue Sep 11, 2024 · 1 comment

Comments

@fcharlie
Copy link

I use kong to parse command line, but I found some problems while using it, one of them is "duplicate flag".

  1. the presence of the same negatable flag in different subcommands will cause panic.
package main

import "github.com/alecthomas/kong"

var CLI struct {
	Rm struct {
		Force     bool `help:"Force removal."`
		Recursive bool `help:"Recursively remove files."`

		Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
		Fast  bool     `name:"fast" negatable:"" help:"fast check"`
	} `cmd:"" help:"Remove files."`

	Ls struct {
		Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"`
		Fast  bool     `name:"fast" negatable:"" help:"fast check"`
	} `cmd:"" help:"List paths."`
}

func main() {
	ctx := kong.Parse(&CLI)
	switch ctx.Command() {
	case "rm <path>":
	case "ls":
	default:
		panic(ctx.Command())
	}
}
  1. subcommands cannot override command flags:
package main

import "github.com/alecthomas/kong"

var CLI struct {
	Config []string `help:"config value" short:"c"`
	Rm     struct {
		Force     bool `help:"Force removal."`
		Recursive bool `help:"Recursively remove files."`

		Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
	} `cmd:"" help:"Remove files."`

	Branch struct {
		Paths  []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"`
		Create bool     `name:"create" short:"c" help:"Create branch"`
	} `cmd:"" help:"List paths."`
}

func main() {
	ctx := kong.Parse(&CLI)
	switch ctx.Command() {
	case "rm <path>":
	case "ls":
	default:
		panic(ctx.Command())
	}
}

In addition, it would be great if commands like git -c X=value switch -c new-branch could be supported. (First -c set config to command)

@alecthomas
Copy link
Owner

Thanks for the bug report!

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

2 participants