pgcli icon indicating copy to clipboard operation
pgcli copied to clipboard

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

Open DiegoDAF opened this issue 4 weeks ago • 0 comments

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 avatar Dec 05 '25 18:12 DiegoDAF