Skip to content

Conversation

@DiegoDAF
Copy link

Description

Like in psql command, where you can use -c/--command to run a single command (SQL or internal) and exit, I added the same behavior to pgcli.

Some screenshots:

image image

Tests:

(pgcli.dev) daf@d:tests$ ## particularmente este
/home/daf/scripts/pgcli/pgcli.dev/.venv/bin/behave features/command_option.feature
USING RUNNER: behave.runner:Runner
package root: /home/daf/scripts/pgcli/pgcli.dev
fixture dir: /home/daf/scripts/pgcli/pgcli.dev/tests/features/fixture_data
Created connection: {}.
Created connection: {}.
Pgbouncer is not available.
reading fixture data: /home/daf/scripts/pgcli/pgcli.dev/tests/features/fixture_data/
--- os.environ changed values: ---
BEHAVE_WARN="moderate"
COLUMNS="100"
COVERAGE_PROCESS_START="/home/daf/scripts/pgcli/pgcli.dev/.coveragerc"
EDITOR="ex"
LINES="100"
PGDATABASE="pgcli_behave_tests_3_10_17"
PGSERVICEFILE="/home/daf/scripts/pgcli/pgcli.dev/tests/features/fixture_data/mock_pg_service.conf"
PROMPT_TOOLKIT_NO_CPR="1"
VISUAL="ex"
XDG_CONFIG_HOME="/tmp/pgcli_home__5nyw4bu"
--------------------
Feature: run the cli with -c/--command option, # features/command_option.feature:1
  execute a single command,
  and exit
  Scenario: run pgcli with -c and a SQL query           # features/command_option.feature:5
    When we run pgcli with -c "SELECT 1 as test_column" # features/steps/command_option.py:9 0.355s
    Then we see the query result                        # features/steps/command_option.py:63 0.000s
    And pgcli exits successfully                        # features/steps/command_option.py:108 0.000s

  Scenario: run pgcli with --command and a SQL query              # features/command_option.feature:10
    When we run pgcli with --command "SELECT 'hello' as greeting" # features/steps/command_option.py:36 0.355s
    Then we see the query result                                  # features/steps/command_option.py:63 0.000s
    And pgcli exits successfully                                  # features/steps/command_option.py:108 0.000s

  Scenario: run pgcli with -c and a special command  # features/command_option.feature:15
    When we run pgcli with -c "\dt"                  # features/steps/command_option.py:9 0.372s
    Then we see the command output                   # features/steps/command_option.py:88 0.000s
    And pgcli exits successfully                     # features/steps/command_option.py:108 0.000s

  Scenario: run pgcli with -c and an invalid query                           # features/command_option.feature:20
    When we run pgcli with -c "SELECT invalid_column FROM nonexistent_table" # features/steps/command_option.py:9 0.363s
    Then we see an error message                                             # features/steps/command_option.py:97 0.000s
    And pgcli exits successfully                                             # features/steps/command_option.py:108 0.000s

  Scenario: run pgcli with -c and multiple statements  # features/command_option.feature:25
    When we run pgcli with -c "SELECT 1; SELECT 2"     # features/steps/command_option.py:9 0.364s
    Then we see both query results                     # features/steps/command_option.py:78 0.000s
    And pgcli exits successfully                       # features/steps/command_option.py:108 0.000s

  Scenario: run pgcli with multiple -c options  # features/command_option.feature:30
    When we run pgcli with multiple -c options  # features/steps/command_option.py:126 0.358s
    Then we see all command outputs             # features/steps/command_option.py:183 0.000s
    And pgcli exits successfully                # features/steps/command_option.py:108 0.000s

  Scenario: run pgcli with mixed -c and --command options  # features/command_option.feature:35
    When we run pgcli with mixed -c and --command          # features/steps/command_option.py:155 0.397s
    Then we see all command outputs                        # features/steps/command_option.py:183 0.000s
    And pgcli exits successfully                           # features/steps/command_option.py:108 0.000s

1 feature passed, 0 failed, 0 skipped
7 scenarios passed, 0 failed, 0 skipped
21 steps passed, 0 failed, 0 skipped
Took 0min 2.565s

pyTests: 2667 passed, 1 xfailed, 1 xpassed in 27.88s

Checklist

  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install).
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

@DiegoDAF DiegoDAF marked this pull request as ready for review November 20, 2025 13:16
diego-feito-stori pushed a commit to DiegoDAF/pgcli.dev that referenced this pull request Nov 27, 2025
@diego-feito-stori diego-feito-stori deleted the addigcparameter1 branch November 27, 2025 18:40
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.

2 participants