pgcli icon indicating copy to clipboard operation
pgcli copied to clipboard

Add support for -c/--command option to execute commands and exit

Open DiegoDAF opened this issue 4 weeks ago • 0 comments

Summary

This PR adds support for the -c/--command option to pgcli, implementing psql-compatible behavior for executing SQL commands or internal commands from the command line.

This is PR #1530, cleaned.

Features

  • Single command execution: pgcli -c "SELECT 1"
  • Multiple commands: pgcli -c "SELECT 1" -c "SELECT 2"
  • Long form support: pgcli --command "SELECT 1"
  • Mixed usage: pgcli -c "cmd1" --command "cmd2"
  • Commands are executed sequentially
  • Pager is automatically disabled in command mode
  • Proper error handling and exit codes

Implementation Details

  • Added new click option -c/--command that accepts multiple values
  • Modified run_cli() to check for command mode and execute commands before entering interactive mode
  • Updated echo_via_pager() to disable pager when in command mode
  • Commands are executed using the existing handle_watch_command() method

Testing

Comprehensive BDD tests included covering:

  • Single command with -c flag
  • Single command with --command flag
  • Multiple statements in one command
  • Multiple -c options
  • Mixed -c and --command options
  • Error handling

Compatibility

This implementation follows psql's behavior and maintains backward compatibility with existing functionality.

Made with ❤️ and 🤖 Claude Code

DiegoDAF avatar Dec 05 '25 18:12 DiegoDAF