Refactor --functions option, improve help

This commit is contained in:
Simon Willison 2026-07-07 17:59:58 -07:00
commit cf3373e7b7
2 changed files with 20 additions and 20 deletions

View file

@ -138,8 +138,8 @@ See :ref:`cli_query`.
-r, --raw Raw output, first column of first row -r, --raw Raw output, first column of first row
--raw-lines Raw output, first column of each row --raw-lines Raw output, first column of each row
-p, --param <TEXT TEXT>... Named :parameters for SQL query -p, --param <TEXT TEXT>... Named :parameters for SQL query
--functions TEXT Python code or file path defining custom SQL --functions TEXT Python code or a file path defining custom SQL
functions functions; can be used multiple times
--load-extension TEXT Path to SQLite extension, with optional --load-extension TEXT Path to SQLite extension, with optional
:entrypoint :entrypoint
-h, --help Show this message and exit. -h, --help Show this message and exit.
@ -181,8 +181,8 @@ See :ref:`cli_memory`.
sqlite-utils memory animals.csv --schema sqlite-utils memory animals.csv --schema
Options: Options:
--functions TEXT Python code or file path defining custom SQL --functions TEXT Python code or a file path defining custom SQL
functions functions; can be used multiple times
--attach <TEXT FILE>... Additional databases to attach - specify alias and --attach <TEXT FILE>... Additional databases to attach - specify alias and
filepath filepath
--flatten Flatten nested JSON objects, so {"foo": {"bar": --flatten Flatten nested JSON objects, so {"foo": {"bar":
@ -383,7 +383,8 @@ See :ref:`cli_bulk`.
Options: Options:
--batch-size INTEGER Commit every X records --batch-size INTEGER Commit every X records
--functions TEXT Python code or file path defining custom SQL functions --functions TEXT Python code or a file path defining custom SQL
functions; can be used multiple times
--flatten Flatten nested JSON objects, so {"a": {"b": 1}} becomes --flatten Flatten nested JSON objects, so {"a": {"b": 1}} becomes
{"a_b": 1} {"a_b": 1}
--nl Expect newline-delimited JSON --nl Expect newline-delimited JSON

View file

@ -153,6 +153,17 @@ def load_extension_option(fn):
)(fn) )(fn)
def functions_option(fn):
return click.option(
"--functions",
help=(
"Python code or a file path defining custom SQL functions; "
"can be used multiple times"
),
multiple=True,
)(fn)
@click.group( @click.group(
cls=DefaultGroup, cls=DefaultGroup,
default="query", default="query",
@ -1450,11 +1461,7 @@ def upsert(
@click.argument("sql") @click.argument("sql")
@click.argument("file", type=click.File("rb"), required=True) @click.argument("file", type=click.File("rb"), required=True)
@click.option("--batch-size", type=int, default=100, help="Commit every X records") @click.option("--batch-size", type=int, default=100, help="Commit every X records")
@click.option( @functions_option
"--functions",
help="Python code or file path defining custom SQL functions",
multiple=True,
)
@import_options @import_options
@load_extension_option @load_extension_option
def bulk( def bulk(
@ -1860,11 +1867,7 @@ def drop_view(path, view, ignore, load_extension):
type=(str, str), type=(str, str),
help="Named :parameters for SQL query", help="Named :parameters for SQL query",
) )
@click.option( @functions_option
"--functions",
help="Python code or file path defining custom SQL functions",
multiple=True,
)
@load_extension_option @load_extension_option
def query( def query(
path, path,
@ -1937,11 +1940,7 @@ def query(
nargs=-1, nargs=-1,
) )
@click.argument("sql") @click.argument("sql")
@click.option( @functions_option
"--functions",
help="Python code or file path defining custom SQL functions",
multiple=True,
)
@click.option( @click.option(
"--attach", "--attach",
type=(str, click.Path(file_okay=True, dir_okay=False, allow_dash=False)), type=(str, click.Path(file_okay=True, dir_okay=False, allow_dash=False)),