feat(cli): accept both bare flags and legacy yes/no for --if-add-* args

Resolves PR #272 review #10/P5. The --if-add-node-id / -node-summary /
-doc-description / -node-text args were store_true, which rejected the
documented yes/no values and left default-on options impossible to disable
from the CLI. They now use nargs='?' + const=True + a yes/no-coercing type:

  --if-add-node-id        -> on
  --if-add-node-id no     -> off   (legacy form still works)
  (omitted)               -> use the IndexConfig default

README updated to the flag usage (noting the legacy `no` off-switch), and
--if-add-node-text is now documented too.

Decisions from the review:
- #7 (api_key semantics): verified FALSE POSITIVE — 0.2.x is a cloud SDK whose
  api_key is a PageIndex cloud key (cloud_api.LegacyCloudAPI + docs.pageindex.ai/sdk),
  matching the new SDK. No change.
- #11 (if_add_doc_description default True): kept intentionally (open mode).

Claude-Session: https://claude.ai/code/session_01Kx5DgKbhK1N8autqXH8SmS
This commit is contained in:
mountain 2026-07-08 19:11:52 +08:00
parent cf7f5ce9bf
commit 89132cb94c
3 changed files with 36 additions and 11 deletions

View file

@ -238,10 +238,13 @@ You can customize the processing with additional optional arguments:
--toc-check-pages Pages to check for table of contents (default: 20)
--max-pages-per-node Max pages per node (default: 10)
--max-tokens-per-node Max tokens per node (default: 20000)
--if-add-node-id Add node ID (yes/no, default: yes)
--if-add-node-summary Add node summary (yes/no, default: yes)
--if-add-doc-description Add doc description (yes/no, default: yes)
--if-add-node-id Add node IDs (on by default; disable with: --if-add-node-id no)
--if-add-node-summary Add node summaries (on by default; disable with: --if-add-node-summary no)
--if-add-doc-description Add a document description (on by default; disable with: --if-add-doc-description no)
--if-add-node-text Add raw text to nodes (off by default; enable with: --if-add-node-text)
```
These flags take no value by default (a bare `--if-add-node-id` turns it on); the
legacy `--if-add-node-id no` form still works for turning an option off.
</details>
<details>

View file

@ -5,6 +5,16 @@ from pageindex.index.page_index import *
from pageindex.index.page_index_md import md_to_tree
from pageindex.config import IndexConfig
def _cli_bool(value):
"""Parse a CLI boolean flag value.
A bare ``--flag`` (no value) resolves to True via ``const``; an explicit
value keeps the legacy yes/no style working, so ``--flag no`` turns it off.
"""
return str(value).strip().lower() in ("yes", "true", "1", "y", "on")
if __name__ == "__main__":
# Set up argument parser
parser = argparse.ArgumentParser(description='Process PDF or Markdown document and generate structure')
@ -20,14 +30,16 @@ if __name__ == "__main__":
parser.add_argument('--max-tokens-per-node', type=int, default=None,
help='Maximum number of tokens per node (PDF only)')
parser.add_argument('--if-add-node-id', action='store_true', default=None,
help='Add node id to the node')
parser.add_argument('--if-add-node-summary', action='store_true', default=None,
help='Add summary to the node')
parser.add_argument('--if-add-doc-description', action='store_true', default=None,
help='Add doc description to the doc')
parser.add_argument('--if-add-node-text', action='store_true', default=None,
help='Add text to the node')
# Bare flag (e.g. --if-add-node-id) turns the option on; an explicit value
# keeps the legacy yes/no style, so --if-add-node-id no turns it off.
parser.add_argument('--if-add-node-id', nargs='?', const=True, type=_cli_bool, default=None,
help='Add node IDs (on by default). Bare flag or yes/no, e.g. --if-add-node-id no')
parser.add_argument('--if-add-node-summary', nargs='?', const=True, type=_cli_bool, default=None,
help='Add node summaries (on by default). Bare flag or yes/no')
parser.add_argument('--if-add-doc-description', nargs='?', const=True, type=_cli_bool, default=None,
help='Add a document description (on by default). Bare flag or yes/no')
parser.add_argument('--if-add-node-text', nargs='?', const=True, type=_cli_bool, default=None,
help='Add raw text to nodes (off by default). Bare flag or yes/no')
# Markdown specific arguments
parser.add_argument('--if-thinning', type=str, default='no',

View file

@ -128,3 +128,13 @@ def test_cloud_query_empty_collection_raises(monkeypatch):
monkeypatch.setattr(backend, "_get_all_doc_ids", lambda col: [])
with pytest.raises(ValueError, match="no documents"):
backend.query("empty", "q") # doc_ids=None -> resolves to []
# ── #10: CLI bool flags still parse legacy yes/no (a bare 'no' must be False) ──
def test_cli_bool_coerces_legacy_yes_no():
import run_pageindex
assert run_pageindex._cli_bool("no") is False # legacy off-switch
assert run_pageindex._cli_bool("yes") is True
assert run_pageindex._cli_bool("false") is False
assert run_pageindex._cli_bool(True) is True # bare flag -> const=True