codesearch 0.1.9

A fast, intelligent CLI tool with multiple search modes (regex, fuzzy, semantic), code analysis, and dead code detection for popular programming languages
Documentation
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
//! CLI argument definitions and parsing
//!
//! This module contains all command-line interface definitions using clap.

use clap::{Parser, Subcommand};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "codesearch")]
#[command(about = "A fast CLI tool for searching codebases")]
#[command(version)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,

    /// Search query pattern
    #[arg(index = 1)]
    pub query: Option<String>,

    /// Path to search (file or directory, default: current directory)
    #[arg(index = 2, default_value = ".")]
    pub path: PathBuf,

    /// File extensions to include (e.g., rs,py,js)
    #[arg(short, long, value_delimiter = ',')]
    pub extensions: Option<Vec<String>>,

    /// Enable fuzzy search
    #[arg(short, long)]
    pub fuzzy: bool,

    /// Case-insensitive search (default: true)
    #[arg(short, long, default_value = "true")]
    pub ignore_case: bool,

    /// Maximum results per file
    #[arg(short, long, default_value = "10")]
    pub max_results: usize,

    /// Exclude directories
    #[arg(long, value_delimiter = ',')]
    pub exclude: Option<Vec<String>>,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Search for text patterns in code files
    Search {
        /// The search query (supports regex)
        query: String,
        /// Path to search (file or directory, default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Case-insensitive search
        #[arg(short, long)]
        ignore_case: bool,
        /// Hide line numbers (line numbers shown by default)
        #[arg(short = 'N', long)]
        no_line_numbers: bool,
        /// Maximum number of results per file
        #[arg(long, default_value = "10")]
        max_results: usize,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
        /// Show search statistics
        #[arg(long)]
        stats: bool,
        /// Enable fuzzy search (handles typos and variations)
        #[arg(long)]
        fuzzy: bool,
        /// Fuzzy search threshold (0.0 = exact match, 1.0 = very loose)
        #[arg(long, default_value = "0.6")]
        fuzzy_threshold: f64,
        /// Exclude directories (default: auto-excludes common build dirs)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Don't auto-exclude common build directories
        #[arg(long)]
        no_auto_exclude: bool,
        /// Sort results by relevance score
        #[arg(long)]
        rank: bool,
        /// Enable intelligent caching for faster repeated searches
        #[arg(long)]
        cache: bool,
        /// Enable semantic search (context-aware matching)
        #[arg(long)]
        semantic: bool,
        /// Performance benchmark mode
        #[arg(long)]
        benchmark: bool,
        /// Compare performance with grep
        #[arg(long)]
        vs_grep: bool,
        /// Export results to file (csv, markdown, md)
        #[arg(long)]
        export: Option<String>,
    },
    /// List all searchable files
    Files {
        /// Path to scan (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
    },
    /// Interactive search mode
    Interactive {
        /// Path to search (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
    },
    /// Find symbol: definition, references, callers (structure-aware)
    Find {
        /// Symbol to find (function, class, or identifier)
        symbol: String,
        /// Path to search (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Filter: definition, references, callers, or all
        #[arg(long, default_value = "all")]
        r#type: String,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Analyze codebase metrics and statistics
    Analyze {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Include Halstead/maintainability metrics (merged from metrics command)
        #[arg(long)]
        metrics: bool,
        /// Output format when used with --metrics (text, json)
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Analyze code complexity metrics
    Complexity {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Show only files above complexity threshold
        #[arg(long)]
        threshold: Option<u32>,
        /// Sort by complexity (highest first)
        #[arg(long)]
        sort: bool,
    },
    /// Analyze design metrics (coupling, cohesion, instability)
    DesignMetrics {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Show detailed metrics for each module
        #[arg(long)]
        detailed: bool,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Comprehensive code metrics (complexity, size, maintainability)
    Metrics {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Show detailed metrics for each file
        #[arg(long)]
        detailed: bool,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Detect code duplication in the codebase
    Duplicates {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Minimum lines for a duplicate block
        #[arg(long, default_value = "3")]
        min_lines: usize,
        /// Similarity threshold (0.0 - 1.0)
        #[arg(long, default_value = "0.9")]
        similarity: f64,
    },
    /// Detect potentially dead/unused code
    Deadcode {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
    },
    /// Unified codebase health scan (dead code + duplicates + complexity)
    Health {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
        /// Exit with code 1 if health score is below this threshold (CI gate)
        #[arg(long)]
        fail_under: Option<u8>,
    },
    /// Detect circular function calls
    Circular {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
    },
    /// List all supported programming languages
    Languages,
    /// Run as MCP server
    McpServer,
    /// Build or update code index for faster searches
    Index {
        /// Path to index (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Index file path
        #[arg(long, default_value = ".codesearch/index.json")]
        index_file: PathBuf,
    },
    /// Watch directory for changes and update index
    Watch {
        /// Path to watch (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to watch (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Index file path
        #[arg(long, default_value = ".codesearch/index.json")]
        index_file: PathBuf,
    },
    /// Trace data flow from source variable (lite path-query, no indexing)
    Flow {
        /// Source variable to trace
        source: String,
        /// Path to analyze (file or directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// Sink pattern to filter (e.g., exec, eval)
        #[arg(long)]
        sink: Option<String>,
        /// File extensions (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
    },
    /// Scan for dangerous security patterns (instant, no query language)
    Security {
        /// Path to scan (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions (e.g., js,py,php)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Unified graph analysis: cfg, dfg, dep, ast, pdg, unified
    Graph {
        /// Graph type (cfg, dfg, dep, ast, pdg, unified)
        #[arg(value_parser = ["cfg", "dfg", "dep", "ast", "pdg", "unified"])]
        graph_type: String,
        /// Path to analyze (file or directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to analyze (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Output format (text, json, dot)
        #[arg(long, default_value = "text")]
        format: String,
        /// Export to file
        #[arg(long)]
        export: Option<PathBuf>,
        /// Show parallelization opportunities (pdg only)
        #[arg(long)]
        parallel: bool,
        /// Show circular dependencies only (dep only)
        #[arg(long)]
        circular_only: bool,
    },
    /// Analyze code using AST (Abstract Syntax Tree)
    Ast {
        /// Path to analyze (file or directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to analyze (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Analyze Control Flow Graph (CFG)
    Cfg {
        /// Path to analyze (file or directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to analyze (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Output format (text, json, dot)
        #[arg(long, default_value = "text")]
        format: String,
        /// Export to file
        #[arg(long)]
        export: Option<PathBuf>,
    },
    /// Analyze Data Flow Graph (DFG)
    Dfg {
        /// Path to analyze (file or directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to analyze (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Output format (text, json, dot)
        #[arg(long, default_value = "text")]
        format: String,
        /// Export to file
        #[arg(long)]
        export: Option<PathBuf>,
    },
    /// Analyze Call Graph
    Callgraph {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Output format (text, json, dot)
        #[arg(long, default_value = "text")]
        format: String,
        /// Show only recursive functions
        #[arg(long)]
        recursive_only: bool,
        /// Show only dead functions
        #[arg(long)]
        dead_only: bool,
    },
    /// Analyze Program Dependency Graph (PDG)
    Pdg {
        /// Path to analyze (file or directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to analyze (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Output format (text, json, dot)
        #[arg(long, default_value = "text")]
        format: String,
        /// Show parallelization opportunities
        #[arg(long)]
        parallel: bool,
        /// Export to file
        #[arg(long)]
        export: Option<PathBuf>,
    },
    /// Analyze all graph types for a file
    GraphAll {
        /// Path to analyze (file)
        path: PathBuf,
        /// Output format (text, json)
        #[arg(long, default_value = "text")]
        format: String,
        /// Export directory for graph files
        #[arg(long)]
        export_dir: Option<PathBuf>,
    },
    /// Build and analyze dependency graph
    Depgraph {
        /// Path to analyze (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// Exclude directories (e.g., target,node_modules)
        #[arg(long, value_delimiter = ',')]
        exclude: Option<Vec<String>>,
        /// Output format (text, json, dot)
        #[arg(long, default_value = "text")]
        format: String,
        /// Show circular dependencies only
        #[arg(long)]
        circular_only: bool,
    },
    /// Search git history
    GitHistory {
        /// Search pattern
        query: String,
        /// Repository path (default: current directory)
        #[arg(default_value = ".")]
        path: PathBuf,
        /// Maximum commits to search
        #[arg(long, default_value = "100")]
        max_commits: usize,
        /// Search by author
        #[arg(long)]
        author: Option<String>,
        /// Search in commit messages
        #[arg(long)]
        message: bool,
        /// Search specific file history
        #[arg(long)]
        file: Option<String>,
    },
    /// Search remote repositories
    Remote {
        /// Search pattern
        query: String,
        /// Repository URL or search on GitHub/GitLab
        #[arg(long)]
        repo: Option<String>,
        /// File extensions to include (e.g., rs,py,js)
        #[arg(short, long, value_delimiter = ',')]
        extensions: Option<Vec<String>>,
        /// GitHub API token (or set GITHUB_TOKEN env var)
        #[arg(long)]
        token: Option<String>,
        /// Search on GitHub
        #[arg(long)]
        github: bool,
        /// Language filter for GitHub search
        #[arg(long)]
        language: Option<String>,
        /// Maximum results
        #[arg(long, default_value = "20")]
        max_results: usize,
    },
}

/// Default directories to exclude from search
pub fn get_default_exclude_dirs() -> Vec<String> {
    vec![
        "target".to_string(),
        "node_modules".to_string(),
        ".git".to_string(),
        "build".to_string(),
        "dist".to_string(),
        "__pycache__".to_string(),
        ".venv".to_string(),
        "vendor".to_string(),
    ]
}