-
Notifications
You must be signed in to change notification settings - Fork 578
Add --tuples-only (-t) option to print rows without extra output #1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Implements the -f/--file command-line option similar to psql's behavior. This allows executing SQL commands from a file and then exiting. Features: - Support for -f and --file (short and long forms) - Multiple files can be specified (-f file1 -f file2) - Can be combined with -c option (-c commands execute first, then -f files) - Pager is disabled in file mode (consistent with -c behavior) - Comprehensive BDD tests added for all scenarios - Version bumped to 4.3.1
When using SSH tunnels, PostgreSQL's .pgpass file was not being used because the connection was using '127.0.0.1' as the hostname instead of the original database hostname. This change preserves the original hostname using PostgreSQL's host/ hostaddr parameters: - host: original database hostname (used for .pgpass lookup and SSL) - hostaddr: 127.0.0.1 (actual connection endpoint via SSH tunnel) Additionally: - Fix connect_uri() to pass DSN parameter for proper .pgpass handling - Add paramiko version constraint to avoid DSSKey compatibility issues - Add SSH tunnel configuration options (ssh_config_file, allow_agent) - Preserve hostaddr parameter when using DSN connections - Add comprehensive test coverage for .pgpass + SSH tunnel scenarios Fixes: SSH tunnel connections now work seamlessly with .pgpass files
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This commit implements a new --tuples-only option similar to psql's -t flag, which prints query results without status messages or timing information. Features: - `-t` or `--tuples-only` without value defaults to csv-noheader format - `-t <format>` allows specifying any table format (e.g., `-t minimal`) - Suppresses "SELECT X" status messages when enabled - Suppresses "Time: X.XXXs" timing output when enabled - Does not affect normal output when option is not used Documentation improvements: - Added missing table formats to pgclirc config file comments: - csv-noheader (CSV without headers) - tsv_noheader (TSV without headers) - csv-tab-noheader (same as tsv_noheader) - minimal (aligned columns without headers or borders) Implementation details: - Added tuples_only parameter to PGCli.__init__() - Added tuples_only field to OutputSettings namedtuple - Modified format_output() to skip status when tuples_only is True - Modified timing output logic to skip when tuples_only is True - All existing tests pass without modifications Example usage: pgcli -t -c "SELECT oid FROM pg_roles WHERE rolname='user';" # Output: 2124219 (nothing else) pgcli -t minimal -c "SELECT oid, rolname FROM pg_roles LIMIT 3;" # Output: aligned columns without headers Made with ❤️ and 🤖 Claude Code
Created comprehensive behavioral tests for the -t/--tuples-only feature: Tests cover: - Basic -t flag usage (default csv-noheader) - Long form --tuples-only - Custom format specification (-t minimal, -t tsv_noheader) - Verification that status messages are suppressed - Verification that timing info is suppressed - Normal mode comparison (without -t) - Multiple rows handling - Special commands with -t Test files: - tests/features/tuples_only.feature: 7 test scenarios - tests/features/steps/tuples_only.py: Step implementations Run tests with: cd tests && behave features/tuples_only.feature Made with ❤️ and 🤖 Claude Code Co-Authored-By: Claude <[email protected]>
Changes: - Removed duplicate step definitions in tuples_only.py that already exist in command_option.py to avoid AmbiguousStep errors - Updated version from 4.3.4 to 4.3.5 All BDD tests passing: 7 scenarios, 32 steps ✓ Made with ❤️ and 🤖 Claude Code Co-Authored-By: Claude <[email protected]>
Test Execution LogsAttaching the complete test execution logs for validation: 1. BDD Tests (behave) - tuples-only featureFile: behave.file.2.log Results:
|
Added to Upcoming (TBD) section: - New tuples-only option feature description - Documentation section for newly documented table formats Made with ❤️ and 🤖 Claude Code Co-Authored-By: Claude <[email protected]>
📋 Complete Test LogsBDD Tests - tuples_only feature (behave.file.2.log)Full test output showing all 7 scenarios passing: Loading config defaults from "./behave.ini" |
Unit Tests (pytest.file.1.log) ============================= test session starts ============================== |
Full BDD Test Suite (behave.file.1.log)Complete output from running the full behave test suite: USING RUNNER: behave.runner:Runner |
| @then("we see the command output") | ||
| def step_see_command_output(context): | ||
| """Verify that the special command output is present.""" | ||
| output = context.cmd_output.decode('utf-8') |
Check notice
Code scanning / CodeQL
Unused local variable Note test
| @then("we see the command was not executed") | ||
| def step_see_command_not_executed(context): | ||
| """Verify that the destructive command was not executed in non-interactive mode.""" | ||
| output = context.cmd_output.decode('utf-8') |
Check notice
Code scanning / CodeQL
Unused local variable Note test
Summary
This PR implements a new
--tuples-only(-t) option for pgcli, similar to psql's-tflag, which prints query results without status messages or timing information. This is particularly useful for scripting and automation where clean, parseable output is needed.Features
Command-line options:
-tor--tuples-onlywithout value → usescsv-noheaderformat by default-t <format>→ allows specifying any table format (e.g.,-t minimal,-t tsv_noheader)Example usage:
Implementation Details
Changes to core files:
pgcli/main.py:
-t/--tuples-onlyCLI option with optional format parametertuples_onlyparameter toPGCli.__init__()OutputSettingsnamedtuple to includetuples_onlyfieldformat_output()to skip status messages whentuples_only=Truetuples_only=Truepgcli/pgclirc:
csv-noheader(CSV without headers)tsv_noheader(TSV without headers)csv-tab-noheader(same as tsv_noheader)minimal(aligned columns without headers or borders)Tests:
tests/features/tuples_only.feature(7 scenarios)tests/features/steps/tuples_only.py(step implementations)Test Results
Unit Tests (pytest):
BDD Tests (behave):
Test scenarios covered:
-tflag with default csv-noheader format--tuples-only(long form)-t minimal(custom format)-t tsv_noheader(tab-separated values)-t(verification)-twith multiple rows-twith special commands (\dt)Compatibility
-t-c,--less-chatty, etc.)Documentation
The feature includes:
pgcli --helpshows the new optionRelated Issues
This addresses the need for cleaner output when using pgcli in scripts and automation pipelines, similar to psql's
-tflag.Made with ❤️ and 🤖 Claude Code