Skip to content

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

Open
DiegoDAF wants to merge 4 commits into
dbcli:mainfrom
DiegoDAF:feature/command-option
Open

Add support for -c/--command option to execute commands and exit#1542
DiegoDAF wants to merge 4 commits into
dbcli:mainfrom
DiegoDAF:feature/command-option

Conversation

@DiegoDAF

@DiegoDAF DiegoDAF commented Dec 5, 2025

Copy link
Copy Markdown
Contributor

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

This commit adds support for the -c/--command option to pgcli, similar to
psql's behavior. Users can now execute SQL commands or internal commands
from the command line and exit immediately after execution.

Features:
- Single command execution: pgcli -c "SELECT 1"
- Multiple commands: pgcli -c "SELECT 1" -c "SELECT 2"
- Long form: 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

Tests included for all scenarios.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@DiegoDAF
DiegoDAF marked this pull request as ready for review December 5, 2025 18:22
Comment thread tests/features/steps/command_option.py Fixed
DiegoDAF added 2 commits July 20, 2026 12:23
Address review feedback: the step decoded cmd_output into `output` but never
used it (flagged by CodeQL as an unused local variable). It now asserts on the
\dt column headers, which are rendered whether or not any tables exist, so the
step verifies the special command actually produced its listing.

Also drop the unused `as e` bindings in the TimeoutExpired handlers (ruff F841)
and apply ruff format.
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.

4 participants