--functions can take filenames, can be used multiple times (#681)

Closes #659

The --functions option now accepts:
- File paths ending in .py (e.g., --functions my_funcs.py)
- Multiple invocations (e.g., --functions foo.py --functions 'def bar(): ...')
- Inline Python code (existing behavior)

Implementation follows the same pattern as llm's --functions flag
(simonw/llm@a880c123).

Changes:
- Added multiple=True to --functions Click option in query, bulk, and memory commands
- Modified _register_functions() to detect and read .py files
- Updated _maybe_register_functions() to iterate over multiple function sources
- Removed unused bytes/bytearray handling
- Added comprehensive tests for file paths and multiple invocations
- Updated documentation with examples

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

Co-Authored-By: Claude <noreply@anthropic.com>

* Shorter help for --functions

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Simon Willison 2025-11-23 21:46:51 -08:00 committed by GitHub
commit 0bbc68089c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 155 additions and 12 deletions

View file

@ -45,6 +45,32 @@ def test_cli_bulk(test_db_and_path):
] == list(db["example"].rows)
def test_cli_bulk_multiple_functions(test_db_and_path):
db, db_path = test_db_and_path
result = CliRunner().invoke(
cli.cli,
[
"bulk",
db_path,
"insert into example (id, name) values (:id, myupper(mylower(:name)))",
"-",
"--nl",
"--functions",
"myupper = lambda s: s.upper()",
"--functions",
"mylower = lambda s: s.lower()",
],
input='{"id": 3, "name": "ThReE"}\n{"id": 4, "name": "FoUr"}\n',
)
assert result.exit_code == 0, result.output
assert [
{"id": 1, "name": "One"},
{"id": 2, "name": "Two"},
{"id": 3, "name": "THREE"},
{"id": 4, "name": "FOUR"},
] == list(db["example"].rows)
def test_cli_bulk_batch_size(test_db_and_path):
db, db_path = test_db_and_path
proc = subprocess.Popen(