Debian Codegraph @

docs/cli-options.md

raw
1 # CLI options: the groups, and how to add one
2
3 Harlequin and `hsql` share their option machinery, and `hsql` additionally
4 sorts every option into a **group**. This document is the reference for that,
5 and the checklist for adding an option.
6
7 ## Where an option comes from
8
9 There are two kinds, and they are declared in different places.
10
11 **An adapter's options** are `AbstractOption` subclasses in the adapter's own
12 `ADAPTER_OPTIONS` list. Declaring one gets all three renderings —
13 `to_click()` for the CLI, `to_widgets()` for the TUI, `to_questionary()` for
14 the config wizard — and both commands attach them dynamically once the first
15 pass has named the adapter. Nothing in core lists them.
16
17 **A command's own options** are `@click.option` decorators: `harlequin`'s in
18 `harlequin/cli.py`, `hsql`'s in `harlequin/hsql/cli.py`. These are the frozen
19 part of each command's surface; an adapter option whose spelling collides with
20 one loses that spelling (`first_pass.attach_adapter_options`).
21
22 Every option that is not `CONN_STR` is also a **profile key**, under the name
23 click gives it (`--read-only` is `read_only`). `config.py` validates a profile
24 against the command's parameters plus the adapter's declared options, so a key
25 neither declares is an error.
26
27 ## The five groups
28
29 `hsql` runs in two roles — a server (`--serve NAME`) and a client
30 (`--session NAME`) — and an option belongs to exactly one group, decided by
31 **when its value is read**. The groups are frozensets at the top of
32 `harlequin/hsql/cli.py`.
33
34 | Group | Read | `--serve` | Served request |
35 | --- | --- | --- | --- |
36 | `CONNECTION_OPTIONS` | once, when the connection opens | accepted | compared against the session's; equal is served, differing exits 2 |
37 | `PER_REQUEST_OPTIONS` | on every invocation | refused, exit 2 | accepted |
38 | `SERVER_OPTIONS` | once, at start-up; only a server has one | accepted | refused, exit 2 |
39 | `ROLE_OPTIONS` | to pick which process runs the invocation | — | — |
40 | `CONFIG_OPTIONS` | to pick the file and profile the rest come from | accepted | accepted |
41
42 Two of these need a word.
43
44 **`CONFIG_OPTIONS` (`-P`, `--config-path`) name where values come from rather
45 than being values**, so which group a profile belongs to is decided by what it
46 holds. A profile of nothing but `format` and `limit` is per-request whichever
47 way it was named; one that names a database is compared like a typed flag.
48
49 **Every adapter option is a connection option.** `connection_option_names()`
50 is the join: `CONNECTION_OPTIONS` plus the adapter's declared names. It is a
51 *positive* test — a key that is neither hsql's nor the adapter's, such as one
52 of `TUI_ONLY_KEYS` or a misspelling in a profile, describes no connection and
53 is not compared.
54
55 ## Adding an option
56
57 1. **Decide whose it is.** If it changes how a database is reached, it is an
58 adapter option — declare it in that adapter's `ADAPTER_OPTIONS` and stop.
59 Everything below is for a core option on `harlequin` or `hsql`.
60
61 2. **Write the `@click.option`.** For `hsql`, keep the help one or two
62 sentences and name the default.
63
64 3. **Put its name in exactly one group** in `harlequin/hsql/cli.py`, by when
65 its value is read:
66 - once, at connect time → `CONNECTION_OPTIONS`
67 - on each invocation, including every mode → `PER_REQUEST_OPTIONS`
68 - once, for a server that is up → `SERVER_OPTIONS`
69
70 `tests/unit_tests/test_hsql_serve.py::test_every_option_is_in_exactly_one_group`
71 fails if you skip this, or if you put it in two.
72
73 4. **If it is a mode** — one that reports rather than running SQL — also add
74 it to `_one_mode()`, so two modes in one invocation are refused. A mode that
75 reports on the installation or the config files goes in the `extra_options`
76 and the `needs_profile` / `needs_adapter` predicates in `build_cli()` too,
77 so the first pass reads no profile for it; a mode that is configured like a
78 run does not (`--catalog`, `--history`).
79
80 5. **If a config file may not set it**, add it to `CLI_ONLY_SESSION_KEYS` or
81 `CLI_ONLY_SSH_KEYS` in `config.py`. That is for options whose value decides
82 *which process runs the invocation*, or which a config file discovered in
83 the working directory should not be able to weaken.
84
85 6. **If the IDE reads it and `hsql` must not**, add it to `TUI_ONLY_KEYS`.
86
87 7. **Regenerate the committed artifacts**, which are pinned by tests:
88
89 ```bash
90 uv run python scripts/write_config_schema.py
91 uv run python scripts/write_cli_reference.py
92 ```
93
94 8. **Add a `CHANGELOG.md` entry** under `[Unreleased]` if the option is
95 user-facing.
96
97 ## Known weakness
98
99 Group membership lives in a name list beside the declaration rather than on
100 it, so the two can drift. The partition test is what catches the drift for a
101 *declared* option, which is why it exists and why it should not be relaxed.
102 It cannot catch a profile key that no command declares; that class is handled
103 instead by `connection_option_names()` being a positive test.
104
105 [#1145](https://github.com/tconbeer/harlequin/issues/1145) tracks moving the
106 group onto the option, so that it cannot be declared without one.