pgcli
pgcli copied to clipboard
Add support for -f/--file option to execute SQL from files
Summary
This PR adds support for the -f/--file option to pgcli, implementing psql-compatible behavior for executing SQL commands from files.
Features
- Single file execution:
pgcli -f file.sql - Multiple files:
pgcli -f file1.sql -f file2.sql - Long form support:
pgcli --file file.sql - Files are executed sequentially
- Pager is automatically disabled in file mode
- Proper error handling and exit codes
Implementation Details
- Added new click option
-f/--filethat accepts multiple file paths - File paths are validated to ensure they exist and are readable
- Modified
run_cli()to check for file mode and execute file contents before entering interactive mode - Updated
echo_via_pager()to disable pager when in file mode - File contents are read and executed using the existing
handle_watch_command()method
Testing
Comprehensive BDD tests included covering:
- Single file with
-fflag - Single file with
--fileflag - Multiple statements in one file
- Multiple
-foptions for different files - Files with special commands
- Error handling for invalid SQL
Compatibility
This implementation follows psql's behavior and maintains backward compatibility with existing functionality.
Made with ❤️ and 🤖 Claude Code