cli: add NewPassthrough builder for flag.FlagSet commands #12

Closed
opened 2026-02-22 19:17:27 +00:00 by Virgil · 1 comment
Member

Problem

Commands migrating incrementally from flag.FlagSet to cobra need DisableFlagParsing = true so cobra doesn't reject unknown flags. LEM had to write a local helper:

func passthrough(use, short string, fn func([]string)) *cli.Command {
    cmd := cli.NewRun(use, short, "", func(_ *cli.Command, args []string) {
        fn(args)
    })
    cmd.DisableFlagParsing = true
    return cmd
}

Every domain repo doing incremental migration will need the same pattern.

Proposal

Add cli.NewPassthrough to pkg/cli/command.go:

// NewPassthrough creates a command that passes all args (including flags)
// to the given function. Used for commands that do their own flag parsing.
func NewPassthrough(use, short string, fn func([]string)) *Command {
    cmd := NewRun(use, short, "", func(_ *Command, args []string) {
        fn(args)
    })
    cmd.DisableFlagParsing = true
    return cmd
}

Context

LEM has 24 commands using this pattern (Phase 1 passthrough). go-ml will need it too when migrating from init() + cli.RegisterCommands() to WithCommands.

Acceptance

  • cli.NewPassthrough(use, short, fn) exists in pkg/cli/command.go
  • DisableFlagParsing = true is set
  • Documented with example in godoc
  • Test coverage
## Problem Commands migrating incrementally from `flag.FlagSet` to cobra need `DisableFlagParsing = true` so cobra doesn't reject unknown flags. LEM had to write a local helper: ```go func passthrough(use, short string, fn func([]string)) *cli.Command { cmd := cli.NewRun(use, short, "", func(_ *cli.Command, args []string) { fn(args) }) cmd.DisableFlagParsing = true return cmd } ``` Every domain repo doing incremental migration will need the same pattern. ## Proposal Add `cli.NewPassthrough` to `pkg/cli/command.go`: ```go // NewPassthrough creates a command that passes all args (including flags) // to the given function. Used for commands that do their own flag parsing. func NewPassthrough(use, short string, fn func([]string)) *Command { cmd := NewRun(use, short, "", func(_ *Command, args []string) { fn(args) }) cmd.DisableFlagParsing = true return cmd } ``` ## Context LEM has 24 commands using this pattern (Phase 1 passthrough). go-ml will need it too when migrating from `init() + cli.RegisterCommands()` to `WithCommands`. ## Acceptance - [ ] `cli.NewPassthrough(use, short, fn)` exists in `pkg/cli/command.go` - [ ] `DisableFlagParsing = true` is set - [ ] Documented with example in godoc - [ ] Test coverage
Author
Member

Completed by Charon in core/cli (e360115). CLI package now lives at forge.lthn.ai/core/cli/pkg/cli, not core/go/pkg/cli.

Completed by Charon in `core/cli` (e360115). CLI package now lives at `forge.lthn.ai/core/cli/pkg/cli`, not `core/go/pkg/cli`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: core/go#12
No description provided.