dynamic-cli 0.2.0

A framework for building configurable CLI and REPL applications from YAML/JSON configuration files
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
//! Command-line and REPL parsing
//!
//! This module provides comprehensive parsing functionality for both
//! traditional command-line interfaces (CLI) and interactive REPL mode.
//!
//! # Module Structure
//!
//! The parser module consists of three main components:
//!
//! - [`type_parser`]: Type conversion functions (string → typed values)
//! - [`cli_parser`]: CLI argument parser (Unix-style options)
//! - [`repl_parser`]: REPL line parser (interactive mode)
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────┐
//! │         User Input                      │
//! │   "process file.txt --verbose"          │
//! └──────────────┬──────────────────────────┘
//!//!//! ┌─────────────────────────────────────────┐
//! │       ReplParser (REPL mode)            │
//! │   - Tokenize line                       │
//! │   - Resolve command name via Registry   │
//! │   - Delegate to CliParser               │
//! └──────────────┬──────────────────────────┘
//!//!//! ┌─────────────────────────────────────────┐
//! │       CliParser (CLI mode)              │
//! │   - Parse positional arguments          │
//! │   - Parse options (-v, --verbose)       │
//! │   - Apply defaults                      │
//! │   - Use TypeParser for conversion       │
//! └──────────────┬──────────────────────────┘
//!//!//! ┌─────────────────────────────────────────┐
//! │       TypeParser                        │
//! │   - Convert strings to typed values     │
//! │   - Validate type constraints           │
//! └──────────────┬──────────────────────────┘
//!//!//! ┌─────────────────────────────────────────┐
//! │   HashMap<String, String>               │
//! │   {"input": "file.txt",                 │
//! │    "verbose": "true"}                   │
//! └─────────────────────────────────────────┘
//! ```
//!
//! # Design Principles
//!
//! ## 1. Separation of Concerns
//!
//! Each parser has a specific responsibility:
//! - **TypeParser**: Handles type conversion only
//! - **CliParser**: Handles CLI syntax (options, arguments)
//! - **ReplParser**: Handles REPL-specific concerns (tokenization, command resolution)
//!
//! ## 2. Composability
//!
//! Parsers compose naturally:
//! - ReplParser uses CliParser for argument parsing
//! - CliParser uses TypeParser for type conversion
//! - Each can be used independently when needed
//!
//! ## 3. Error Clarity
//!
//! All parsers provide detailed error messages with:
//! - Clear descriptions of what went wrong
//! - Suggestions for typos (via Levenshtein distance)
//! - Hints for correct usage
//!
//! # Usage Examples
//!
//! ## CLI Mode (Direct Argument Parsing)
//!
//! ```
//! use dynamic_cli::parser::cli_parser::CliParser;
//! use dynamic_cli::config::schema::{CommandDefinition, ArgumentDefinition, ArgumentType};
//!
//! let definition = CommandDefinition {
//!     name: "process".to_string(),
//!     aliases: vec![],
//!     description: "Process files".to_string(),
//!     required: false,
//!     arguments: vec![
//!         ArgumentDefinition {
//!             name: "input".to_string(),
//!             arg_type: ArgumentType::Path,
//!             required: true,
//!             description: "Input file".to_string(),
//!             validation: vec![],
//!         }
//!     ],
//!     options: vec![],
//!     implementation: "handler".to_string(),
//! };
//!
//! let parser = CliParser::new(&definition);
//! let args = vec!["input.txt".to_string()];
//! let parsed = parser.parse(&args).unwrap();
//!
//! assert_eq!(parsed.get("input"), Some(&"input.txt".to_string()));
//! ```
//!
//! ## REPL Mode (Interactive Parsing)
//!
//! ```no_run
//! use dynamic_cli::parser::repl_parser::ReplParser;
//! use dynamic_cli::registry::CommandRegistry;
//!
//! let registry = CommandRegistry::new();
//! // ... register commands ...
//!
//! let parser = ReplParser::new(&registry);
//!
//! // Parse user input
//! let line = "process input.txt --verbose";
//! let parsed = parser.parse_line(line).unwrap();
//!
//! println!("Command: {}", parsed.command_name);
//! println!("Arguments: {:?}", parsed.arguments);
//! ```
//!
//! ## Type Parsing (Low-Level)
//!
//! ```
//! use dynamic_cli::parser::type_parser::{parse_integer, parse_bool};
//!
//! let number = parse_integer("42").unwrap();
//! assert_eq!(number, 42);
//!
//! let flag = parse_bool("yes").unwrap();
//! assert_eq!(flag, true);
//! ```
//!
//! # Error Handling
//!
//! All parsing functions return [`Result<T>`] where errors are instances
//! of [`ParseError`]. Common error scenarios:
//!
//! - **Unknown command**: User typed a non-existent command
//!   ```text
//!   Error: Unknown command: 'simulat'
//!   ? Did you mean:
//!     • simulate
//!     • validation
//!   ```
//!
//! - **Type mismatch**: Value cannot be converted to expected type
//!   ```text
//!   Error: Failed to parse count as integer: 'abc'
//!   ```
//!
//! - **Missing argument**: Required argument not provided
//!   ```text
//!   Error: Missing required argument: input for command 'process'
//!   ```
//!
//! # Performance Considerations
//!
//! - **Type parsing**: O(1) for most types, O(n) for string length
//! - **CLI parsing**: O(n) where n = number of arguments
//! - **REPL parsing**: O(m + n) where m = line length (tokenization), n = arguments
//! - **Command resolution**: O(1) via HashMap lookup in registry
//!
//! # Thread Safety
//!
//! All parsers are:
//! - **Stateless**: Can be used concurrently from multiple threads
//! - **Borrowing**: Use references to definitions/registry (no ownership)
//! - **Reusable**: Can parse multiple commands with the same parser instance
//!
//! # Future Extensions
//!
//! Potential enhancements for future versions:
//! - Support for subcommands (e.g., `git commit`)
//! - Environment variable expansion
//! - Glob pattern matching for paths
//! - Command history and auto-completion hints
//! - Streaming parser for very large inputs

#[allow(unused_imports)]
use crate::error::Result;

// Public submodules
pub mod cli_parser;
pub mod repl_parser;
pub mod type_parser;

// Re-export commonly used types
pub use cli_parser::CliParser;
pub use repl_parser::{ParsedCommand, ReplParser};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::schema::{
        ArgumentDefinition, ArgumentType, CommandDefinition, OptionDefinition,
    };
    use crate::context::ExecutionContext;
    use crate::executor::CommandHandler;
    use crate::registry::CommandRegistry;
    use std::collections::HashMap;

    // Dummy handler for integration tests
    struct IntegrationTestHandler;

    impl CommandHandler for IntegrationTestHandler {
        fn execute(
            &self,
            _context: &mut dyn ExecutionContext,
            _args: &HashMap<String, String>,
        ) -> Result<()> {
            Ok(())
        }
    }

    /// Helper to create a comprehensive test command
    fn create_comprehensive_command() -> CommandDefinition {
        CommandDefinition {
            name: "analyze".to_string(),
            aliases: vec!["analyse".to_string(), "check".to_string()],
            description: "Analyze data files".to_string(),
            required: false,
            arguments: vec![
                ArgumentDefinition {
                    name: "input".to_string(),
                    arg_type: ArgumentType::Path,
                    required: true,
                    description: "Input data file".to_string(),
                    validation: vec![],
                },
                ArgumentDefinition {
                    name: "output".to_string(),
                    arg_type: ArgumentType::Path,
                    required: false,
                    description: "Output report file".to_string(),
                    validation: vec![],
                },
            ],
            options: vec![
                OptionDefinition {
                    name: "verbose".to_string(),
                    short: Some("v".to_string()),
                    long: Some("verbose".to_string()),
                    option_type: ArgumentType::Bool,
                    required: false,
                    default: Some("false".to_string()),
                    description: "Enable verbose output".to_string(),
                    choices: vec![],
                },
                OptionDefinition {
                    name: "iterations".to_string(),
                    short: Some("i".to_string()),
                    long: Some("iterations".to_string()),
                    option_type: ArgumentType::Integer,
                    required: false,
                    default: Some("100".to_string()),
                    description: "Number of iterations".to_string(),
                    choices: vec![],
                },
                OptionDefinition {
                    name: "threshold".to_string(),
                    short: Some("t".to_string()),
                    long: Some("threshold".to_string()),
                    option_type: ArgumentType::Float,
                    required: false,
                    default: Some("0.5".to_string()),
                    description: "Analysis threshold".to_string(),
                    choices: vec![],
                },
            ],
            implementation: "analyze_handler".to_string(),
        }
    }

    // ========================================================================
    // Integration tests: CLI Parser
    // ========================================================================

    #[test]
    fn test_cli_parser_integration_minimal() {
        let definition = create_comprehensive_command();
        let parser = CliParser::new(&definition);

        let args = vec!["data.csv".to_string()];
        let result = parser.parse(&args).unwrap();

        // Required argument
        assert_eq!(result.get("input"), Some(&"data.csv".to_string()));

        // Defaults should be applied
        assert_eq!(result.get("verbose"), Some(&"false".to_string()));
        assert_eq!(result.get("iterations"), Some(&"100".to_string()));
        assert_eq!(result.get("threshold"), Some(&"0.5".to_string()));
    }

    #[test]
    fn test_cli_parser_integration_full() {
        let definition = create_comprehensive_command();
        let parser = CliParser::new(&definition);

        let args = vec![
            "data.csv".to_string(),
            "report.txt".to_string(),
            "--verbose".to_string(),
            "--iterations=200".to_string(),
            "-t".to_string(),
            "0.75".to_string(),
        ];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("input"), Some(&"data.csv".to_string()));
        assert_eq!(result.get("output"), Some(&"report.txt".to_string()));
        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
        assert_eq!(result.get("iterations"), Some(&"200".to_string()));
        assert_eq!(result.get("threshold"), Some(&"0.75".to_string()));
    }

    #[test]
    fn test_cli_parser_integration_mixed_options() {
        let definition = create_comprehensive_command();
        let parser = CliParser::new(&definition);

        // Options can be interspersed with positional arguments
        let args = vec![
            "--verbose".to_string(),
            "data.csv".to_string(),
            "-i200".to_string(),
            "report.txt".to_string(),
            "--threshold".to_string(),
            "0.9".to_string(),
        ];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("input"), Some(&"data.csv".to_string()));
        assert_eq!(result.get("output"), Some(&"report.txt".to_string()));
        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
        assert_eq!(result.get("iterations"), Some(&"200".to_string()));
        assert_eq!(result.get("threshold"), Some(&"0.9".to_string()));
    }

    // ========================================================================
    // Integration tests: REPL Parser
    // ========================================================================

    #[test]
    fn test_repl_parser_integration_simple() {
        let mut registry = CommandRegistry::new();
        let definition = create_comprehensive_command();
        registry
            .register(definition, Box::new(IntegrationTestHandler))
            .unwrap();

        let parser = ReplParser::new(&registry);

        let parsed = parser.parse_line("analyze data.csv").unwrap();
        assert_eq!(parsed.command_name, "analyze");
        assert_eq!(parsed.arguments.get("input"), Some(&"data.csv".to_string()));
    }

    #[test]
    fn test_repl_parser_integration_alias() {
        let mut registry = CommandRegistry::new();
        let definition = create_comprehensive_command();
        registry
            .register(definition, Box::new(IntegrationTestHandler))
            .unwrap();

        let parser = ReplParser::new(&registry);

        // Use alias instead of command name
        let parsed = parser.parse_line("check data.csv --verbose").unwrap();
        assert_eq!(parsed.command_name, "analyze"); // Resolves to canonical name
        assert_eq!(parsed.arguments.get("input"), Some(&"data.csv".to_string()));
        assert_eq!(parsed.arguments.get("verbose"), Some(&"true".to_string()));
    }

    #[test]
    fn test_repl_parser_integration_quoted_paths() {
        let mut registry = CommandRegistry::new();
        let definition = create_comprehensive_command();
        registry
            .register(definition, Box::new(IntegrationTestHandler))
            .unwrap();

        let parser = ReplParser::new(&registry);

        let parsed = parser
            .parse_line(r#"analyze "/path/with spaces/data.csv" "output report.txt""#)
            .unwrap();

        assert_eq!(
            parsed.arguments.get("input"),
            Some(&"/path/with spaces/data.csv".to_string())
        );
        assert_eq!(
            parsed.arguments.get("output"),
            Some(&"output report.txt".to_string())
        );
    }

    #[test]
    fn test_repl_parser_integration_complex() {
        let mut registry = CommandRegistry::new();
        let definition = create_comprehensive_command();
        registry
            .register(definition, Box::new(IntegrationTestHandler))
            .unwrap();

        let parser = ReplParser::new(&registry);

        let parsed = parser
            .parse_line(r#"analyse "data file.csv" report.txt -v --iterations=500 -t 0.95"#)
            .unwrap();

        assert_eq!(parsed.command_name, "analyze");
        assert_eq!(
            parsed.arguments.get("input"),
            Some(&"data file.csv".to_string())
        );
        assert_eq!(
            parsed.arguments.get("output"),
            Some(&"report.txt".to_string())
        );
        assert_eq!(parsed.arguments.get("verbose"), Some(&"true".to_string()));
        assert_eq!(parsed.arguments.get("iterations"), Some(&"500".to_string()));
        assert_eq!(parsed.arguments.get("threshold"), Some(&"0.95".to_string()));
    }

    // ========================================================================
    // Integration tests: Type Parser
    // ========================================================================

    #[test]
    fn test_type_parser_integration_all_types() {
        use type_parser::parse_value;

        // Test all argument types
        assert!(parse_value("hello", ArgumentType::String).is_ok());
        assert!(parse_value("42", ArgumentType::Integer).is_ok());
        assert!(parse_value("3.14", ArgumentType::Float).is_ok());
        assert!(parse_value("true", ArgumentType::Bool).is_ok());
        assert!(parse_value("/path/to/file", ArgumentType::Path).is_ok());
    }

    #[test]
    fn test_type_parser_integration_error_propagation() {
        let definition = create_comprehensive_command();
        let parser = CliParser::new(&definition);

        // Invalid integer should fail
        let args = vec![
            "data.csv".to_string(),
            "--iterations".to_string(),
            "not_a_number".to_string(),
        ];

        let result = parser.parse(&args);
        assert!(result.is_err());
    }

    // ========================================================================
    // Integration tests: End-to-End Workflows
    // ========================================================================

    #[test]
    fn test_workflow_cli_to_execution() {
        // Simulate: User provides CLI args → Parser → Handler could execute

        let definition = create_comprehensive_command();
        let parser = CliParser::new(&definition);

        let args = vec!["data.csv".to_string(), "-v".to_string()];
        let parsed = parser.parse(&args).unwrap();

        // Verify parsed data is ready for execution
        assert!(parsed.contains_key("input"));
        assert!(parsed.contains_key("verbose"));
        assert_eq!(parsed.get("verbose"), Some(&"true".to_string()));
    }

    #[test]
    fn test_workflow_repl_to_execution() {
        // Simulate: User types in REPL → Parser → Handler could execute

        let mut registry = CommandRegistry::new();
        let definition = create_comprehensive_command();
        registry
            .register(definition, Box::new(IntegrationTestHandler))
            .unwrap();

        let parser = ReplParser::new(&registry);

        let line = "analyze data.csv --verbose --iterations=1000";
        let parsed = parser.parse_line(line).unwrap();

        // Verify parsed command is ready for execution
        assert_eq!(parsed.command_name, "analyze");
        assert!(parsed.arguments.contains_key("input"));
        assert_eq!(parsed.arguments.get("verbose"), Some(&"true".to_string()));
        assert_eq!(
            parsed.arguments.get("iterations"),
            Some(&"1000".to_string())
        );
    }

    #[test]
    fn test_workflow_typo_suggestions() {
        let mut registry = CommandRegistry::new();
        let definition = create_comprehensive_command();
        registry
            .register(definition, Box::new(IntegrationTestHandler))
            .unwrap();

        let parser = ReplParser::new(&registry);

        // User makes a typo
        let result = parser.parse_line("analyz data.csv");

        assert!(result.is_err());

        // Error should contain suggestions
        let error = result.unwrap_err();
        let error_msg = format!("{}", error);
        assert!(error_msg.contains("Unknown command"));
    }

    // ========================================================================
    // Re-export verification tests
    // ========================================================================

    #[test]
    fn test_reexports_accessible() {
        // Verify that re-exported types are accessible from module root

        let definition = create_comprehensive_command();

        // CliParser should be accessible
        let _cli_parser = CliParser::new(&definition);

        // ReplParser should be accessible (needs registry)
        let registry = CommandRegistry::new();
        let _repl_parser = ReplParser::new(&registry);

        // ParsedCommand should be accessible
        let _parsed = ParsedCommand {
            command_name: "test".to_string(),
            arguments: HashMap::new(),
        };
    }
}