Skip to content

feat: add --stdin-filename flag to auto-detect the format of STDIN input#2784

Open
hsdfat wants to merge 1 commit into
mikefarah:masterfrom
hsdfat:feat/stdin-filename
Open

feat: add --stdin-filename flag to auto-detect the format of STDIN input#2784
hsdfat wants to merge 1 commit into
mikefarah:masterfrom
hsdfat:feat/stdin-filename

Conversation

@hsdfat

@hsdfat hsdfat commented Jul 20, 2026

Copy link
Copy Markdown

Disclosure: this pull request — code, tests and description — was written by Claude Code, an AI agent acting on the user's behalf, not by the user personally. Raised in line with this repository's AGENTS.md disclosure requirement.

Fixes #1876.

What

Adds a --stdin-filename flag. When input comes from STDIN, the given name is used for automatic input/output format detection, exactly as if the data had been read from that file:

$ echo '{ "mike": { "things": "cool" } }' | yq --stdin-filename thing.json
{
  "mike": {
    "things": "cool"
  }
}

The name is only ever passed to filepath.Ext — the file is never opened and does not need to exist, which is what makes this useful for editor and formatter integrations. This follows the naming suggested in the issue (--stdin-filename, as used by ruff) rather than dprint's --stdin.

Why

Per the issue: an editor integration that wants yq to format a buffer currently has to hardcode yq's list of formats and pass the right -p itself. Handing yq the buffer's filename instead lets yq's own detection do the work, so integrations stay correct as yq gains formats.

How

The production change is three lines in configureInputFormat:

formatFilename := inputFilename
if inputFilename == "-" && stdinFilename != "" {
        formatFilename = stdinFilename
}

formatFilename feeds format detection; inputFilename is left untouched so the existing inputFilename != "-" backwards-compatibility check still behaves correctly. Everything else is flag registration, tests and the regenerated README flag table. Nothing in pkg/yqlib is touched.

Behaviour is unchanged when the flag is absent, an explicit -p/--input-format still wins, and the flag is ignored when a real file argument is supplied.

Tests

  • cmd/utils_test.go — 11 new cases in the existing TestConfigureInputFormat/TestConfigureFormats tables: stdin with and without the flag, known/unknown/absent extension, explicit-format-wins, and ignored-when-a-file-is-given.
  • acceptance_tests/inputs-format-auto.sh — 5 e2e cases across yq and yq ea, covering JSON and XML stdin, unknown extensions, the explicit - argument form, and the -p + --stdin-filename combination.

./scripts/format.sh, ./scripts/check.sh, ./scripts/spelling.sh, go test ./cmd/... and scripts/acceptance.sh all pass.

Two failures exist on clean master and are unrelated to this change (confirmed by re-running there): TestTomlScenarios after the go-toml v2.4.2 bump in e2f1d5c changed an error string, and a gci lint hit in pkg/yqlib/encoder_toml.go.

One question on scope

I kept this to format detection only, since that is what the issue asks for — it does not touch yqlib.SetFilenameAlias(), so the filename operator and bad file '-' errors still report - for STDIN.

Wiring it through would make those report the given name too, which is arguably the least surprising behaviour, but it goes beyond the request and widens the diff into pkg/yqlib. Happy to add it in this PR or a follow-up if you'd prefer that — just say which.

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.

RFE: ability to specify input filename for stdin

1 participant