Skip to content

feat(x/cmd): publish tagged args on context - #13

Merged
lucasew merged 3 commits into
mainfrom
20260912-cmd-ctx
Sep 12, 2026
Merged

lucasew merged 3 commits into
mainfrom
20260912-cmd-ctx

Conversation

@lewtec-bot

@lewtec-bot lewtec-bot Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Optional ctx tag publishes the parsed value onto a bag App.Run fills. Get[T] reads it. Empty ctx:"" uses long, or the field name.

Value() is what gets stored (string from StringArg, int from Count).

App flags: verbose, profile-dir, help, version. Run binds the bag first and uses Get for help/version.

Get panics with a sentinel. Recover and errors.Is:

Sentinel When
ErrNoValues ctx was never bound (App.Run not used)
ErrNotSet key missing or nil optional skipped
ErrWrongType stored value is not T
type root struct {
    db db.Arg[Q] `long:"database" ctx:"db"`
    serve *serveCmd
}

func (s *serveCmd) Run(ctx context.Context) error {
    conn := cmd.Get[*db.Conn[Q]](ctx, "db")
    _ = cmd.Get[int](ctx, "verbose")
    return conn.Open(ctx, migrations, New)
}

Optional ctx tag puts parsed Value()s in a bag App.Run fills.
Get[T] reads them. Core App flags are tagged.
ErrNoValues, ErrNotSet, ErrWrongType so tests can errors.Is.
Drop the recover helper. testify already compares Error().
Comment thread x/cmd/ctx_test.go
require.NoError(t, err)
ctx := withValues(t.Context())
bind(ctx, reflect.ValueOf(&got).Elem())
assert.PanicsWithError(t, "context key not set: name", func() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stretchr/testify#1353 would definitely make this look better

@lucasew
lucasew merged commit 2456344 into main Sep 12, 2026
1 check passed
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.

1 participant