raz-core 0.2.4

Universal command generator for Rust projects - Core library with stateless file analysis and cursor-aware execution
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Complete catalog of all cargo subcommand options
//!
//! This module contains a comprehensive list of all cargo options
//! extracted from cargo help commands for intelligent parsing.

use once_cell::sync::Lazy;
use std::collections::HashMap;

/// Type of option value
#[derive(Debug, Clone, PartialEq)]
pub enum OptionValueType {
    /// No value (boolean flag)
    Flag,
    /// Single value required
    Required(String), // description of expected value
    /// Optional value
    Optional(String), // description of expected value
    /// Multiple values allowed
    Multiple(String), // description of expected value
}

/// Option metadata
#[derive(Debug, Clone)]
pub struct OptionInfo {
    /// Long form (e.g., "--release")
    pub long: &'static str,
    /// Short form if available (e.g., "-r")
    pub short: Option<&'static str>,
    /// Value type
    pub value_type: OptionValueType,
    /// Description
    pub description: &'static str,
    /// Category (Package Selection, Target Selection, etc.)
    pub category: &'static str,
}

/// Complete catalog of cargo options by subcommand
pub static CARGO_OPTIONS_CATALOG: Lazy<HashMap<&'static str, Vec<OptionInfo>>> = Lazy::new(|| {
    let mut catalog = HashMap::new();

    // Common options shared by all subcommands
    let common_options = vec![
        OptionInfo {
            long: "--message-format",
            short: None,
            value_type: OptionValueType::Required("FMT".to_string()),
            description: "Error format",
            category: "Options",
        },
        OptionInfo {
            long: "--verbose",
            short: Some("-v"),
            value_type: OptionValueType::Flag,
            description: "Use verbose output (-vv very verbose/build.rs output)",
            category: "Options",
        },
        OptionInfo {
            long: "--quiet",
            short: Some("-q"),
            value_type: OptionValueType::Flag,
            description: "Do not print cargo log messages",
            category: "Options",
        },
        OptionInfo {
            long: "--color",
            short: None,
            value_type: OptionValueType::Required("WHEN".to_string()),
            description: "Coloring: auto, always, never",
            category: "Options",
        },
        OptionInfo {
            long: "--config",
            short: None,
            value_type: OptionValueType::Required("KEY=VALUE|PATH".to_string()),
            description: "Override a configuration value",
            category: "Options",
        },
        OptionInfo {
            long: "-Z",
            short: None,
            value_type: OptionValueType::Required("FLAG".to_string()),
            description: "Unstable (nightly-only) flags",
            category: "Options",
        },
    ];

    // Package selection options
    let package_options = vec![
        OptionInfo {
            long: "--package",
            short: Some("-p"),
            value_type: OptionValueType::Optional("SPEC".to_string()),
            description: "Package to operate on",
            category: "Package Selection",
        },
        OptionInfo {
            long: "--workspace",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Operate on all packages in the workspace",
            category: "Package Selection",
        },
        OptionInfo {
            long: "--exclude",
            short: None,
            value_type: OptionValueType::Required("SPEC".to_string()),
            description: "Exclude packages from the operation",
            category: "Package Selection",
        },
        OptionInfo {
            long: "--all",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Alias for --workspace (deprecated)",
            category: "Package Selection",
        },
    ];

    // Feature selection options
    let feature_options = vec![
        OptionInfo {
            long: "--features",
            short: Some("-F"),
            value_type: OptionValueType::Required("FEATURES".to_string()),
            description: "Space or comma separated list of features to activate",
            category: "Feature Selection",
        },
        OptionInfo {
            long: "--all-features",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Activate all available features",
            category: "Feature Selection",
        },
        OptionInfo {
            long: "--no-default-features",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Do not activate the default feature",
            category: "Feature Selection",
        },
    ];

    // Compilation options
    let compilation_options = vec![
        OptionInfo {
            long: "--jobs",
            short: Some("-j"),
            value_type: OptionValueType::Required("N".to_string()),
            description: "Number of parallel jobs",
            category: "Compilation Options",
        },
        OptionInfo {
            long: "--release",
            short: Some("-r"),
            value_type: OptionValueType::Flag,
            description: "Build artifacts in release mode",
            category: "Compilation Options",
        },
        OptionInfo {
            long: "--profile",
            short: None,
            value_type: OptionValueType::Required("PROFILE-NAME".to_string()),
            description: "Build artifacts with the specified profile",
            category: "Compilation Options",
        },
        OptionInfo {
            long: "--target",
            short: None,
            value_type: OptionValueType::Optional("TRIPLE".to_string()),
            description: "Build for the target triple",
            category: "Compilation Options",
        },
        OptionInfo {
            long: "--target-dir",
            short: None,
            value_type: OptionValueType::Required("DIRECTORY".to_string()),
            description: "Directory for all generated artifacts",
            category: "Compilation Options",
        },
    ];

    // Manifest options
    let manifest_options = vec![
        OptionInfo {
            long: "--manifest-path",
            short: None,
            value_type: OptionValueType::Required("PATH".to_string()),
            description: "Path to Cargo.toml",
            category: "Manifest Options",
        },
        OptionInfo {
            long: "--locked",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Assert that Cargo.lock will remain unchanged",
            category: "Manifest Options",
        },
        OptionInfo {
            long: "--offline",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Run without accessing the network",
            category: "Manifest Options",
        },
        OptionInfo {
            long: "--frozen",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Equivalent to specifying both --locked and --offline",
            category: "Manifest Options",
        },
    ];

    // cargo run specific options
    let mut run_options = vec![];
    run_options.extend(common_options.clone());
    run_options.extend(package_options.clone());
    run_options.extend(feature_options.clone());
    run_options.extend(compilation_options.clone());
    run_options.extend(manifest_options.clone());
    run_options.extend(vec![
        OptionInfo {
            long: "--bin",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Name of the bin target to run",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--example",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Name of the example target to run",
            category: "Target Selection",
        },
    ]);
    catalog.insert("run", run_options);

    // cargo test specific options
    let mut test_options = vec![];
    test_options.extend(common_options.clone());
    test_options.extend(package_options.clone());
    test_options.extend(feature_options.clone());
    test_options.extend(compilation_options.clone());
    test_options.extend(manifest_options.clone());
    test_options.extend(vec![
        OptionInfo {
            long: "--no-run",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Compile, but don't run tests",
            category: "Options",
        },
        OptionInfo {
            long: "--no-fail-fast",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Run all tests regardless of failure",
            category: "Options",
        },
        OptionInfo {
            long: "--lib",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Test only this package's library",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bins",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Test all binaries",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bin",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Test only the specified binary",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--examples",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Test all examples",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--example",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Test only the specified example",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--tests",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Test all test targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--test",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Test only the specified test target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--benches",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Test all bench targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bench",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Test only the specified bench target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--all-targets",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Test all targets (does not include doctests)",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--doc",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Test only this library's documentation",
            category: "Target Selection",
        },
    ]);
    catalog.insert("test", test_options);

    // cargo build specific options
    let mut build_options = vec![];
    build_options.extend(common_options.clone());
    build_options.extend(package_options.clone());
    build_options.extend(feature_options.clone());
    build_options.extend(compilation_options.clone());
    build_options.extend(manifest_options.clone());
    build_options.extend(vec![
        OptionInfo {
            long: "--lib",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Build only this package's library",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bins",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Build all binaries",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bin",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Build only the specified binary",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--examples",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Build all examples",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--example",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Build only the specified example",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--tests",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Build all test targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--test",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Build only the specified test target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--benches",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Build all bench targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bench",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Build only the specified bench target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--all-targets",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Build all targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--keep-going",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Do not abort the build as soon as there is an error",
            category: "Compilation Options",
        },
    ]);
    catalog.insert("build", build_options);

    // cargo bench specific options (similar to test)
    let mut bench_options = vec![];
    bench_options.extend(common_options.clone());
    bench_options.extend(package_options.clone());
    bench_options.extend(feature_options.clone());
    bench_options.extend(compilation_options.clone());
    bench_options.extend(manifest_options.clone());
    bench_options.extend(vec![
        OptionInfo {
            long: "--no-run",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Compile, but don't run benchmarks",
            category: "Options",
        },
        OptionInfo {
            long: "--no-fail-fast",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Run all benchmarks regardless of failure",
            category: "Options",
        },
        // Target selection options same as test
        OptionInfo {
            long: "--lib",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Benchmark only this package's library",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bins",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Benchmark all binaries",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bin",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Benchmark only the specified binary",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--examples",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Benchmark all examples",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--example",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Benchmark only the specified example",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--tests",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Benchmark all test targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--test",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Benchmark only the specified test target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--benches",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Benchmark all bench targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bench",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Benchmark only the specified bench target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--all-targets",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Benchmark all targets",
            category: "Target Selection",
        },
    ]);
    catalog.insert("bench", bench_options);

    // cargo check specific options
    let mut check_options = vec![];
    check_options.extend(common_options.clone());
    check_options.extend(package_options.clone());
    check_options.extend(feature_options.clone());
    check_options.extend(compilation_options.clone());
    check_options.extend(manifest_options);
    check_options.extend(vec![
        OptionInfo {
            long: "--lib",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Check only this package's library",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bins",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Check all binaries",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bin",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Check only the specified binary",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--examples",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Check all examples",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--example",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Check only the specified example",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--tests",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Check all test targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--test",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Check only the specified test target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--benches",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Check all bench targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--bench",
            short: None,
            value_type: OptionValueType::Optional("NAME".to_string()),
            description: "Check only the specified bench target",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--all-targets",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Check all targets",
            category: "Target Selection",
        },
        OptionInfo {
            long: "--keep-going",
            short: None,
            value_type: OptionValueType::Flag,
            description: "Do not abort the build as soon as there is an error",
            category: "Compilation Options",
        },
    ]);
    catalog.insert("check", check_options);

    catalog
});

/// Check if a string is a valid cargo option for the given subcommand
pub fn is_cargo_option(subcommand: &str, option: &str) -> bool {
    if let Some(options) = CARGO_OPTIONS_CATALOG.get(subcommand) {
        options
            .iter()
            .any(|opt| opt.long == option || (opt.short == Some(option)))
    } else {
        false
    }
}

/// Get option info for a given subcommand and option
pub fn get_option_info(subcommand: &str, option: &str) -> Option<&'static OptionInfo> {
    CARGO_OPTIONS_CATALOG
        .get(subcommand)?
        .iter()
        .find(|opt| opt.long == option || (opt.short == Some(option)))
}

/// Parse an option and its value from tokens
pub fn parse_option_value(
    option: &str,
    tokens: &[String],
    index: usize,
) -> (Option<String>, usize) {
    if let Some(info) = CARGO_OPTIONS_CATALOG
        .values()
        .flatten()
        .find(|opt| opt.long == option || (opt.short == Some(option)))
    {
        match &info.value_type {
            OptionValueType::Flag => (None, 0),
            OptionValueType::Required(_) => {
                if index + 1 < tokens.len() && !tokens[index + 1].starts_with('-') {
                    (Some(tokens[index + 1].clone()), 1)
                } else {
                    // Error: required value missing
                    (None, 0)
                }
            }
            OptionValueType::Optional(_) => {
                if index + 1 < tokens.len() && !tokens[index + 1].starts_with('-') {
                    (Some(tokens[index + 1].clone()), 1)
                } else {
                    (None, 0)
                }
            }
            OptionValueType::Multiple(_) => {
                // For multiple values, take all non-option tokens
                let mut values = vec![];
                let mut consumed = 0;
                for token in tokens.iter().skip(index + 1) {
                    if token.starts_with('-') {
                        break;
                    }
                    values.push(token.clone());
                    consumed += 1;
                }
                if values.is_empty() {
                    (None, 0)
                } else {
                    (Some(values.join(" ")), consumed)
                }
            }
        }
    } else {
        (None, 0)
    }
}