alecthomas / kong

Kong is a command-line parser for Go
MIT License
2.15k stars 140 forks source link

Duplicate flag check and Parse order modification request #456

Closed fcharlie closed 2 months ago

fcharlie commented 2 months ago

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 commented 2 months ago

Thanks for the bug report!