passthrough:"<mode>"` is silently ignored on a cmd:"" tag
Summary
The passthrough tag accepts a mode (all or partial). On a positional argument the mode is honoured. On a command it is parsed and validated, and then discarded: passthrough:"", passthrough:"all" and passthrough:"partial" all behave identically, always as all.
Because the value is validated (an invalid mode is a grammar error) but never consulted, passthrough:"partial" on a command looks like it is doing something and is not. Either it should be honoured, or it should be rejected at grammar-build time.
Reproduction
package main
import (
"fmt"
"github.com/alecthomas/kong"
)
type cmdArgs struct {
Args []string `arg:"" optional:""`
}
type defaultMode struct {
Flag string
Cmd cmdArgs `cmd:"" passthrough:""`
}
type allMode struct {
Flag string
Cmd cmdArgs `cmd:"" passthrough:"all"`
}
type partialMode struct {
Flag string
Cmd cmdArgs `cmd:"" passthrough:"partial"`
}
var argv = []string{"cmd", "--flag", "value", "positional"}
func main() {
fmt.Printf("argv: %q\n\n", argv)
var a defaultMode
mustParse(&a)
fmt.Printf("passthrough:\"\" Flag=%-7q Args=%q\n", a.Flag, a.Cmd.Args)
var b allMode
mustParse(&b)
fmt.Printf("passthrough:\"all\" Flag=%-7q Args=%q\n", b.Flag, b.Cmd.Args)
var c partialMode
mustParse(&c)
fmt.Printf("passthrough:\"partial\" Flag=%-7q Args=%q\n", c.Flag, c.Cmd.Args)
}
func mustParse(grammar any) {
parser, err := kong.New(grammar)
if err != nil {
panic(err)
}
if _, err := parser.Parse(argv); err != nil {
panic(err)
}
}Actual output
argv: ["cmd" "--flag" "value" "positional"]
passthrough:"" Flag="" Args=["--flag" "value" "positional"]
passthrough:"all" Flag="" Args=["--flag" "value" "positional"]
passthrough:"partial" Flag="" Args=["--flag" "value" "positional"]Expected
The documentation describes partial as "will validate flags until the first positional argument is encountered, then pass through all remaining positional arguments". Applied to cmd --flag value positional, that implies --flag value is parsed and only positional is passed through:
passthrough:"partial" Flag="value" Args=["positional"]If command-level partial is not intended to be meaningful, then the expected behaviour is a grammar error from kong.New rather than silent acceptance.
Root cause
The mode is parsed into Tag.PassthroughMode, but there is no corresponding field on Node. model.go has the mode only on
Value:
// model.go:56 (Node)
Passthrough bool // Set to true to stop flag parsing when encountered.
// model.go:274-275 (Value)
Passthrough bool // Deprecated: Use PassthroughMode instead. ...
PassthroughMode PassthroughMode //build.go:344 copies tag.PassthroughMode when building a Value, so positionals keep it. The command branch keys off the deprecated boolean and drops the mode:
// build.go, command branch
if tag.Passthrough {
...
child.Passthrough = true // mode not recorded anywhere
}And the only two places the mode is ever read are positional-only:
if isUnknownFlagError(err) && positional < len(node.Positional) &&
node.Positional[positional].PassthroughMode == PassThroughModeAll {whereas the command path ends parsing unconditionally, with no mode check:
if node.Passthrough {
c.endParsing()
}Related
Setting the mode on the command's single positional instead does not help either:
type cmdArgs struct {
Args []string `arg:"" optional:"" passthrough:"partial"`
}node.Passthrough calls endParsing() on entry to the command node, before any positional-level handling runs, so the positional's mode never gets a chance to apply.
Suggested resolutions
Either:
Add
PassthroughModetoNode, populate it from the tag in the command branch ofbuild.go, and have context consult it, mirroring what the positional path already does. This makespartialusable for command wrappers that want their own flags parsed before the wrapped command's arguments begin.Or, if command-level modes are out of scope,
failFieldin the command branch ofbuild.gowhentag.PassthroughMode == PassThroughModePartial, so the grammar fails loudly instead of silently behaving asall.
Either way it would be worth stating in the tag documentation whether the mode applies to commands, since the current wording describes the positional and the command cases in the same paragraph without saying that the mode is positional-only.
Source: alecthomas/kong