tree-sitter-analyzer 0.9.8__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tree-sitter-analyzer might be problematic. Click here for more details.

@@ -1,528 +1,535 @@
1
- #!/usr/bin/env python3
2
- """
3
- Command Line Interface
4
-
5
- New CLI implementation that uses the API facade for all operations.
6
- Provides a clean separation between CLI concerns and core analysis logic.
7
- """
8
-
9
- import argparse
10
- import json
11
- import logging
12
- import sys
13
- from pathlib import Path
14
- from typing import Any
15
-
16
- from .. import api
17
-
18
- # Configure logging for CLI
19
- logging.basicConfig(
20
- level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
21
- )
22
- logger = logging.getLogger(__name__)
23
-
24
-
25
- def create_parser() -> argparse.ArgumentParser:
26
- """Create and configure the argument parser."""
27
- parser = argparse.ArgumentParser(
28
- prog="tree-sitter-analyzer",
29
- description="Extensible multi-language code analyzer using Tree-sitter",
30
- formatter_class=argparse.RawDescriptionHelpFormatter,
31
- epilog="""
32
- Examples:
33
- # Analyze a Java file
34
- tree-sitter-analyzer analyze example.java
35
-
36
- # Analyze with specific language
37
- tree-sitter-analyzer analyze --language python script.py
38
-
39
- # Execute specific queries
40
- tree-sitter-analyzer analyze --queries functions,classes example.java
41
-
42
- # Extract only code elements
43
- tree-sitter-analyzer extract example.py
44
-
45
- # List supported languages
46
- tree-sitter-analyzer languages
47
-
48
- # Get framework information
49
- tree-sitter-analyzer info
50
-
51
- For more information, visit: https://github.com/aimasteracc/tree-sitter-analyzer
52
- """,
53
- )
54
-
55
- # Global options
56
- parser.add_argument(
57
- "--version", action="version", version="tree-sitter-analyzer 0.9.3"
58
- )
59
- parser.add_argument(
60
- "--verbose", "-v", action="store_true", help="Enable verbose output"
61
- )
62
- parser.add_argument(
63
- "--quiet", "-q", action="store_true", help="Suppress non-essential output"
64
- )
65
- parser.add_argument(
66
- "--output",
67
- "-o",
68
- choices=["json", "text", "table"],
69
- default="text",
70
- help="Output format (default: text)",
71
- )
72
-
73
- # Subcommands
74
- subparsers = parser.add_subparsers(dest="command", help="Available commands")
75
-
76
- # Analyze command
77
- analyze_parser = subparsers.add_parser(
78
- "analyze",
79
- help="Analyze source code files",
80
- description="Perform comprehensive analysis of source code files",
81
- )
82
- analyze_parser.add_argument("file_path", help="Path to the source file to analyze")
83
- analyze_parser.add_argument(
84
- "--language", "-l", help="Programming language (auto-detected if not specified)"
85
- )
86
- analyze_parser.add_argument(
87
- "--queries", help="Comma-separated list of queries to execute"
88
- )
89
- analyze_parser.add_argument(
90
- "--no-elements", action="store_true", help="Skip code element extraction"
91
- )
92
- analyze_parser.add_argument(
93
- "--no-queries", action="store_true", help="Skip query execution"
94
- )
95
-
96
- # Extract command
97
- extract_parser = subparsers.add_parser(
98
- "extract",
99
- help="Extract code elements from files",
100
- description="Extract specific code elements like functions, classes, etc.",
101
- )
102
- extract_parser.add_argument("file_path", help="Path to the source file")
103
- extract_parser.add_argument(
104
- "--language", "-l", help="Programming language (auto-detected if not specified)"
105
- )
106
- extract_parser.add_argument(
107
- "--types",
108
- help="Comma-separated list of element types to extract (e.g., functions,classes)",
109
- )
110
-
111
- # Query command
112
- query_parser = subparsers.add_parser(
113
- "query",
114
- help="Execute specific queries on files",
115
- description="Execute specific tree-sitter queries on source files",
116
- )
117
- query_parser.add_argument("file_path", help="Path to the source file")
118
- query_parser.add_argument("query_name", help="Name of the query to execute")
119
- query_parser.add_argument(
120
- "--language", "-l", help="Programming language (auto-detected if not specified)"
121
- )
122
-
123
- # Validate command
124
- validate_parser = subparsers.add_parser(
125
- "validate",
126
- help="Validate source files",
127
- description="Check if files can be parsed and analyzed",
128
- )
129
- validate_parser.add_argument(
130
- "file_path", help="Path to the source file to validate"
131
- )
132
-
133
- # Languages command
134
- languages_parser = subparsers.add_parser(
135
- "languages",
136
- help="List supported languages",
137
- description="Show all supported programming languages and their extensions",
138
- )
139
- languages_parser.add_argument(
140
- "--extensions",
141
- action="store_true",
142
- help="Show file extensions for each language",
143
- )
144
-
145
- # Info command
146
- subparsers.add_parser(
147
- "info",
148
- help="Show framework information",
149
- description="Display information about the analyzer framework",
150
- )
151
-
152
- # Queries command
153
- queries_parser = subparsers.add_parser(
154
- "queries",
155
- help="List available queries",
156
- description="Show available queries for a specific language",
157
- )
158
- queries_parser.add_argument("language", help="Programming language name")
159
-
160
- return parser
161
-
162
-
163
- def handle_analyze_command(args: argparse.Namespace) -> int:
164
- """Handle the analyze command."""
165
- try:
166
- file_path = Path(args.file_path)
167
-
168
- if not file_path.exists():
169
- print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
170
- return 1
171
-
172
- # Parse queries if provided
173
- queries = None
174
- if args.queries:
175
- queries = [q.strip() for q in args.queries.split(",")]
176
-
177
- # Perform analysis
178
- result = api.analyze_file(
179
- file_path=file_path,
180
- language=args.language,
181
- queries=queries,
182
- include_elements=not args.no_elements,
183
- include_queries=not args.no_queries,
184
- )
185
-
186
- # Output results
187
- if args.output == "json":
188
- print(json.dumps(result, indent=2))
189
- else:
190
- format_analysis_output(result, args.output)
191
-
192
- return 0 if result.get("success", False) else 1
193
-
194
- except Exception as e:
195
- print(f"Error during analysis: {e}", file=sys.stderr)
196
- return 1
197
-
198
-
199
- def handle_extract_command(args: argparse.Namespace) -> int:
200
- """Handle the extract command."""
201
- try:
202
- file_path = Path(args.file_path)
203
-
204
- if not file_path.exists():
205
- print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
206
- return 1
207
-
208
- # Parse element types if provided
209
- element_types = None
210
- if args.types:
211
- element_types = [t.strip() for t in args.types.split(",")]
212
-
213
- # Extract elements
214
- result = api.extract_elements(
215
- file_path=file_path, language=args.language, element_types=element_types
216
- )
217
-
218
- # Output results
219
- if args.output == "json":
220
- print(json.dumps(result, indent=2))
221
- else:
222
- format_extraction_output(result, args.output)
223
-
224
- return 0 if result.get("success", False) else 1
225
-
226
- except Exception as e:
227
- print(f"Error during extraction: {e}", file=sys.stderr)
228
- return 1
229
-
230
-
231
- def handle_query_command(args: argparse.Namespace) -> int:
232
- """Handle the query command."""
233
- try:
234
- file_path = Path(args.file_path)
235
-
236
- if not file_path.exists():
237
- print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
238
- return 1
239
-
240
- # Execute query
241
- result = api.execute_query(
242
- file_path=file_path, query_name=args.query_name, language=args.language
243
- )
244
-
245
- # Output results
246
- if args.output == "json":
247
- print(json.dumps(result, indent=2))
248
- else:
249
- format_query_output(result, args.output)
250
-
251
- return 0 if result.get("success", False) else 1
252
-
253
- except Exception as e:
254
- print(f"Error during query execution: {e}", file=sys.stderr)
255
- return 1
256
-
257
-
258
- def handle_validate_command(args: argparse.Namespace) -> int:
259
- """Handle the validate command."""
260
- try:
261
- file_path = Path(args.file_path)
262
-
263
- # Validate file
264
- result = api.validate_file(file_path)
265
-
266
- # Output results
267
- if args.output == "json":
268
- print(json.dumps(result, indent=2))
269
- else:
270
- format_validation_output(result, args.output)
271
-
272
- return 0 if result.get("valid", False) else 1
273
-
274
- except Exception as e:
275
- print(f"Error during validation: {e}", file=sys.stderr)
276
- return 1
277
-
278
-
279
- def handle_languages_command(args: argparse.Namespace) -> int:
280
- """Handle the languages command."""
281
- try:
282
- languages = api.get_supported_languages()
283
-
284
- if args.output == "json":
285
- if args.extensions:
286
- lang_info = {}
287
- for lang in languages:
288
- extensions = api.get_file_extensions(lang)
289
- lang_info[lang] = extensions
290
- print(json.dumps(lang_info, indent=2))
291
- else:
292
- print(json.dumps(languages, indent=2))
293
- else:
294
- print("Supported Languages:")
295
- print("=" * 20)
296
- for lang in sorted(languages):
297
- if args.extensions:
298
- extensions = api.get_file_extensions(lang)
299
- ext_str = ", ".join(extensions) if extensions else "No extensions"
300
- print(f" {lang:<12} - {ext_str}")
301
- else:
302
- print(f" {lang}")
303
-
304
- if not args.extensions:
305
- print(f"\nTotal: {len(languages)} languages")
306
- print("Use --extensions to see file extensions for each language")
307
-
308
- return 0
309
-
310
- except Exception as e:
311
- print(f"Error getting language information: {e}", file=sys.stderr)
312
- return 1
313
-
314
-
315
- def handle_info_command(args: argparse.Namespace) -> int:
316
- """Handle the info command."""
317
- try:
318
- info = api.get_framework_info()
319
-
320
- if args.output == "json":
321
- print(json.dumps(info, indent=2))
322
- else:
323
- print("Tree-sitter Analyzer Framework Information")
324
- print("=" * 45)
325
- print(f"Name: {info.get('name', 'Unknown')}")
326
- print(f"Version: {info.get('version', 'Unknown')}")
327
- print(f"Supported Languages: {info.get('total_languages', 0)}")
328
-
329
- languages = info.get("supported_languages", [])
330
- if languages:
331
- print(f"Languages: {', '.join(sorted(languages))}")
332
-
333
- components = info.get("core_components", [])
334
- if components:
335
- print(f"Core Components: {', '.join(components)}")
336
-
337
- return 0
338
-
339
- except Exception as e:
340
- print(f"Error getting framework information: {e}", file=sys.stderr)
341
- return 1
342
-
343
-
344
- def handle_queries_command(args: argparse.Namespace) -> int:
345
- """Handle the queries command."""
346
- try:
347
- if not api.is_language_supported(args.language):
348
- print(
349
- f"Error: Language '{args.language}' is not supported", file=sys.stderr
350
- )
351
- return 1
352
-
353
- queries = api.get_available_queries(args.language)
354
-
355
- if args.output == "json":
356
- print(json.dumps(queries, indent=2))
357
- else:
358
- print(f"Available Queries for {args.language}:")
359
- print("=" * (25 + len(args.language)))
360
- for query in sorted(queries):
361
- print(f" {query}")
362
-
363
- print(f"\nTotal: {len(queries)} queries")
364
-
365
- return 0
366
-
367
- except Exception as e:
368
- print(f"Error getting query information: {e}", file=sys.stderr)
369
- return 1
370
-
371
-
372
- def format_analysis_output(result: dict[str, Any], output_format: str) -> None:
373
- """Format and display analysis results."""
374
- if not result.get("success", False):
375
- print(
376
- f"Analysis failed: {result.get('error', 'Unknown error')}", file=sys.stderr
377
- )
378
- return
379
-
380
- print("Analysis Results")
381
- print("=" * 16)
382
-
383
- # File information
384
- file_info = result.get("file_info", {})
385
- print(f"File: {file_info.get('path', 'Unknown')}")
386
-
387
- # Language information
388
- lang_info = result.get("language_info", {})
389
- language = lang_info.get("language", "Unknown")
390
- auto_detected = lang_info.get("auto_detected", False)
391
- detection_str = " (auto-detected)" if auto_detected else ""
392
- print(f"Language: {language}{detection_str}")
393
-
394
- # AST information
395
- ast_info = result.get("ast_info", {})
396
- print(f"Source Lines: {ast_info.get('source_lines', 0)}")
397
- print(f"AST Nodes: {ast_info.get('node_count', 0)}")
398
-
399
- # Query results
400
- query_results = result.get("query_results", {})
401
- if query_results:
402
- print("\nQuery Results:")
403
- for query_name, matches in query_results.items():
404
- print(f" {query_name}: {len(matches)} matches")
405
-
406
- # Elements
407
- elements = result.get("elements", [])
408
- if elements:
409
- print(f"\nCode Elements: {len(elements)} found")
410
- element_types: dict[str, int] = {}
411
- for element in elements:
412
- elem_type = element.get("type", "unknown")
413
- element_types[elem_type] = element_types.get(elem_type, 0) + 1
414
-
415
- for elem_type, count in sorted(element_types.items()):
416
- print(f" {elem_type}: {count}")
417
-
418
-
419
- def format_extraction_output(result: dict[str, Any], output_format: str) -> None:
420
- """Format and display extraction results."""
421
- if not result.get("success", False):
422
- print(
423
- f"Extraction failed: {result.get('error', 'Unknown error')}",
424
- file=sys.stderr,
425
- )
426
- return
427
-
428
- elements = result.get("elements", [])
429
- language = result.get("language", "Unknown")
430
-
431
- print("Code Element Extraction Results")
432
- print("=" * 31)
433
- print(f"File: {result.get('file_path', 'Unknown')}")
434
- print(f"Language: {language}")
435
- print(f"Elements Found: {len(elements)}")
436
-
437
- if elements:
438
- print("\nElements:")
439
- for element in elements:
440
- name = element.get("name", "Unknown")
441
- elem_type = element.get("type", "unknown")
442
- start_line = element.get("start_line", 0)
443
- print(f" {elem_type}: {name} (line {start_line})")
444
-
445
-
446
- def format_query_output(result: dict[str, Any], output_format: str) -> None:
447
- """Format and display query results."""
448
- if not result.get("success", False):
449
- print(f"Query failed: {result.get('error', 'Unknown error')}", file=sys.stderr)
450
- return
451
-
452
- query_name = result.get("query_name", "Unknown")
453
- matches = result.get("results", [])
454
- language = result.get("language", "Unknown")
455
-
456
- print("Query Execution Results")
457
- print("=" * 23)
458
- print(f"File: {result.get('file_path', 'Unknown')}")
459
- print(f"Language: {language}")
460
- print(f"Query: {query_name}")
461
- print(f"Matches: {len(matches)}")
462
-
463
- if matches:
464
- print("\nMatches:")
465
- for i, match in enumerate(matches, 1):
466
- start_line = match.get("start_line", 0)
467
- content = match.get("content", "").strip()
468
- if len(content) > 50:
469
- content = content[:47] + "..."
470
- print(f" {i}. Line {start_line}: {content}")
471
-
472
-
473
- def format_validation_output(result: dict[str, Any], output_format: str) -> None:
474
- """Format and display validation results."""
475
- valid = result.get("valid", False)
476
- exists = result.get("exists", False)
477
- readable = result.get("readable", False)
478
- language = result.get("language")
479
- supported = result.get("supported", False)
480
- errors = result.get("errors", [])
481
-
482
- print("File Validation Results")
483
- print("=" * 23)
484
- print(f"Valid: {'✓' if valid else '✗'}")
485
- print(f"Exists: {'✓' if exists else '✗'}")
486
- print(f"Readable: {'✓' if readable else '✗'}")
487
- print(f"Language: {language or 'Unknown'}")
488
- print(f"Supported: {'✓' if supported else '✗'}")
489
-
490
- if errors:
491
- print("\nErrors:")
492
- for error in errors:
493
- print(f" - {error}")
494
-
495
-
496
- def main() -> int:
497
- """Main CLI entry point."""
498
- parser = create_parser()
499
- args = parser.parse_args()
500
-
501
- # Configure logging based on verbosity
502
- if args.quiet:
503
- logging.getLogger().setLevel(logging.ERROR)
504
- elif args.verbose:
505
- logging.getLogger().setLevel(logging.DEBUG)
506
-
507
- # Handle commands
508
- if args.command == "analyze":
509
- return handle_analyze_command(args)
510
- elif args.command == "extract":
511
- return handle_extract_command(args)
512
- elif args.command == "query":
513
- return handle_query_command(args)
514
- elif args.command == "validate":
515
- return handle_validate_command(args)
516
- elif args.command == "languages":
517
- return handle_languages_command(args)
518
- elif args.command == "info":
519
- return handle_info_command(args)
520
- elif args.command == "queries":
521
- return handle_queries_command(args)
522
- else:
523
- parser.print_help()
524
- return 1
525
-
526
-
527
- if __name__ == "__main__":
528
- sys.exit(main())
1
+ #!/usr/bin/env python3
2
+ """
3
+ Command Line Interface
4
+
5
+ New CLI implementation that uses the API facade for all operations.
6
+ Provides a clean separation between CLI concerns and core analysis logic.
7
+ """
8
+
9
+ import argparse
10
+ import importlib.metadata
11
+ import json
12
+ import logging
13
+ import sys
14
+ from pathlib import Path
15
+ from typing import Any
16
+
17
+ from .. import api
18
+
19
+ # Configure logging for CLI
20
+ logging.basicConfig(
21
+ level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
22
+ )
23
+ logger = logging.getLogger(__name__)
24
+
25
+
26
+ def create_parser() -> argparse.ArgumentParser:
27
+ """Create and configure the argument parser."""
28
+ parser = argparse.ArgumentParser(
29
+ prog="tree-sitter-analyzer",
30
+ description="Extensible multi-language code analyzer using Tree-sitter",
31
+ formatter_class=argparse.RawDescriptionHelpFormatter,
32
+ epilog="""
33
+ Examples:
34
+ # Analyze a Java file
35
+ tree-sitter-analyzer analyze example.java
36
+
37
+ # Analyze with specific language
38
+ tree-sitter-analyzer analyze --language python script.py
39
+
40
+ # Execute specific queries
41
+ tree-sitter-analyzer analyze --queries functions,classes example.java
42
+
43
+ # Extract only code elements
44
+ tree-sitter-analyzer extract example.py
45
+
46
+ # List supported languages
47
+ tree-sitter-analyzer languages
48
+
49
+ # Get framework information
50
+ tree-sitter-analyzer info
51
+
52
+ For more information, visit: https://github.com/aimasteracc/tree-sitter-analyzer
53
+ """,
54
+ )
55
+
56
+ # Global options
57
+ # Get version dynamically from package metadata
58
+ try:
59
+ version = importlib.metadata.version("tree-sitter-analyzer")
60
+ except importlib.metadata.PackageNotFoundError:
61
+ version = "0.9.9" # Fallback version
62
+
63
+ parser.add_argument(
64
+ "--version", action="version", version=f"tree-sitter-analyzer {version}"
65
+ )
66
+ parser.add_argument(
67
+ "--verbose", "-v", action="store_true", help="Enable verbose output"
68
+ )
69
+ parser.add_argument(
70
+ "--quiet", "-q", action="store_true", help="Suppress non-essential output"
71
+ )
72
+ parser.add_argument(
73
+ "--output",
74
+ "-o",
75
+ choices=["json", "text", "table"],
76
+ default="text",
77
+ help="Output format (default: text)",
78
+ )
79
+
80
+ # Subcommands
81
+ subparsers = parser.add_subparsers(dest="command", help="Available commands")
82
+
83
+ # Analyze command
84
+ analyze_parser = subparsers.add_parser(
85
+ "analyze",
86
+ help="Analyze source code files",
87
+ description="Perform comprehensive analysis of source code files",
88
+ )
89
+ analyze_parser.add_argument("file_path", help="Path to the source file to analyze")
90
+ analyze_parser.add_argument(
91
+ "--language", "-l", help="Programming language (auto-detected if not specified)"
92
+ )
93
+ analyze_parser.add_argument(
94
+ "--queries", help="Comma-separated list of queries to execute"
95
+ )
96
+ analyze_parser.add_argument(
97
+ "--no-elements", action="store_true", help="Skip code element extraction"
98
+ )
99
+ analyze_parser.add_argument(
100
+ "--no-queries", action="store_true", help="Skip query execution"
101
+ )
102
+
103
+ # Extract command
104
+ extract_parser = subparsers.add_parser(
105
+ "extract",
106
+ help="Extract code elements from files",
107
+ description="Extract specific code elements like functions, classes, etc.",
108
+ )
109
+ extract_parser.add_argument("file_path", help="Path to the source file")
110
+ extract_parser.add_argument(
111
+ "--language", "-l", help="Programming language (auto-detected if not specified)"
112
+ )
113
+ extract_parser.add_argument(
114
+ "--types",
115
+ help="Comma-separated list of element types to extract (e.g., functions,classes)",
116
+ )
117
+
118
+ # Query command
119
+ query_parser = subparsers.add_parser(
120
+ "query",
121
+ help="Execute specific queries on files",
122
+ description="Execute specific tree-sitter queries on source files",
123
+ )
124
+ query_parser.add_argument("file_path", help="Path to the source file")
125
+ query_parser.add_argument("query_name", help="Name of the query to execute")
126
+ query_parser.add_argument(
127
+ "--language", "-l", help="Programming language (auto-detected if not specified)"
128
+ )
129
+
130
+ # Validate command
131
+ validate_parser = subparsers.add_parser(
132
+ "validate",
133
+ help="Validate source files",
134
+ description="Check if files can be parsed and analyzed",
135
+ )
136
+ validate_parser.add_argument(
137
+ "file_path", help="Path to the source file to validate"
138
+ )
139
+
140
+ # Languages command
141
+ languages_parser = subparsers.add_parser(
142
+ "languages",
143
+ help="List supported languages",
144
+ description="Show all supported programming languages and their extensions",
145
+ )
146
+ languages_parser.add_argument(
147
+ "--extensions",
148
+ action="store_true",
149
+ help="Show file extensions for each language",
150
+ )
151
+
152
+ # Info command
153
+ subparsers.add_parser(
154
+ "info",
155
+ help="Show framework information",
156
+ description="Display information about the analyzer framework",
157
+ )
158
+
159
+ # Queries command
160
+ queries_parser = subparsers.add_parser(
161
+ "queries",
162
+ help="List available queries",
163
+ description="Show available queries for a specific language",
164
+ )
165
+ queries_parser.add_argument("language", help="Programming language name")
166
+
167
+ return parser
168
+
169
+
170
+ def handle_analyze_command(args: argparse.Namespace) -> int:
171
+ """Handle the analyze command."""
172
+ try:
173
+ file_path = Path(args.file_path)
174
+
175
+ if not file_path.exists():
176
+ print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
177
+ return 1
178
+
179
+ # Parse queries if provided
180
+ queries = None
181
+ if args.queries:
182
+ queries = [q.strip() for q in args.queries.split(",")]
183
+
184
+ # Perform analysis
185
+ result = api.analyze_file(
186
+ file_path=file_path,
187
+ language=args.language,
188
+ queries=queries,
189
+ include_elements=not args.no_elements,
190
+ include_queries=not args.no_queries,
191
+ )
192
+
193
+ # Output results
194
+ if args.output == "json":
195
+ print(json.dumps(result, indent=2))
196
+ else:
197
+ format_analysis_output(result, args.output)
198
+
199
+ return 0 if result.get("success", False) else 1
200
+
201
+ except Exception as e:
202
+ print(f"Error during analysis: {e}", file=sys.stderr)
203
+ return 1
204
+
205
+
206
+ def handle_extract_command(args: argparse.Namespace) -> int:
207
+ """Handle the extract command."""
208
+ try:
209
+ file_path = Path(args.file_path)
210
+
211
+ if not file_path.exists():
212
+ print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
213
+ return 1
214
+
215
+ # Parse element types if provided
216
+ element_types = None
217
+ if args.types:
218
+ element_types = [t.strip() for t in args.types.split(",")]
219
+
220
+ # Extract elements
221
+ result = api.extract_elements(
222
+ file_path=file_path, language=args.language, element_types=element_types
223
+ )
224
+
225
+ # Output results
226
+ if args.output == "json":
227
+ print(json.dumps(result, indent=2))
228
+ else:
229
+ format_extraction_output(result, args.output)
230
+
231
+ return 0 if result.get("success", False) else 1
232
+
233
+ except Exception as e:
234
+ print(f"Error during extraction: {e}", file=sys.stderr)
235
+ return 1
236
+
237
+
238
+ def handle_query_command(args: argparse.Namespace) -> int:
239
+ """Handle the query command."""
240
+ try:
241
+ file_path = Path(args.file_path)
242
+
243
+ if not file_path.exists():
244
+ print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
245
+ return 1
246
+
247
+ # Execute query
248
+ result = api.execute_query(
249
+ file_path=file_path, query_name=args.query_name, language=args.language
250
+ )
251
+
252
+ # Output results
253
+ if args.output == "json":
254
+ print(json.dumps(result, indent=2))
255
+ else:
256
+ format_query_output(result, args.output)
257
+
258
+ return 0 if result.get("success", False) else 1
259
+
260
+ except Exception as e:
261
+ print(f"Error during query execution: {e}", file=sys.stderr)
262
+ return 1
263
+
264
+
265
+ def handle_validate_command(args: argparse.Namespace) -> int:
266
+ """Handle the validate command."""
267
+ try:
268
+ file_path = Path(args.file_path)
269
+
270
+ # Validate file
271
+ result = api.validate_file(file_path)
272
+
273
+ # Output results
274
+ if args.output == "json":
275
+ print(json.dumps(result, indent=2))
276
+ else:
277
+ format_validation_output(result, args.output)
278
+
279
+ return 0 if result.get("valid", False) else 1
280
+
281
+ except Exception as e:
282
+ print(f"Error during validation: {e}", file=sys.stderr)
283
+ return 1
284
+
285
+
286
+ def handle_languages_command(args: argparse.Namespace) -> int:
287
+ """Handle the languages command."""
288
+ try:
289
+ languages = api.get_supported_languages()
290
+
291
+ if args.output == "json":
292
+ if args.extensions:
293
+ lang_info = {}
294
+ for lang in languages:
295
+ extensions = api.get_file_extensions(lang)
296
+ lang_info[lang] = extensions
297
+ print(json.dumps(lang_info, indent=2))
298
+ else:
299
+ print(json.dumps(languages, indent=2))
300
+ else:
301
+ print("Supported Languages:")
302
+ print("=" * 20)
303
+ for lang in sorted(languages):
304
+ if args.extensions:
305
+ extensions = api.get_file_extensions(lang)
306
+ ext_str = ", ".join(extensions) if extensions else "No extensions"
307
+ print(f" {lang:<12} - {ext_str}")
308
+ else:
309
+ print(f" {lang}")
310
+
311
+ if not args.extensions:
312
+ print(f"\nTotal: {len(languages)} languages")
313
+ print("Use --extensions to see file extensions for each language")
314
+
315
+ return 0
316
+
317
+ except Exception as e:
318
+ print(f"Error getting language information: {e}", file=sys.stderr)
319
+ return 1
320
+
321
+
322
+ def handle_info_command(args: argparse.Namespace) -> int:
323
+ """Handle the info command."""
324
+ try:
325
+ info = api.get_framework_info()
326
+
327
+ if args.output == "json":
328
+ print(json.dumps(info, indent=2))
329
+ else:
330
+ print("Tree-sitter Analyzer Framework Information")
331
+ print("=" * 45)
332
+ print(f"Name: {info.get('name', 'Unknown')}")
333
+ print(f"Version: {info.get('version', 'Unknown')}")
334
+ print(f"Supported Languages: {info.get('total_languages', 0)}")
335
+
336
+ languages = info.get("supported_languages", [])
337
+ if languages:
338
+ print(f"Languages: {', '.join(sorted(languages))}")
339
+
340
+ components = info.get("core_components", [])
341
+ if components:
342
+ print(f"Core Components: {', '.join(components)}")
343
+
344
+ return 0
345
+
346
+ except Exception as e:
347
+ print(f"Error getting framework information: {e}", file=sys.stderr)
348
+ return 1
349
+
350
+
351
+ def handle_queries_command(args: argparse.Namespace) -> int:
352
+ """Handle the queries command."""
353
+ try:
354
+ if not api.is_language_supported(args.language):
355
+ print(
356
+ f"Error: Language '{args.language}' is not supported", file=sys.stderr
357
+ )
358
+ return 1
359
+
360
+ queries = api.get_available_queries(args.language)
361
+
362
+ if args.output == "json":
363
+ print(json.dumps(queries, indent=2))
364
+ else:
365
+ print(f"Available Queries for {args.language}:")
366
+ print("=" * (25 + len(args.language)))
367
+ for query in sorted(queries):
368
+ print(f" {query}")
369
+
370
+ print(f"\nTotal: {len(queries)} queries")
371
+
372
+ return 0
373
+
374
+ except Exception as e:
375
+ print(f"Error getting query information: {e}", file=sys.stderr)
376
+ return 1
377
+
378
+
379
+ def format_analysis_output(result: dict[str, Any], output_format: str) -> None:
380
+ """Format and display analysis results."""
381
+ if not result.get("success", False):
382
+ print(
383
+ f"Analysis failed: {result.get('error', 'Unknown error')}", file=sys.stderr
384
+ )
385
+ return
386
+
387
+ print("Analysis Results")
388
+ print("=" * 16)
389
+
390
+ # File information
391
+ file_info = result.get("file_info", {})
392
+ print(f"File: {file_info.get('path', 'Unknown')}")
393
+
394
+ # Language information
395
+ lang_info = result.get("language_info", {})
396
+ language = lang_info.get("language", "Unknown")
397
+ auto_detected = lang_info.get("auto_detected", False)
398
+ detection_str = " (auto-detected)" if auto_detected else ""
399
+ print(f"Language: {language}{detection_str}")
400
+
401
+ # AST information
402
+ ast_info = result.get("ast_info", {})
403
+ print(f"Source Lines: {ast_info.get('source_lines', 0)}")
404
+ print(f"AST Nodes: {ast_info.get('node_count', 0)}")
405
+
406
+ # Query results
407
+ query_results = result.get("query_results", {})
408
+ if query_results:
409
+ print("\nQuery Results:")
410
+ for query_name, matches in query_results.items():
411
+ print(f" {query_name}: {len(matches)} matches")
412
+
413
+ # Elements
414
+ elements = result.get("elements", [])
415
+ if elements:
416
+ print(f"\nCode Elements: {len(elements)} found")
417
+ element_types: dict[str, int] = {}
418
+ for element in elements:
419
+ elem_type = element.get("type", "unknown")
420
+ element_types[elem_type] = element_types.get(elem_type, 0) + 1
421
+
422
+ for elem_type, count in sorted(element_types.items()):
423
+ print(f" {elem_type}: {count}")
424
+
425
+
426
+ def format_extraction_output(result: dict[str, Any], output_format: str) -> None:
427
+ """Format and display extraction results."""
428
+ if not result.get("success", False):
429
+ print(
430
+ f"Extraction failed: {result.get('error', 'Unknown error')}",
431
+ file=sys.stderr,
432
+ )
433
+ return
434
+
435
+ elements = result.get("elements", [])
436
+ language = result.get("language", "Unknown")
437
+
438
+ print("Code Element Extraction Results")
439
+ print("=" * 31)
440
+ print(f"File: {result.get('file_path', 'Unknown')}")
441
+ print(f"Language: {language}")
442
+ print(f"Elements Found: {len(elements)}")
443
+
444
+ if elements:
445
+ print("\nElements:")
446
+ for element in elements:
447
+ name = element.get("name", "Unknown")
448
+ elem_type = element.get("type", "unknown")
449
+ start_line = element.get("start_line", 0)
450
+ print(f" {elem_type}: {name} (line {start_line})")
451
+
452
+
453
+ def format_query_output(result: dict[str, Any], output_format: str) -> None:
454
+ """Format and display query results."""
455
+ if not result.get("success", False):
456
+ print(f"Query failed: {result.get('error', 'Unknown error')}", file=sys.stderr)
457
+ return
458
+
459
+ query_name = result.get("query_name", "Unknown")
460
+ matches = result.get("results", [])
461
+ language = result.get("language", "Unknown")
462
+
463
+ print("Query Execution Results")
464
+ print("=" * 23)
465
+ print(f"File: {result.get('file_path', 'Unknown')}")
466
+ print(f"Language: {language}")
467
+ print(f"Query: {query_name}")
468
+ print(f"Matches: {len(matches)}")
469
+
470
+ if matches:
471
+ print("\nMatches:")
472
+ for i, match in enumerate(matches, 1):
473
+ start_line = match.get("start_line", 0)
474
+ content = match.get("content", "").strip()
475
+ if len(content) > 50:
476
+ content = content[:47] + "..."
477
+ print(f" {i}. Line {start_line}: {content}")
478
+
479
+
480
+ def format_validation_output(result: dict[str, Any], output_format: str) -> None:
481
+ """Format and display validation results."""
482
+ valid = result.get("valid", False)
483
+ exists = result.get("exists", False)
484
+ readable = result.get("readable", False)
485
+ language = result.get("language")
486
+ supported = result.get("supported", False)
487
+ errors = result.get("errors", [])
488
+
489
+ print("File Validation Results")
490
+ print("=" * 23)
491
+ print(f"Valid: {'✓' if valid else '✗'}")
492
+ print(f"Exists: {'✓' if exists else '✗'}")
493
+ print(f"Readable: {'✓' if readable else '✗'}")
494
+ print(f"Language: {language or 'Unknown'}")
495
+ print(f"Supported: {'✓' if supported else '✗'}")
496
+
497
+ if errors:
498
+ print("\nErrors:")
499
+ for error in errors:
500
+ print(f" - {error}")
501
+
502
+
503
+ def main() -> int:
504
+ """Main CLI entry point."""
505
+ parser = create_parser()
506
+ args = parser.parse_args()
507
+
508
+ # Configure logging based on verbosity
509
+ if args.quiet:
510
+ logging.getLogger().setLevel(logging.ERROR)
511
+ elif args.verbose:
512
+ logging.getLogger().setLevel(logging.DEBUG)
513
+
514
+ # Handle commands
515
+ if args.command == "analyze":
516
+ return handle_analyze_command(args)
517
+ elif args.command == "extract":
518
+ return handle_extract_command(args)
519
+ elif args.command == "query":
520
+ return handle_query_command(args)
521
+ elif args.command == "validate":
522
+ return handle_validate_command(args)
523
+ elif args.command == "languages":
524
+ return handle_languages_command(args)
525
+ elif args.command == "info":
526
+ return handle_info_command(args)
527
+ elif args.command == "queries":
528
+ return handle_queries_command(args)
529
+ else:
530
+ parser.print_help()
531
+ return 1
532
+
533
+
534
+ if __name__ == "__main__":
535
+ sys.exit(main())