feat: auto-downgrade output to YAML in non-TTY#737
Merged
Conversation
When stdout is not a TTY (pipes, AI agents, subprocesses), automatically output YAML instead of table with ANSI colors and box-drawing characters. This makes opencli output parseable by downstream tools and AI agents. Behavior: - TTY: table (default, unchanged) - Non-TTY: yaml (auto-detected) - OUTPUT env var: overrides auto-detection (yaml/json/table/etc) - Explicit -f flag: always respected
- fmt='table' from commanderAdapter now correctly triggers non-TTY downgrade - Priority: explicit -f (non-table) > OUTPUT env var > TTY auto-detect - Added test for explicit -f precedence over OUTPUT env var
Use Commander's getOptionValueSource to distinguish explicit -f from default. Explicit -f table in non-TTY keeps table output. Only auto- downgrade when user didn't pass -f. Priority: explicit -f > OUTPUT env var > TTY auto-detect > table default
When user passes -f explicitly, command-level defaultFormat (e.g. gemini/ask defaultFormat:'plain') no longer overrides their choice.
Contributor
|
用 |
just-buer
pushed a commit
to just-buer/opencli
that referenced
this pull request
Apr 8, 2026
* feat: auto-downgrade table output to YAML in non-TTY environments When stdout is not a TTY (pipes, AI agents, subprocesses), automatically output YAML instead of table with ANSI colors and box-drawing characters. This makes opencli output parseable by downstream tools and AI agents. Behavior: - TTY: table (default, unchanged) - Non-TTY: yaml (auto-detected) - OUTPUT env var: overrides auto-detection (yaml/json/table/etc) - Explicit -f flag: always respected * fix: TTY detection now works with commanderAdapter default fmt - fmt='table' from commanderAdapter now correctly triggers non-TTY downgrade - Priority: explicit -f (non-table) > OUTPUT env var > TTY auto-detect - Added test for explicit -f precedence over OUTPUT env var * fix: explicit -f flag now takes precedence over TTY auto-detection Use Commander's getOptionValueSource to distinguish explicit -f from default. Explicit -f table in non-TTY keeps table output. Only auto- downgrade when user didn't pass -f. Priority: explicit -f > OUTPUT env var > TTY auto-detect > table default * fix: explicit -f also skips command defaultFormat override When user passes -f explicitly, command-level defaultFormat (e.g. gemini/ask defaultFormat:'plain') no longer overrides their choice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OUTPUTenv var (yaml/json/table/etc) overrides auto-detection-fflag always takes precedenceoutput.pyTTY detection patternCloses the TTY detection gap identified in #98.
Test plan
npx vitest run src/output.test.ts— 4 tests covering TTY/non-TTY/env-var/explicit-flagopencli zhihu question 123 | catshould output YAMLOUTPUT=json opencli zhihu question 123 | catshould output JSON