Skip to content

Commit 18b1a94

Browse files
committed
add -v flag
1 parent 0a3babd commit 18b1a94

File tree

5 files changed

+1823
-20
lines changed

5 files changed

+1823
-20
lines changed

CLI_Commands.md

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33

44
Here is the **complete list** of all CLI commands available in `CodeGraphContext`, categorized by workflow scenario.
55

6+
## Global Flags
7+
8+
These flags can be used with any command:
9+
10+
| Flag | Aliases | Description |
11+
| :--- | :--- | :--- |
12+
| **`--visual`** | `--viz`, `-V` | Shows results as an interactive graph visualization in your browser. Works with analyze, find, and query commands. |
13+
| **`--database`** | `-d` | Temporarily override the database backend (`falkordb` or `neo4j`) for any command. |
14+
| **`--version`** | `-v` | Show version and exit. |
15+
| **`--help`** | `-h` | Show help and exit. |
16+
17+
> **Note:** The visual flag uses uppercase `-V` to avoid conflict with `-v` which is reserved for `--version`.
18+
619
## 1. Project Management
720
Use these commands to manage the repositories in your code graph.
821

@@ -29,22 +42,23 @@ Understand the structure, quality, and relationships of your code.
2942

3043
| Command | Arguments | Description |
3144
| :--- | :--- | :--- |
32-
| **`cgc analyze calls`** | `<func_name>` <br> `--file` | Shows **outgoing** calls: what functions does this function call? |
33-
| **`cgc analyze callers`** | `<func_name>` <br> `--file` | Shows **incoming** calls: who calls this function? |
34-
| **`cgc analyze chain`** | `<start> <end>` <br> `--depth` | Finds the call path between two functions. Default depth is 5. |
35-
| **`cgc analyze deps`** | `<module>` <br> `--no-external` | Inspects dependencies (imports and importers) for a module. |
36-
| **`cgc analyze tree`** | `<class_name>` <br> `--file` | Visualizes the Class Inheritance hierarchy for a given class. |
45+
| **`cgc analyze calls`** | `<func_name>` <br> `--file` <br> `--visual` | Shows **outgoing** calls: what functions does this function call? Use `--visual` or `-V` for graph view. |
46+
| **`cgc analyze callers`** | `<func_name>` <br> `--file` <br> `--visual` | Shows **incoming** calls: who calls this function? Use `--visual` or `-V` for graph view. |
47+
| **`cgc analyze chain`** | `<start> <end>` <br> `--depth` <br> `--visual` | Finds the call path between two functions. Default depth is 5. Use `--visual` or `-V` for graph view. |
48+
| **`cgc analyze deps`** | `<module>` <br> `--no-external` <br> `--visual` | Inspects dependencies (imports and importers) for a module. Use `--visual` or `-V` for graph view. |
49+
| **`cgc analyze tree`** | `<class_name>` <br> `--file` <br> `--visual` | Visualizes the Class Inheritance hierarchy for a given class. Use `--visual` or `-V` for graph view. |
3750
| **`cgc analyze complexity`**| `[path]` <br> `--threshold` <br> `--limit` | Lists functions with high Cyclomatic Complexity. Default threshold: 10. |
3851
| **`cgc analyze dead-code`** | `--exclude` | Finds potentially unused functions (0 callers). Use `--exclude` for decorators. |
52+
| **`cgc analyze overrides`** | `<func_name>` <br> `--visual` | Finds all implementations of a function across different classes. Use `--visual` or `-V` for graph view. |
3953

4054
## 4. Discovery & Search
4155
Find code elements when you don't know the exact structure.
4256

4357
| Command | Arguments | Description |
4458
| :--- | :--- | :--- |
45-
| **`cgc find name`** | `<name>` <br> `--type` | Finds code elements (Class, Function) by their **exact** name. |
46-
| **`cgc find pattern`** | `<pattern>` <br> `--case-sensitive` | Finds elements using fuzzy substring matching (e.g. "User" finds "UserHelper"). |
47-
| **`cgc find type`** | `<type>` <br> `--limit` | Lists all nodes of a specific type (e.g. `function`, `class`, `module`). |
59+
| **`cgc find name`** | `<name>` <br> `--type` <br> `--visual` | Finds code elements (Class, Function) by their **exact** name. Use `--visual` or `-V` for graph view. |
60+
| **`cgc find pattern`** | `<pattern>` <br> `--case-sensitive` <br> `--visual` | Finds elements using fuzzy substring matching (e.g. "User" finds "UserHelper"). Use `--visual` or `-V` for graph view. |
61+
| **`cgc find type`** | `<type>` <br> `--limit` <br> `--visual` | Lists all nodes of a specific type (e.g. `function`, `class`, `module`). Use `--visual` or `-V` for graph view. |
4862

4963
## 5. Configuration & Setup
5064
Manage your environment and database connections.
@@ -65,7 +79,7 @@ Helper commands for developers and the MCP server.
6579
| :--- | :--- | :--- |
6680
| **`cgc doctor`** | None | Runs system diagnostics (DB connection, dependencies, permissions). |
6781
| **`cgc visualize`** | `[query]` | Generates a link to open the Neo4j Browser. <br> *(Alias: `cgc v`)* |
68-
| **`cgc query`** | `<query>` | Executes a raw Cypher query directly against the DB. |
82+
| **`cgc query`** | `<query>` <br> `--visual` | Executes a raw Cypher query directly against the DB. Use `--visual` or `-V` for graph view. |
6983
| **`cgc mcp start`** | None | Starts the MCP Server (used by IDEs). |
7084
| **`cgc mcp tools`** | None | Lists all available MCP tools supported by the server. |
7185
| **`cgc start`** | None | **Deprecated**. Use `cgc mcp start` instead. |
@@ -171,3 +185,47 @@ cgc index . # Start indexing
171185
- Press `Ctrl+C` in the watch terminal
172186

173187
**💡 Tip:** This is perfect for active development sessions where you want your AI assistant to always have the latest code context!
188+
189+
### Scenario G: Visual Graph Exploration
190+
Use the `--visual` flag (or `-V`) to see results as interactive graphs in your browser.
191+
192+
1. **Visualize function calls:**
193+
```bash
194+
cgc analyze calls process_data --visual
195+
# or use the short form
196+
cgc analyze calls process_data -V
197+
```
198+
199+
2. **Visualize call chain between functions:**
200+
```bash
201+
cgc analyze chain main handle_request --visual
202+
```
203+
204+
3. **Visualize class inheritance:**
205+
```bash
206+
cgc analyze tree MyBaseClass --visual
207+
```
208+
209+
4. **Visualize module dependencies:**
210+
```bash
211+
cgc analyze deps mymodule --visual
212+
```
213+
214+
5. **Visualize search results:**
215+
```bash
216+
cgc find pattern "Controller" --visual
217+
```
218+
219+
6. **Visualize Cypher query results:**
220+
```bash
221+
cgc query "MATCH (n:Function)-[r:CALLS]->(m:Function) RETURN n,r,m LIMIT 50" --visual
222+
```
223+
224+
7. **Use global flag (applies to any command):**
225+
```bash
226+
cgc -V analyze callers my_function
227+
```
228+
229+
**💡 Tip:** The visualizations are interactive! Drag to pan, scroll to zoom, and click on nodes to highlight connections.
230+
231+
**📍 Output:** Visualization HTML files are saved to `~/.codegraphcontext/visualizations/` and automatically opened in your default browser.

src/codegraphcontext/cli/cli_helpers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,39 @@ def cypher_helper(query: str):
227227
db_manager.close_driver()
228228

229229

230+
def cypher_helper_visual(query: str):
231+
"""Executes a read-only Cypher query and visualizes the results."""
232+
from .visualizer import visualize_cypher_results
233+
234+
services = _initialize_services()
235+
if not all(services):
236+
return
237+
238+
db_manager, _, _ = services
239+
240+
# Replicating safety checks from MCPServer
241+
forbidden_keywords = ['CREATE', 'MERGE', 'DELETE', 'SET', 'REMOVE', 'DROP', 'CALL apoc']
242+
if any(keyword in query.upper() for keyword in forbidden_keywords):
243+
console.print("[bold red]Error: This command only supports read-only queries.[/bold red]")
244+
db_manager.close_driver()
245+
return
246+
247+
try:
248+
with db_manager.get_driver().session() as session:
249+
result = session.run(query)
250+
records = [record.data() for record in result]
251+
252+
if not records:
253+
console.print("[yellow]No results to visualize.[/yellow]")
254+
return # finally block will close driver
255+
256+
visualize_cypher_results(records, query)
257+
except Exception as e:
258+
console.print(f"[bold red]An error occurred while executing query:[/bold red] {e}")
259+
finally:
260+
db_manager.close_driver()
261+
262+
230263
import webbrowser
231264

232265
def visualize_helper(query: str):

0 commit comments

Comments
 (0)