-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshellock.py
More file actions
executable file
·1174 lines (958 loc) · 37.2 KB
/
shellock.py
File metadata and controls
executable file
·1174 lines (958 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.13"
# dependencies = []
# ///
"""Shellock - Real-time CLI flag explainer.
Parses command-line flags and retrieves their descriptions from --help or man pages.
Designed to integrate with fish shell for real-time explanations as you type.
Storage model:
- Durable command metadata lives under `~/.config/fish/shellock/data/`.
- One JSON file per command (e.g. `git.json`) with nested subcommand data.
- `SHELLOCK_HOME` environment variable overrides the storage location.
Usage:
shellock parse "jq -r -s file.json" # Parse flags from command
shellock lookup jq -r # Look up description for -r flag of jq
shellock explain "jq -r -s file.json" # Parse and explain all flags
shellock -r tree # Refresh `tree` (command + subcommands)
shellock -r -a # Refresh all known commands
"""
from __future__ import annotations
import argparse
import json
import os
import re
import shlex
import subprocess
import sys
import time
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Final, Protocol
# Constants
PROTOCOL_VERSION: Final[int] = 1
# ANSI colors for terminal output
DIM: Final[str] = "\033[2m"
RESET: Final[str] = "\033[0m"
CYAN: Final[str] = "\033[36m"
# Command-specific help overrides
# Some commands need special help invocations to show all flags
HELP_SOURCE_OVERRIDES: Final[dict[str, object]] = {
"curl": ["curl", "--help", "all"],
"git": lambda subcmd: ["git", "help", subcmd] if subcmd else ["git", "-h"],
"docker": lambda subcmd: ["docker", subcmd, "--help"]
if subcmd
else ["docker", "--help"],
"kubectl": lambda subcmd: ["kubectl", subcmd, "--help"]
if subcmd
else ["kubectl", "--help"],
"npm": lambda subcmd: ["npm", subcmd, "--help"] if subcmd else ["npm", "--help"],
}
def _utc_now_iso() -> str:
return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
def shellock_home() -> Path:
"""Return Shellock's home directory.
Defaults to `~/.config/fish/shellock`, overridable via `SHELLOCK_HOME`.
"""
raw = os.environ.get("SHELLOCK_HOME")
if raw:
return Path(raw).expanduser()
return Path.home() / ".config" / "fish" / "shellock"
def shellock_data_dir() -> Path:
return shellock_home() / "data"
def _command_file_name(command: str) -> str:
"""Use the command token as the name, sanitized for filesystem."""
safe = command.replace("/", "_").replace("\\", "_")
safe = safe.replace(":", "_")
safe = re.sub(r"\s+", "_", safe)
return safe
def command_data_path(*, command: str) -> Path:
return shellock_data_dir() / f"{_command_file_name(command)}.json"
class HelpProvider(Protocol):
def get_help_text(self, *, command: str, subcommand: str | None) -> str | None: ...
def get_man_text(self, *, command: str, subcommand: str | None) -> str | None: ...
class FlagExtractor(Protocol):
def extract_from_help(self, *, help_text: str) -> dict[str, str]: ...
def extract_from_man(self, *, man_text: str) -> dict[str, str]: ...
class SubcommandDiscoverer(Protocol):
def discover(
self, *, command: str, help_text: str | None, man_text: str | None
) -> set[str]: ...
@dataclass(frozen=True, slots=True)
class ScanResult:
flags: dict[str, str]
sources: dict[str, bool]
discovered_subcommands: set[str]
@dataclass(frozen=True, slots=True)
class CommandProtocol:
help_provider: HelpProvider
extractor: FlagExtractor
subcommands: SubcommandDiscoverer
class DefaultHelpProvider:
def get_help_text(self, *, command: str, subcommand: str | None) -> str | None:
return get_help_text(command=command, subcommand=subcommand)
def get_man_text(self, *, command: str, subcommand: str | None) -> str | None:
return get_man_text(command=command, subcommand=subcommand)
class RegexExtractor:
def extract_from_help(self, *, help_text: str) -> dict[str, str]:
return parse_help_output(help_text=help_text)
def extract_from_man(self, *, man_text: str) -> dict[str, str]:
return parse_man_page(man_text=man_text)
class HeuristicSubcommandDiscoverer:
_section_markers = re.compile(
r"(?im)^\s*(commands|available commands|subcommands)\s*:?:\s*$"
)
def discover(
self, *, command: str, help_text: str | None, man_text: str | None
) -> set[str]:
text = "\n".join([t for t in [help_text, man_text] if t])
if not text:
return set()
discovered: set[str] = set()
list_entry = re.compile(r"^\s{2,}([a-z0-9][a-z0-9-]*)\s{2,}.*$", re.IGNORECASE)
in_section = False
for line in text.splitlines():
stripped = line.strip()
if not stripped:
if in_section:
continue
continue
if self._section_markers.match(line):
in_section = True
continue
if in_section and re.match(r"^[A-Z][A-Z0-9 _-]+$", stripped):
in_section = False
continue
match = list_entry.match(line)
if match:
token = match.group(1)
if token.startswith("-"):
continue
if token in {"options", "usage", "help"}:
continue
if token == command:
continue
discovered.add(token)
return discovered
def _scan_flags_and_subcommands(
*, protocol: CommandProtocol, command: str, subcommand: str | None
) -> ScanResult:
help_text = protocol.help_provider.get_help_text(
command=command, subcommand=subcommand
)
man_text = protocol.help_provider.get_man_text(
command=command, subcommand=subcommand
)
flags: dict[str, str] = {}
sources = {"help": False, "man": False}
if help_text:
sources["help"] = True
flags.update(protocol.extractor.extract_from_help(help_text=help_text))
if man_text:
sources["man"] = True
for k, v in protocol.extractor.extract_from_man(man_text=man_text).items():
flags.setdefault(k, v)
discovered_subcommands = protocol.subcommands.discover(
command=command, help_text=help_text, man_text=man_text
)
return ScanResult(
flags=flags, sources=sources, discovered_subcommands=discovered_subcommands
)
def load_command_data(*, command: str) -> dict | None:
path = command_data_path(command=command)
if not path.exists():
return None
try:
return json.loads(path.read_text())
except json.JSONDecodeError:
return None
def save_command_data(*, command: str, data: dict) -> None:
shellock_data_dir().mkdir(parents=True, exist_ok=True)
path = command_data_path(command=command)
# Atomic write: write to temp file then rename
temp_path = path.with_suffix(f".tmp.{os.getpid()}")
try:
temp_path.write_text(json.dumps(data, indent=2, sort_keys=True) + "\n")
temp_path.rename(path) # Atomic on POSIX
except Exception:
temp_path.unlink(missing_ok=True)
raise
def is_scan_in_progress(*, command: str) -> bool:
"""Check if a background scan is already running for this command."""
lock_path = command_data_path(command=command).with_suffix(".scanning")
if not lock_path.exists():
return False
# Stale lock check (5 minute timeout)
try:
age = time.time() - lock_path.stat().st_mtime
return age < 300
except FileNotFoundError:
return False
def spawn_background_scan(*, command: str) -> None:
"""Spawn a detached background process to scan a command."""
script_path = str(Path(__file__).resolve())
subprocess.Popen(
[script_path, "scan", command],
start_new_session=True, # Detach from terminal
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
def ensure_command_scanned(
*, protocol: CommandProtocol, command: str, refresh: bool
) -> dict:
data = load_command_data(command=command)
if data is None or refresh:
scan = _scan_flags_and_subcommands(
protocol=protocol, command=command, subcommand=None
)
subcommands: dict[str, dict] = {}
for sub in sorted(scan.discovered_subcommands):
sub_scan = _scan_flags_and_subcommands(
protocol=protocol, command=command, subcommand=sub
)
subcommands[sub] = {
"generated_at": _utc_now_iso(),
"sources": sub_scan.sources,
"flags": sub_scan.flags,
}
data = {
"protocol_version": PROTOCOL_VERSION,
"command": command,
"generated_at": _utc_now_iso(),
"sources": scan.sources,
"flags": scan.flags,
"subcommands": subcommands,
}
save_command_data(command=command, data=data)
return data
return data
def ensure_subcommand_scanned(
*, protocol: CommandProtocol, command: str, subcommand: str
) -> dict:
data = ensure_command_scanned(protocol=protocol, command=command, refresh=False)
subcommands = data.get("subcommands", {})
if subcommand not in subcommands:
sub_scan = _scan_flags_and_subcommands(
protocol=protocol, command=command, subcommand=subcommand
)
subcommands[subcommand] = {
"generated_at": _utc_now_iso(),
"sources": sub_scan.sources,
"flags": sub_scan.flags,
}
data["subcommands"] = subcommands
save_command_data(command=command, data=data)
return data
def refresh_command(*, protocol: CommandProtocol, command: str) -> None:
existing = load_command_data(command=command) or {}
scan = _scan_flags_and_subcommands(
protocol=protocol, command=command, subcommand=None
)
recorded_subs = set((existing.get("subcommands") or {}).keys())
discovered_subs = set(scan.discovered_subcommands)
all_subs = sorted(recorded_subs | discovered_subs)
subcommands: dict[str, dict] = {}
for sub in all_subs:
sub_scan = _scan_flags_and_subcommands(
protocol=protocol, command=command, subcommand=sub
)
subcommands[sub] = {
"generated_at": _utc_now_iso(),
"sources": sub_scan.sources,
"flags": sub_scan.flags,
}
data = {
"protocol_version": PROTOCOL_VERSION,
"command": command,
"generated_at": _utc_now_iso(),
"sources": scan.sources,
"flags": scan.flags,
"subcommands": subcommands,
}
save_command_data(command=command, data=data)
def refresh_all(*, protocol: CommandProtocol) -> None:
data_dir = shellock_data_dir()
if not data_dir.exists():
return
for path in sorted(data_dir.glob("*.json")):
try:
payload = json.loads(path.read_text())
except json.JSONDecodeError:
continue
cmd = payload.get("command")
if isinstance(cmd, str) and cmd:
refresh_command(protocol=protocol, command=cmd)
@dataclass(frozen=True, slots=True)
class Flag:
"""Represents a command-line flag."""
name: str # e.g., "-r" or "--raw-output"
is_long: bool # True for --flag, False for -f
@dataclass(frozen=True, slots=True)
class FlagDescription:
"""A flag with its description."""
flag: str
description: str
@dataclass(frozen=True, slots=True)
class ParsedCommand:
"""Result of parsing a command line."""
command: str
flags: tuple[Flag, ...]
subcommand: str | None = None
def parse_command_line(*, cmdline: str) -> ParsedCommand | None:
"""
Parse a command line string to extract command and flags.
Handles:
- Short flags: -r, -v, -rf (combined)
- Long flags: --raw-output, --verbose
- Subcommands: git commit -m
"""
try:
tokens = shlex.split(cmdline)
except ValueError:
# Incomplete quotes, try simple split
tokens = cmdline.split()
if not tokens:
return None
command = tokens[0]
flags: list[Flag] = []
subcommand: str | None = None
# Known commands with subcommands
subcommand_commands = {
"git",
"docker",
"kubectl",
"npm",
"cargo",
"go",
"pip",
"uv",
}
i = 1
skip_next = False # Track if next token is a flag value
while i < len(tokens):
token = tokens[i]
if token == "--":
# Option terminator: stop parsing flags, keep prior hints.
break
# Skip this token if it's a flag value (from previous flag)
if skip_next:
skip_next = False
i += 1
continue
# Check for subcommand (first non-flag after command)
# Don't treat key=value pairs as subcommands (likely flag values)
if (
command in subcommand_commands
and subcommand is None
and not token.startswith("-")
and "=" not in token # key=value pairs are likely flag values
):
subcommand = token
i += 1
continue
if token.startswith("--"):
# Long flag: --raw-output or --key=value
flag_name = token.split("=")[0]
flags.append(Flag(name=flag_name, is_long=True))
elif token.startswith("-") and len(token) > 1 and token[1] != "-":
# Could be:
# - Short flag(s): -r or -rf (combined)
# - Single-dash long flag: -name, -type (find, java style)
# Heuristic: 4+ lowercase letters after dash = single-dash long flag
# This handles -name (4), -type (4), -exec (4) but not -avz (3), -rf (2)
flag_part = token[1:]
if len(flag_part) >= 4 and flag_part.isalpha() and flag_part.islower():
# Likely a single-dash long flag (e.g., -name, -type, -exec)
flags.append(Flag(name=token, is_long=False))
else:
# Short flag(s): -r or -rf (combined)
for char in token[1:]:
if char.isalpha():
flags.append(Flag(name=f"-{char}", is_long=False))
else:
# Stop at non-alpha (e.g., -9 for kill)
flags.append(Flag(name=f"-{char}", is_long=False))
break
i += 1
return ParsedCommand(
command=command,
flags=tuple(flags),
subcommand=subcommand,
)
def truncate_description(text: str, max_length: int = 160) -> str:
"""Truncate description at sentence or word boundary."""
if len(text) <= max_length:
return text
limit = max_length - 3 # Room for "..."
truncated = text[:limit]
# Try sentence boundary (. ! ?) followed by space
for punct in (". ", "! ", "? "):
pos = truncated.rfind(punct)
if pos > limit // 2:
return text[: pos + 1]
# Fall back to word boundary
last_space = truncated.rfind(" ")
if last_space > limit // 2:
return truncated[:last_space] + "..."
return truncated + "..."
def wrap_text(text: str, width: int = 80) -> list[str]:
"""Wrap text to multiple lines at word boundaries."""
if len(text) <= width:
return [text]
lines = []
while len(text) > width:
# Find last space before width
wrap_pos = text.rfind(" ", 0, width)
if wrap_pos == -1: # No space found, hard wrap
wrap_pos = width
lines.append(text[:wrap_pos])
text = text[wrap_pos:].lstrip()
if text:
lines.append(text)
return lines
def strip_man_formatting(*, text: str) -> str:
"""
Strip man page formatting (bold/underline via backspace sequences).
Man pages use X\bX for bold and _\bX for underline.
"""
# Remove backspace sequences (X\bX -> X, _\bX -> X)
result = re.sub(r".\x08", "", text)
# Note: We don't dedupe doubled characters (like NNAAMMEE -> NAME)
# because col -b already handles this, and blind deduplication
# can corrupt normal text like "commit" -> "comit"
return result
def parse_help_output(*, help_text: str) -> dict[str, str]:
"""
Parse --help output to extract flag descriptions.
Handles common formats:
- "-r, --raw-output description"
- "-r description"
- " --flag description"
"""
flags: dict[str, str] = {}
# Clean up man page formatting if present
help_text = strip_man_formatting(text=help_text)
# Pattern for flags with descriptions
patterns = [
# llama-cli style: -m, --model FNAME description
r"^\s*(-\w+),\s+(--[\w-]+)\s+\S+\s{2,}(.+)$",
# llama-cli style without arg: -h, --help, --usage description
r"^\s*(-\w+),\s+(--[\w-]+)(?:,\s+--[\w-]+)*\s{2,}(.+)$",
# claude style: --camelCase, --kebab-case <arg> description
r"^\s*(--[\w]+),\s+(--[\w-]+)\s+<[^>]+>\s{2,}(.+)$",
# GNU style: -x, --long-option Description
r"^\s*(-\w)(?:[,\s]+(--[\w-]+))?\s{2,}(.+)$",
# Long then short: --option, -x Description
r"^\s*(--[\w-]+)(?:[,\s]+(-\w))?\s{2,}(.+)$",
# With equals and optional value: --option[=VALUE] Description
r"^\s*(--[\w-]+)(?:\[?=\S*\]?)?\s{2,}(.+)$",
# Long flag with positional arg: --temp N description
r"^\s*(--[\w-]+)\s+\S+\s{2,}(.+)$",
# Short flag with arg: -x arg description
r"^\s*(-[^\s])\s+\S+\s{2,}(.+)$",
# Short only: -x Description
r"^\s*(-[^\s])\s{2,}(.+)$",
# Brackets style: [-x] Description
r"^\s*\[(-[^\s])\]\s{2,}(.+)$",
]
# Special handling for lines with multiple mode flags (e.g., tar -c Create -r Add/Replace)
import re
mode_flags = {}
for line in help_text.split("\n"):
line = line.rstrip()
# Look for mode flag patterns like: -c Create -r Add/Replace
if re.search(r"-\w\s+[A-Z][a-z]+", line):
# Split by flag indicators
parts = re.finditer(r"(-[^\s,\s]+)\s+([A-Z][a-zA-Z]+(?:\s+\w+)*)", line)
for match in parts:
flag, desc = match.groups()
mode_flags[flag] = truncate_description(desc)
# Add mode flags to the results
flags.update(mode_flags)
for line in help_text.split("\n"):
line = line.rstrip()
for pattern in patterns:
match = re.match(pattern, line)
if match:
groups = match.groups()
if len(groups) == 3:
first, second, desc = groups
desc = truncate_description(desc.strip())
if first:
flags[first] = desc
if second:
flags[second] = desc
elif len(groups) == 2:
flag, desc = groups
flags[flag] = truncate_description(desc.strip())
break
return flags
def parse_man_page(*, man_text: str) -> dict[str, str]:
"""
Parse man page to extract flag descriptions.
Man pages have various formats, often in OPTIONS section.
Handles git-style man pages where description is on next line.
"""
flags: dict[str, str] = {}
# Clean up formatting
man_text = strip_man_formatting(text=man_text)
# Try to find OPTIONS section
options_match = re.search(
r"^OPTIONS?\s*$(.+?)(?=^[A-Z]+\s*$|\Z)",
man_text,
re.MULTILINE | re.DOTALL,
)
if options_match:
options_text = options_match.group(1)
else:
options_text = man_text
# Patterns to extract flags from a line
# These patterns handle various man page formats:
# - Git style: exactly 7 spaces, flags on own line, tab-indented description below
# - GNU style: -x, --long-option (on own line, description below)
# - BSD style: -x Description on same line
# - Multiple flags: -R, -r, --recursive
# - Tree style: -L level (arg without angle brackets)
flag_line_patterns = [
# Git style: exactly 7 spaces, -x, --long-option
r"^( )(-[^\s])(?:,\s+(--[\w-]+))?\s*$",
# Git style: -x <arg>, --long-option=<arg> (like -C <path>)
r"^( )(-[^\s])\s+<[^>]+>(?:,\s+(--[\w-]+)(?:=<[^>]+>)?)?\s*$",
# Git style: -x <name>=<value> (like -c <name>=<value>)
r"^( )(-[^\s])\s+<[^>]+=<[^>]+>\s*$",
# Git style: --long-option only (like --bare)
r"^( )(--[\w-]+)(?:\[?=<[^>]+>\]?)?\s*$",
# Git style: --config-env=<name>=<envvar>
r"^( )(--[\w-]+=)<[^>]+=<[^>]+>\s*$",
# GNU: -x <arg>, --long-option=<arg> on own line
r"^(\s{1,12})(-[^\s])(?:\s+<\S+>)?(?:,\s*(--[\w-]+)(?:=<\S+>)?)?\s*$",
# GNU: --long-option=<arg>, -x <arg> on own line
r"^(\s{1,12})(--[\w-]+)(?:=<\S+>)?(?:,\s*(-[^\s])(?:\s+<\S+>)?)?\s*$",
# GNU: --long-option on own line
r"^(\s{1,12})(--[\w-]+)(?:=\S+)?\s*$",
# Multiple short/long flags: -R, -r, --recursive (no description)
r"^(\s{1,12})(-[^\s])(?:,\s*(-[^\s]))?(?:,\s*(--[\w-]+))?\s*$",
# Tree style: -X arg (arg without brackets, on own line, may have no indent)
r"^(\s*)(-[^\s])\s+[a-z]+\s*$",
# Find style: -name pattern (single-dash long flag with arg, 4+ letters)
r"^(\s*)(-[a-z]{4,})\s+[a-z]+\s*$",
# Find style with XY suffix: -newerXY reference
r"^(\s*)(-[a-z]+(?:XY|[A-Z]{2}))\s+\S+\s*$",
# Find style with complex args: -exec utility [argument ...] ;
r"^(\s*)(-[a-z]{4,})\s+\S+",
# BSD style: -x Description (6+ spaces before description)
r"^(\s{1,12})(-[^\s])\s{4,}(\S.*)$",
# Long flag with description on same line
r"^(\s{1,12})(--[\w-]+)\s{4,}(\S.*)$",
# Multiple flags with description on same line (BSD/GNU style)
r"^(\s*)(-\w)(?:,\s*(-\w))*\s{4,}(\S.*)$",
]
current_flags: list[str] = []
current_desc: list[str] = []
current_indent = 0
lines = options_text.split("\n")
i = 0
while i < len(lines):
line = lines[i]
# Check if this line starts a new flag definition
is_flag_line = False
for pattern in flag_line_patterns:
match = re.match(pattern, line)
if match:
# Found a flag line
# Save previous flag(s)
if current_flags and current_desc:
desc = truncate_description(" ".join(current_desc).strip())
for f in current_flags:
flags[f] = desc
# Extract flags and possibly description from this line
# First group is always indent
groups = match.groups()
current_indent = len(groups[0]) if groups[0] else 0
# Separate flags (start with -) from potential description
current_flags = []
inline_desc = None
for g in groups[1:]:
if g:
if g.startswith("-"):
current_flags.append(g)
elif not g.isspace():
# This is likely an inline description (BSD style)
inline_desc = g
if inline_desc:
current_desc = [inline_desc]
else:
current_desc = []
is_flag_line = True
break
if not is_flag_line and current_flags:
# Check if this is a description line
# Description lines are more indented than the flag line
# Common patterns: tab-indented, or many spaces
stripped = line.strip()
# Skip lines that look like new flag definitions
if stripped and not re.match(r"^-\w", stripped):
# Check indentation - description should be more indented than flag
leading_spaces = len(line) - len(line.lstrip())
if line.startswith("\t") or leading_spaces > current_indent + 2:
current_desc.append(stripped)
i += 1
# Save last flag(s)
if current_flags and current_desc:
desc = truncate_description(" ".join(current_desc).strip())
for f in current_flags:
flags[f] = desc
return flags
def get_help_text(*, command: str, subcommand: str | None = None) -> str | None:
"""
Get help output for a command.
Tries multiple approaches: --help, -h, help subcommand.
Uses command-specific overrides for complex commands like curl.
"""
# Check for command-specific help overrides
if command in HELP_SOURCE_OVERRIDES:
help_source = HELP_SOURCE_OVERRIDES[command]
cmd: list[str] | None = None
if isinstance(help_source, list):
cmd = help_source.copy()
elif callable(help_source):
maybe = help_source(subcommand)
if isinstance(maybe, list):
cmd = maybe
if cmd is not None:
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=15,
)
output = result.stdout or result.stderr
if output and len(output) > 50 and "-" in output:
return output
except (subprocess.TimeoutExpired, FileNotFoundError, PermissionError):
pass
# Fall back to standard help options
base_cmd = [command]
if subcommand:
base_cmd.append(subcommand)
# Try different help options
help_variants = [
base_cmd + ["--help"],
base_cmd + ["-h"],
]
# For some commands, help is a subcommand
if not subcommand:
help_variants.append([command, "help"])
for cmd in help_variants:
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=15, # Some commands (llama-cli) need time to initialize
)
output = result.stdout or result.stderr
# Check if we got meaningful output (not just an error)
if output and len(output) > 50 and "-" in output:
return output
except (subprocess.TimeoutExpired, FileNotFoundError, PermissionError):
continue
return None
def get_man_text(*, command: str, subcommand: str | None = None) -> str | None:
"""
Get man page text for a command.
Uses col -b to strip formatting for cleaner output.
"""
import os
# Build man page name (git-commit for git commit, etc.)
man_page = f"{command}-{subcommand}" if subcommand else command
# Try with col -b to strip backspaces
try:
# Use sh -c to pipe man through col
result = subprocess.run(
["sh", "-c", f"man {man_page} 2>/dev/null | col -b"],
capture_output=True,
text=True,
timeout=10,
env={
**os.environ,
"MANPAGER": "cat",
"PAGER": "cat",
"MAN_KEEP_FORMATTING": "",
},
)
if result.returncode == 0 and result.stdout:
return result.stdout
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
# Fallback: try without col
try:
result = subprocess.run(
["man", man_page],
capture_output=True,
text=True,
timeout=10,
env={**os.environ, "MANPAGER": "cat", "PAGER": "cat"},
)
if result.returncode == 0:
return result.stdout
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
return None
def lookup_flag(
*, protocol: CommandProtocol, command: str, flag: str, subcommand: str | None = None
) -> str | None:
"""Look up the description for a specific flag.
- Ensures durable JSON exists under `~/.config/fish/shellock/data/`.
- Falls back from `(command, subcommand)` to `(command)` when needed.
- Supports combined short flags (e.g., `-rf`).
"""
if subcommand is None:
data = ensure_command_scanned(protocol=protocol, command=command, refresh=False)
flags = data.get("flags", {})
else:
data = ensure_subcommand_scanned(
protocol=protocol, command=command, subcommand=subcommand
)
flags = (data.get("subcommands", {}).get(subcommand, {}) or {}).get("flags", {})
if flag in flags:
return flags[flag]
if subcommand is not None:
# fall back to parent command
return lookup_flag(
protocol=protocol, command=command, flag=flag, subcommand=None
)
# Combined short flags: -rf => -r + -f
if re.match(r"^-[a-zA-Z]{2,}$", flag):
chars = flag[1:]
parts: list[str] = []
for char in chars:
single_flag = f"-{char}"
if single_flag in flags:
parts.append(f"{single_flag}: {flags[single_flag]}")
if parts:
return "Combination: " + " | ".join(parts)
return None
def explain_command(
*, protocol: CommandProtocol, cmdline: str
) -> list[FlagDescription]:
"""Parse a command line and return descriptions for all flags.
Non-blocking: if command data doesn't exist, spawns a background scan
and returns a "Learning..." indicator instead of blocking.
"""
parsed = parse_command_line(cmdline=cmdline)
if not parsed:
return []
# Non-blocking: check if data exists without triggering a scan
data = load_command_data(command=parsed.command)
if data is None:
# No data yet - spawn background scan if not already running
if not is_scan_in_progress(command=parsed.command):
spawn_background_scan(command=parsed.command)
# Return "Learning..." indicator
return [
FlagDescription(
flag="__scanning__", description=f"Learning {parsed.command}..."
)
]
# Data exists - proceed with lookup
results: list[FlagDescription] = []
seen: set[str] = set()
for flag in parsed.flags:
if flag.name in seen:
continue
seen.add(flag.name)
desc = lookup_flag_from_data(
data=data,
command=parsed.command,
flag=flag.name,
subcommand=parsed.subcommand,
)
results.append(
FlagDescription(flag=flag.name, description=desc or "Unknown flag")
)
return results
def lookup_flag_from_data(
*, data: dict, command: str, flag: str, subcommand: str | None = None
) -> str | None:
"""Look up flag description from already-loaded data (non-blocking)."""
if subcommand:
subcommand_data = data.get("subcommands", {}).get(subcommand, {})
if subcommand_data:
flags = subcommand_data.get("flags", {})
if flag in flags:
return flags[flag]
# Fall back to main command flags
flags = data.get("flags", {})
if flag in flags:
return flags[flag]
# Combined short flags: -rf => -r + -f
if re.match(r"^-[a-zA-Z]{2,}$", flag):
chars = flag[1:]
parts: list[str] = []
for char in chars:
single_flag = f"-{char}"
if single_flag in flags:
parts.append(f"{single_flag}: {flags[single_flag]}")
if parts:
return "Combination: " + " | ".join(parts)
return None
def format_explanations(
*,
explanations: list[FlagDescription],
use_color: bool = True,
) -> str:
"""Format flag explanations for terminal display."""
if not explanations:
return ""
# Handle scanning indicator specially
if len(explanations) == 1 and explanations[0].flag == "__scanning__":
if use_color: