|
| 1 | +"""attune-help CLI. |
| 2 | +
|
| 3 | +Minimal user-facing CLI wrapped around :class:`HelpEngine`. |
| 4 | +Exposes the handful of operations most useful from a |
| 5 | +terminal: |
| 6 | +
|
| 7 | + attune-help lookup <topic> |
| 8 | + attune-help list [--type <kind>] [--limit N] |
| 9 | + attune-help search <query> [--limit N] |
| 10 | + attune-help simpler <topic> |
| 11 | +
|
| 12 | +The MCP server (``attune-help-mcp``) is the equivalent |
| 13 | +surface for agent/IDE clients. Both wrap the same public |
| 14 | +``HelpEngine`` API — the CLI exists so terminal users have |
| 15 | +a first-class entry point without standing up an MCP |
| 16 | +client. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import argparse |
| 22 | +import sys |
| 23 | +from collections.abc import Sequence |
| 24 | + |
| 25 | +from attune_help import HelpEngine, __version__ |
| 26 | + |
| 27 | +_RENDERERS = ("plain", "cli", "claude_code", "marketplace", "json") |
| 28 | + |
| 29 | + |
| 30 | +def _build_parser() -> argparse.ArgumentParser: |
| 31 | + """Build the top-level argument parser.""" |
| 32 | + parser = argparse.ArgumentParser( |
| 33 | + prog="attune-help", |
| 34 | + description=("Look up, list, and search progressive-depth " "help templates."), |
| 35 | + ) |
| 36 | + parser.add_argument( |
| 37 | + "--version", |
| 38 | + action="version", |
| 39 | + version=f"attune-help {__version__}", |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "--template-dir", |
| 43 | + help=("Override template directory. Defaults to " "bundled templates."), |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--renderer", |
| 47 | + default="plain", |
| 48 | + choices=_RENDERERS, |
| 49 | + help="Output renderer. Default: plain.", |
| 50 | + ) |
| 51 | + |
| 52 | + sub = parser.add_subparsers(dest="command", required=True) |
| 53 | + |
| 54 | + p_lookup = sub.add_parser( |
| 55 | + "lookup", |
| 56 | + help="Look up a topic with progressive depth.", |
| 57 | + ) |
| 58 | + p_lookup.add_argument("topic") |
| 59 | + |
| 60 | + p_list = sub.add_parser( |
| 61 | + "list", |
| 62 | + help="List available topic slugs.", |
| 63 | + ) |
| 64 | + p_list.add_argument( |
| 65 | + "--type", |
| 66 | + dest="type_filter", |
| 67 | + help=("Filter by template type (e.g. concept, task, " "reference)."), |
| 68 | + ) |
| 69 | + p_list.add_argument( |
| 70 | + "--limit", |
| 71 | + type=int, |
| 72 | + help="Maximum number of topics to show.", |
| 73 | + ) |
| 74 | + |
| 75 | + p_search = sub.add_parser( |
| 76 | + "search", |
| 77 | + help="Fuzzy-search topic slugs.", |
| 78 | + ) |
| 79 | + p_search.add_argument("query") |
| 80 | + p_search.add_argument( |
| 81 | + "--limit", |
| 82 | + type=int, |
| 83 | + default=10, |
| 84 | + help="Maximum results. Default: 10.", |
| 85 | + ) |
| 86 | + |
| 87 | + p_simpler = sub.add_parser( |
| 88 | + "simpler", |
| 89 | + help=("Step the topic's progressive depth back one " "level."), |
| 90 | + ) |
| 91 | + p_simpler.add_argument("topic") |
| 92 | + |
| 93 | + return parser |
| 94 | + |
| 95 | + |
| 96 | +def _engine(args: argparse.Namespace) -> HelpEngine: |
| 97 | + """Build a HelpEngine from parsed CLI args.""" |
| 98 | + return HelpEngine( |
| 99 | + template_dir=args.template_dir, |
| 100 | + renderer=args.renderer, |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def _cmd_lookup(args: argparse.Namespace) -> int: |
| 105 | + engine = _engine(args) |
| 106 | + result = engine.lookup(args.topic) |
| 107 | + if result is None: |
| 108 | + # Miss: show "did you mean" suggestions on stderr and |
| 109 | + # return nonzero so scripts can react. |
| 110 | + suggestions = engine.suggest(args.topic) |
| 111 | + if suggestions: |
| 112 | + joined = ", ".join(suggestions) |
| 113 | + print( |
| 114 | + f"No help for {args.topic!r}. Did you mean: {joined}?", |
| 115 | + file=sys.stderr, |
| 116 | + ) |
| 117 | + else: |
| 118 | + print(f"No help for {args.topic!r}.", file=sys.stderr) |
| 119 | + return 1 |
| 120 | + print(result) |
| 121 | + return 0 |
| 122 | + |
| 123 | + |
| 124 | +def _cmd_list(args: argparse.Namespace) -> int: |
| 125 | + engine = _engine(args) |
| 126 | + topics = engine.list_topics( |
| 127 | + type_filter=args.type_filter, |
| 128 | + limit=args.limit, |
| 129 | + ) |
| 130 | + if not topics: |
| 131 | + print("No topics found.", file=sys.stderr) |
| 132 | + return 1 |
| 133 | + for topic in topics: |
| 134 | + print(topic) |
| 135 | + return 0 |
| 136 | + |
| 137 | + |
| 138 | +def _cmd_search(args: argparse.Namespace) -> int: |
| 139 | + engine = _engine(args) |
| 140 | + hits = engine.search(args.query, limit=args.limit) |
| 141 | + if not hits: |
| 142 | + print(f"No matches for {args.query!r}.", file=sys.stderr) |
| 143 | + return 1 |
| 144 | + for slug, score in hits: |
| 145 | + print(f"{slug}\t{score:.2f}") |
| 146 | + return 0 |
| 147 | + |
| 148 | + |
| 149 | +def _cmd_simpler(args: argparse.Namespace) -> int: |
| 150 | + engine = _engine(args) |
| 151 | + result = engine.simpler(args.topic) |
| 152 | + if result is None: |
| 153 | + print(f"No help for {args.topic!r}.", file=sys.stderr) |
| 154 | + return 1 |
| 155 | + print(result) |
| 156 | + return 0 |
| 157 | + |
| 158 | + |
| 159 | +_DISPATCH = { |
| 160 | + "lookup": _cmd_lookup, |
| 161 | + "list": _cmd_list, |
| 162 | + "search": _cmd_search, |
| 163 | + "simpler": _cmd_simpler, |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +def main(argv: Sequence[str] | None = None) -> int: |
| 168 | + """Entry point for the ``attune-help`` console script.""" |
| 169 | + parser = _build_parser() |
| 170 | + args = parser.parse_args(argv) |
| 171 | + handler = _DISPATCH[args.command] |
| 172 | + try: |
| 173 | + return handler(args) |
| 174 | + except ValueError as e: |
| 175 | + print(f"Error: {e}", file=sys.stderr) |
| 176 | + return 2 |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + sys.exit(main()) |
0 commit comments