Skip to content

Commit d607e17

Browse files
adding support for multiples -c / --command parameter to pgcli w/tests
1 parent 4062834 commit d607e17

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Features:
99
* Support dsn specific init-command in the config file
1010
* Add suggestion when setting the search_path
1111
* Allow per dsn_alias ssh tunnel selection
12+
* Add support for `single-command` to run a SQL command and exit.
13+
* Command line option `-c` or `--command`.
14+
* You can specify multiple times.
1215

1316
Internal:
1417
---------

pgcli/main.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,14 @@ def _check_ongoing_transaction_and_allow_quitting(self):
911911
def run_cli(self):
912912
logger = self.logger
913913

914-
# Handle single command mode (-c flag) - similar to psql behavior
915-
if hasattr(self, 'single_command') and self.single_command:
914+
# Handle command mode (-c flag) - similar to psql behavior
915+
# Multiple -c options are executed sequentially
916+
if hasattr(self, 'commands') and self.commands:
916917
try:
917-
logger.debug("Running single command: %s", self.single_command)
918-
# Execute the command using the same logic as interactive mode
919-
self.handle_watch_command(self.single_command)
918+
for command in self.commands:
919+
logger.debug("Running command: %s", command)
920+
# Execute the command using the same logic as interactive mode
921+
self.handle_watch_command(command)
920922
except PgCliQuitError:
921923
# Normal exit from quit command
922924
sys.exit(0)
@@ -925,7 +927,7 @@ def run_cli(self):
925927
logger.error("traceback: %r", traceback.format_exc())
926928
click.secho(str(e), err=True, fg="red")
927929
sys.exit(1)
928-
# Exit successfully after executing the command
930+
# Exit successfully after executing all commands
929931
sys.exit(0)
930932

931933
history_file = self.config["main"]["history_file"]
@@ -1295,7 +1297,8 @@ def is_too_tall(self, lines):
12951297
return len(lines) >= (self.prompt_app.output.get_size().rows - 4)
12961298

12971299
def echo_via_pager(self, text, color=None):
1298-
if self.pgspecial.pager_config == PAGER_OFF or self.watch_command:
1300+
# Disable pager for -c/--command mode and \watch command
1301+
if self.pgspecial.pager_config == PAGER_OFF or self.watch_command or (hasattr(self, 'commands') and self.commands):
12991302
click.echo(text, color=color)
13001303
elif self.pgspecial.pager_config == PAGER_LONG_OUTPUT and self.table_format != "csv":
13011304
lines = text.split("\n")
@@ -1446,8 +1449,9 @@ def echo_via_pager(self, text, color=None):
14461449
@click.option(
14471450
"-c",
14481451
"--command",
1449-
default="",
1450-
help="run only single command (SQL or internal) and exit.",
1452+
"commands",
1453+
multiple=True,
1454+
help="run command (SQL or internal) and exit. Multiple -c options are allowed.",
14511455
)
14521456
@click.argument("dbname", default=lambda: None, envvar="PGDATABASE", nargs=1)
14531457
@click.argument("username", default=lambda: None, envvar="PGUSER", nargs=1)
@@ -1477,7 +1481,7 @@ def cli(
14771481
ssh_tunnel: str,
14781482
init_command: str,
14791483
log_file: str,
1480-
command: str,
1484+
commands: tuple,
14811485
):
14821486
if version:
14831487
print("Version:", __version__)
@@ -1538,8 +1542,8 @@ def cli(
15381542
log_file=log_file,
15391543
)
15401544

1541-
# Store single command for -c option
1542-
pgcli.single_command = command if command else None
1545+
# Store commands for -c option (can be multiple)
1546+
pgcli.commands = commands if commands else None
15431547

15441548
# Choose which ever one has a valid value.
15451549
if dbname_opt and dbname:

0 commit comments

Comments
 (0)