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

@ -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',