Skip to content

fix(cli): consume global flags placed before the namespace tool#86

Open
chiplay wants to merge 1 commit into
fix/cli-nil-contextfrom
fix/cli-namespace-global-flags
Open

fix(cli): consume global flags placed before the namespace tool#86
chiplay wants to merge 1 commit into
fix/cli-nil-contextfrom
fix/cli-namespace-global-flags

Conversation

@chiplay
Copy link
Copy Markdown
Collaborator

@chiplay chiplay commented May 30, 2026

Stacked on #85 (fix/cli-nil-context). Both edit namespaceRunE; this PR is based on that branch to avoid a conflict. Review/merge #85 first, then this retargets to main.

Problem

Global flags placed before the namespace name break (P2 papercut, separate from #85's P0):

$ subtext --format=text doc list   → tool "doc---format=text" not found
$ subtext doc list --format=text   → works fine

Affects all globals (--format/--api-key/--region/--endpoint/--config) in the pre-namespace position.

Root cause

The namespace commands use DisableFlagParsing, so cobra hands any leading global flag through to namespaceRunE as args[0]. The dispatcher does toolName := cmd.Use + "-" + args[0], gluing the flag onto the tool name (doc---format=text). extractGlobalFlags only ran later inside runCall — after the bad tool name was already built and dispatched.

Fix

Run extractGlobalFlags at the top of namespaceRunE, before deriving the tool name, so leading globals are consumed first. runCall still calls it on the remainder (a no-op once globals are gone).

Test + verification

  • Added TestNamespaceDispatchGlobalFlagBeforeTool — asserts a leading --format=text is consumed as a global (not glued) and the tool resolves.
  • go test ./... clean.
  • Verified against the production na1 endpoint: subtext --format=text doc list and the space-separated --format text form both resolve correctly; flags-after still work.

🤖 Generated with Claude Code


Note

Low Risk
Small CLI dispatch change with a targeted integration test; no auth, data, or server-side impact.

Overview
Namespace commands (doc, live, etc.) now accept global flags before the tool subcommand (e.g. subtext --format=text doc list), matching the behavior users expect from normal cobra parsing.

Because namespace parents use DisableFlagParsing, leading globals were passed through as the first positional arg and concatenated into a bogus tool name (doc---format=text). namespaceRunE now runs extractGlobalFlags first, so globals are stripped before the tool name is built; runCall still parses any remaining globals afterward.

An integration test asserts tools/call succeeds, --format is applied, and output is rendered in text mode.

Reviewed by Cursor Bugbot for commit d89d704. Bugbot is set up for automated code reviews on this repo. Configure here.

With DisableFlagParsing on the namespace commands, cobra passes any
global flag that appears before the tool subcommand (e.g. "subtext
--format=text live view-list") straight through to namespaceRunE as
args[0]. The dispatcher then glued it onto the tool name
("live---format=text"), which 404s. extractGlobalFlags only ran later
in runCall, too late to rescue the tool name.

Run extractGlobalFlags at the top of namespaceRunE so leading globals
(--format/--api-key/--region/--endpoint/--config) are consumed before
the tool name is derived. runCall still calls it on the remainder, now
a no-op for globals.

Add a regression test for a leading --format=text and verify against
the production endpoint that both "--format=text doc list" and the
space-separated form resolve correctly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@chiplay
Copy link
Copy Markdown
Collaborator Author

chiplay commented May 30, 2026

@jurassix one more small one. Stacks on #85 since that PR introduces an integration test file that this leverages.

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

Successfully merging this pull request may close these issues.

2 participants