fix(cli): show usage for a lone 'explain --help/-h' too
Follow-up to #4830, which fixed the same papercut for 'query'. 'explain' is also a free-text command exempt from the generic --help intercept, so 'graphify explain --help' ran a literal node lookup for the string '--help' ("No node matching '--help' found") instead of printing usage. Intercept the exact standalone '--help'/'-h' the same way; a longer target that merely contains '--help' as a substring still runs as a lookup. Adds two regression tests mirroring the query ones. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
97518adbca
commit
a0cea18ddc
2 files changed
+43
-1
No files matched your search
+9
-1
@@ -2138,9 +2138,17 @@ def dispatch_command(cmd: str) -> None:
|
||||
_touch_query_stamp(gp)
|
||||
|
||||
elif cmd == "explain":
|
||||
_explain_usage = 'Usage: graphify explain "<node>" [--all] [--graph path]'
|
||||
if len(sys.argv) < 3:
|
||||
print('Usage: graphify explain "<node>" [--all] [--graph path]', file=sys.stderr)
|
||||
print(_explain_usage, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
# A lone "--help"/"-h" prints usage, mirroring `query` (#4830). explain
|
||||
# is also a free-text command exempt from the generic --help intercept
|
||||
# (see __main__._run_cli), so a standalone help flag would otherwise run
|
||||
# as a literal node query ("No node matching '--help' found").
|
||||
if len(sys.argv) == 3 and sys.argv[2] in ("--help", "-h"):
|
||||
print(_explain_usage)
|
||||
return
|
||||
from graphify.serve import _is_ambiguity_refusal, _resolve_single_node
|
||||
from networkx.readwrite import json_graph
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
import networkx as nx
|
||||
from networkx.readwrite import json_graph
|
||||
|
||||
@@ -154,3 +156,35 @@ def test_query_cli_question_containing_help_text_still_runs(monkeypatch, tmp_pat
|
||||
out = capsys.readouterr().out
|
||||
assert "cluster" in out
|
||||
assert not out.startswith("Usage: graphify query")
|
||||
|
||||
|
||||
def test_explain_cli_lone_help_flag_prints_usage(monkeypatch, tmp_path, capsys):
|
||||
"""`graphify explain --help`/`-h` with nothing else shows usage instead of
|
||||
running a literal node lookup for the text "--help" (#4830 extended): explain
|
||||
is a free-text command with the same standalone-help gap as query."""
|
||||
graph_path = _write_graph(tmp_path)
|
||||
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
|
||||
for flag in ("--help", "-h"):
|
||||
monkeypatch.setattr(mainmod.sys, "argv", ["graphify", "explain", flag, "--graph", str(graph_path)][:3])
|
||||
mainmod.main()
|
||||
out = capsys.readouterr().out
|
||||
assert out.startswith("Usage: graphify explain")
|
||||
assert "No node matching" not in out
|
||||
|
||||
|
||||
def test_explain_cli_node_containing_help_text_still_runs(monkeypatch, tmp_path, capsys):
|
||||
"""A genuine explain target that merely contains "--help" as a substring must
|
||||
still run as a literal lookup, not be swallowed as a help request. The
|
||||
unresolvable label exits not-found (proving it reached the lookup), and never
|
||||
prints usage."""
|
||||
graph_path = _write_graph(tmp_path)
|
||||
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
|
||||
monkeypatch.setattr(
|
||||
mainmod.sys,
|
||||
"argv",
|
||||
["graphify", "explain", "a node named --help here", "--graph", str(graph_path)],
|
||||
)
|
||||
with pytest.raises(SystemExit):
|
||||
mainmod.main()
|
||||
out = capsys.readouterr().out
|
||||
assert not out.startswith("Usage: graphify explain")
|
||||
Reference in new issue
Block a user