Skip to content

Add support for -t/--tuples-only option to print rows without extra output#1545

Open
DiegoDAF wants to merge 3 commits into
dbcli:mainfrom
DiegoDAF:feature/tuples-only-option
Open

Add support for -t/--tuples-only option to print rows without extra output#1545
DiegoDAF wants to merge 3 commits into
dbcli:mainfrom
DiegoDAF:feature/tuples-only-option

Conversation

@DiegoDAF

@DiegoDAF DiegoDAF commented Dec 5, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR implements the -t/--tuples-only option, similar to psql's -t flag, which prints query results without status messages or timing information. This is useful for scripting and piping output to other commands.

Features

  • Default csv-noheader: pgcli -t outputs rows in CSV format without headers
  • Custom format: pgcli -t minimal allows specifying any table format
  • Suppresses extra output:
    • No "SELECT X" status messages
    • No "Time: X.XXXs" timing information
  • Clean for piping: Perfect for shell scripts and automation

Use Cases

# Get just the data, no headers or status
pgcli -t -c "SELECT oid FROM pg_roles WHERE rolname='postgres';"
# Output: 10

# Use with custom format
pgcli -t minimal -c "SELECT name, age FROM users LIMIT 3;"
# Output: aligned columns without headers

# Pipe to other commands
pgcli -t -c "SELECT email FROM users;" | while read email; do
  echo "Sending to $email"
done

Implementation Details

  • Added tuples_only field to OutputSettings namedtuple
  • Added tuples_only parameter to PGCli.__init__()
  • Modified format_output() to skip status when tuples_only=True
  • Modified timing output logic to respect tuples_only flag
  • Click option configured as is_flag=False with flag_value="csv-noheader"
    • Using -t alone defaults to csv-noheader
    • Using -t <format> allows custom formats

Compatibility

  • Does not affect existing functionality when flag is not used
  • Works independently of other flags
  • Compatible with all existing table formats
  • Follows psql behavior

Made with ❤️ and 🤖 Claude Code

@DiegoDAF DiegoDAF marked this pull request as ready for review December 5, 2025 18:59
@j-bennet

Copy link
Copy Markdown
Contributor

Thank you for the PR! I have some problems with it, for two reasons:

  1. It's targeting a non-interactive use-case. Pgcli is a user-interactive tool, and if you want scripting / automation, psql is a better tool for that. We will never be able to keep up with psql in all the features it supports.
  2. It's adding a new option to turn a knob that already exists. We already have \T to change table formats. Exposing -t as the startup flag to set format for just one session may be useful. Perhaps limit the PR to that.

This does not solve your header / status suppression requirement, but I think that should be a separate change. Perhaps pgcli should be aware of noheader output formats and suppress extra output on those. Or we need new flags for --no-timings and --no-status.

@DiegoDAF

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, makes total sense!

I'll rework this PR so that -t [FORMAT] only sets the table format at startup (like a CLI shortcut for \T), defaulting to csv-noheader. No header/status suppression in this PR.

The --no-timings / --no-status flags sound like a good approach for a separate PR — I'll work on that next.

@DiegoDAF DiegoDAF force-pushed the feature/tuples-only-option branch 2 times, most recently from 5ca0788 to 3d86709 Compare February 20, 2026 15:21
DiegoDAF added a commit to DiegoDAF/pgcli.daf that referenced this pull request Feb 23, 2026
@j-bennet

Copy link
Copy Markdown
Contributor

This has conflicts.

@DiegoDAF DiegoDAF force-pushed the feature/tuples-only-option branch from 3d86709 to e1cf78c Compare April 21, 2026 12:40
…ression

Simplified per reviewer feedback (j-bennet):
- `-t` is now a pure boolean flag (no format argument)
- Sets table_format to csv-noheader at startup (like `\T csv-noheader`)
- Does NOT suppress timing or status messages
- Removed tuples_only from OutputSettings namedtuple
- For custom formats, use `\T FORMAT` interactively

Made with ❤️ and 🤖 Claude
@DiegoDAF DiegoDAF force-pushed the feature/tuples-only-option branch from e1cf78c to 8756bfe Compare April 27, 2026 13:23
DiegoDAF added 2 commits May 11, 2026 10:48
Spurious reformat introduced during rebase; ruff 0.15.11 with
preview=true keeps the original split form. Restores upstream layout.
# Conflicts:
#	changelog.rst
@DiegoDAF

Copy link
Copy Markdown
Contributor Author

Reworked per your feedback: -t/--tuples-only is now just a startup shortcut for \T (sets the table format to csv-noheader), with no header/status suppression. Conflicts with main are resolved. Ready for another look, thanks!

@DiegoDAF

DiegoDAF commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

Hey @j-bennet 👋 Friendly bump on this one. I rebased and fixed the ruff format on pgexecute.py, and now that #1596 restored the PG* env vars in CI things should be green again. Whenever you have a spare moment I'd love another look. No rush at all, and thanks a lot for keeping pgcli humming! 🙏

@DiegoDAF

DiegoDAF commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

Hey @j-bennet! 🎉 Congrats on shipping 4.5.0, and thank you so much for merging #1559, really made my week! While you are in release mode, would you have a moment to peek at this one too? It is rebased and green now that the CI env vars are back. Totally understand if you are swamped, no pressure at all. Thanks again for everything you do for pgcli! 🙏

@DiegoDAF

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fe47a48c8f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pgcli/main.py
Comment on lines +240 to +241
if tuples_only:
self.table_format = "csv-noheader"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor tuples-only by suppressing extra output

When -t/--tuples-only is used, this branch only switches the formatter to csv-noheader; the output path still appends command status in format_output (pgcli/main.py:1968-1970) and prints timing whenever enabled (pgcli/main.py:901, with the default config setting timing = True). PostgreSQL documents --tuples-only as turning off column names and row-count footers, so with the default config a query still emits SELECT n and Time: ..., which breaks users relying on this option for rows-only output. Please carry the tuples-only state through to suppress status/timing, or avoid exposing it as psql-style tuples-only.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @codex, fair point on the psql semantics. This is intentional for this PR: -t was deliberately narrowed during review to be a startup shortcut for the output format only (like a CLI \T), not psql's full tuples-only. Suppressing the status footer and timing is kept to separate, composable flags (--no-timings / --no-status) rather than bundled into -t, so each concern stays granular. So the scope here is "format shortcut", and rows-only-with-no-footers is achieved by combining flags. Open to revisiting the naming if -t/--tuples-only reads as too psql-flavored for that scope.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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