MCPcopy Create free account
hub / github.com/idank/explainshell / detect_nested_cmd

Function detect_nested_cmd

explainshell/roff_utils.py:94–121  ·  view source on GitHub ↗

Detect whether a man page's positional args start a nested command. Checks the SYNOPSIS section for a positional argument named 'command'. Excludes angle-bracket patterns like (git subcommand style).

(gz_path: str)

Source from the content-addressed store, hash-verified

92
93
94def detect_nested_cmd(gz_path: str) -> bool:
95 """Detect whether a man page's positional args start a nested command.
96
97 Checks the SYNOPSIS section for a positional argument named 'command'.
98 Excludes angle-bracket patterns like <command> (git subcommand style).
99 """
100 try:
101 with gzip.open(gz_path, "rt", errors="replace") as f:
102 lines = f.readlines()
103 except Exception as e:
104 logger.warning("Failed to read %s for nested_cmd detection: %s", gz_path, e)
105 return False
106
107 synopsis_lines = _extract_section(lines, "SYNOPSIS")
108 for line in synopsis_lines:
109 cleaned = _clean_roff(line)
110 if not _COMMAND_WORD.search(cleaned):
111 continue
112 # Exclude <command> (git-style subcommand pattern)
113 if _COMMAND_ANGLE.search(cleaned):
114 continue
115 # Strip option-name occurrences (e.g. --rsh-command) and recheck
116 stripped = _COMMAND_IN_OPT.sub("", cleaned)
117 if _COMMAND_WORD.search(stripped):
118 logger.debug("nested_cmd: found 'command' in SYNOPSIS of %s", gz_path)
119 return True
120
121 return False

Callers

nothing calls this directly

Calls 2

_extract_sectionFunction · 0.85
_clean_roffFunction · 0.85

Tested by

no test coverage detected