#660·kong

-- protects arguments from flag parsing but not from command matching, which is surprising with a default:"withargs" passthrough command

Author: mitarCreated Sep 18, 2026Updated Sep 18, 2026

Summary

-- stops flag parsing, but the token that follows it is still matched against sibling command names. In a grammar whose default command is a passthrough wrapper for external commands, that means:

  • app -- <program> works for most programs, but silently dispatches a sibling command instead when <program> happens to share its name,
  • and when that program is given arguments, the result is a hard error naming the wrong token.

So --, which is the conventional way to say "stop interpreting, what follows is the payload", protects a following token from being read as a flag but not from being read as a command. The asymmetry is what makes it surprising.

Reproduction

go
package main

import (
	"fmt"
	"strings"

	"github.com/alecthomas/kong"
)

// RunCmd wraps an external command, so everything after it is passed through untouched.
type RunCmd struct {
	Args []string `arg:"" optional:""`
}

// MergeCmd is an ordinary sibling command that takes nothing.
type MergeCmd struct{}

type CLI struct {
	Project string

	Run   RunCmd   `cmd:"" default:"withargs" passthrough:""`
	Merge MergeCmd `cmd:""`
}

func main() {
	for _, argv := range [][]string{
		{"echo", "hi"},
		{"--", "echo", "hi"},
		{"--", "--looks-like-a-flag"},
		{"merge"},
		{"--", "merge"},
		{"--", "merge", "--project", "x"},
	} {
		fmt.Printf("%-34s  %s\n", "app "+strings.Join(argv, " "), parse(argv))
	}
}

func parse(argv []string) string {
	var cli CLI
	parser, err := kong.New(&cli, kong.Exit(func(int) {}))
	if err != nil {
		return "GRAMMAR ERR: " + err.Error()
	}
	ctx, err := parser.Parse(argv)
	if err != nil {
		return "ERR: " + err.Error()
	}
	return fmt.Sprintf("command=%-11q Run.Args=%q", ctx.Command(), cli.Run.Args)
}

Actual output

app echo hi                         command="run <args>" Run.Args=["echo" "hi"]
app -- echo hi                      command="run <args>" Run.Args=["echo" "hi"]
app -- --looks-like-a-flag          command="run <args>" Run.Args=["--looks-like-a-flag"]
app merge                           command="merge"     Run.Args=[]
app -- merge                        command="merge"     Run.Args=[]
app -- merge --project x            ERR: unexpected argument --project

The asymmetry

Row 3 shows -- doing its job: --looks-like-a-flag would otherwise be rejected as an unknown flag, and after -- it reaches the default command as an argument.

Row 5 shows it not doing its job: merge is still matched as a command, so the default command is never reached. There is no way to express "run the program called merge".

Rows 4 and 5 are identical, which is the useful observation: before a sibling command name, -- is currently a no-op. app -- merge means exactly what app merge means, so nobody can be relying on it.

The error is misleading

Row 6 is the practical problem. The user wants to run merge --project x, and writes the conventional escape. What they get is:

ERR: unexpected argument --project

which names the wrong token. --project is a perfectly good flag at the root; the real problem is that merge was consumed as a command and the remaining tokens were then offered to a command that takes no arguments. Nothing in the message points at the actual cause.

The failure is also shape-dependent in a way that is hard to explain: app -- merge succeeds (doing the wrong thing), app -- merge --project x fails, and app -- echo hi works fine, purely because echo does not collide.

Relation to the documented behaviour

The README describes default:"withargs" as allowing the user "to omit the sub-command name on the CLI if its arguments/flags are not ambiguous with the sibling commands or flags", so the ambiguity is acknowledged. What is missing is a way to resolve it. -- is the conventional resolver in every other CLI, and here it resolves the flag half of the ambiguity but not the command half.

Note also that the meaning of -- is not uniform across the parse: at the root it is consumed as a separator, but once the passthrough command has been entered it is preserved as data (app echo -- hi yields ["echo", "--", "hi"], which is correct and desirable for a wrapper).

Proposed resolutions

  1. Preferred. When -- is encountered at a node that has a default:"withargs" command, stop command matching for the remainder and route to the default command. This gives -- a meaning it currently lacks, and because rows 4 and 5 above are identical today, it takes away nothing anyone could depend on.

  2. If changing dispatch is unacceptable, improve the error so it names the cause, for example: merge takes no arguments (it was matched as a command; use "app run merge --project x" to pass it through).

  3. At minimum, document that -- does not prevent command matching, and that a default:"withargs" passthrough command must be named explicitly when its first argument collides with a sibling command.

Options 1 and 2 are not mutually exclusive.