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
//! User interface module
//!
//! This module provides two main interfaces for interacting with the CLI framework:
//!
//! - [`CliInterface`]: One-shot command execution from command-line arguments
//! - [`ReplInterface`]: Interactive REPL (Read-Eval-Print Loop) with history
//!
//! # Overview
//!
//! The `interface` module is the user-facing layer of the framework. It handles:
//! - Parsing user input (CLI args or REPL lines)
//! - Executing commands through the registry
//! - Displaying results and errors
//! - Managing command history (REPL only)
//!
//! # Choosing an Interface
//!
//! ## CLI Interface
//!
//! Use [`CliInterface`] when:
//! - Running single commands from scripts
//! - Building traditional CLI tools
//! - No interaction is needed
//! - Each invocation is independent
//!
//! ```no_run
//! use dynamic_cli::interface::CliInterface;
//! use dynamic_cli::prelude::*;
//!
//! # #[derive(Default)]
//! # struct MyContext;
//! # impl ExecutionContext for MyContext {
//! # fn as_any(&self) -> &dyn std::any::Any { self }
//! # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
//! # }
//! # fn main() -> dynamic_cli::Result<()> {
//! let registry = CommandRegistry::new();
//! let context = Box::new(MyContext::default());
//!
//! let cli = CliInterface::new(registry, context);
//! cli.run(std::env::args().skip(1).collect())?;
//! # Ok(())
//! # }
//! ```
//!
//! ## REPL Interface
//!
//! Use [`ReplInterface`] when:
//! - Building interactive tools
//! - Users need to run multiple commands
//! - Context/state is preserved between commands
//! - Command history and line editing are desired
//!
//! ```no_run
//! use dynamic_cli::interface::ReplInterface;
//! use dynamic_cli::prelude::*;
//!
//! # #[derive(Default)]
//! # struct MyContext;
//! # impl ExecutionContext for MyContext {
//! # fn as_any(&self) -> &dyn std::any::Any { self }
//! # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
//! # }
//! # fn main() -> dynamic_cli::Result<()> {
//! let registry = CommandRegistry::new();
//! let context = Box::new(MyContext::default());
//!
//! let repl = ReplInterface::new(registry, context, "myapp".to_string(), None, None)?;
//! repl.run()?; // Enters interactive loop
//! # Ok(())
//! # }
//! ```
//!
//! # Architecture
//!
//! Both interfaces follow the same flow:
//!
//! ```text
//! User Input → Parser → Validator → Executor → Handler
//! ↓
//! ExecutionContext
//! ```
//!
//! **Key differences**:
//!
//! | Aspect | CLI | REPL |
//! |--------|-----|------|
//! | Input | Command-line args | Interactive lines |
//! | Parser | [`CliParser`] | [`ReplParser`] |
//! | History | None | Written to disk by `:quit` and Ctrl-D |
//! | Errors | Exit process | Display and continue |
//! | Lifecycle | One command, exit | Loop until `:quit`, `:exit` or Ctrl-D |
//!
//! # Error Handling
//!
//! ## CLI Interface
//!
//! Errors cause the process to exit with specific codes:
//! - `0`: Success
//! - `1`: Execution error
//! - `2`: Parse/validation error
//! - `3`: Other errors
//!
//! ## REPL Interface
//!
//! Errors are displayed but the REPL continues:
//! - Parse errors → show suggestions, continue
//! - Validation errors → explain issue, continue
//! - Execution errors → display error, continue
//! - Directive errors → list the available directives (unknown directive)
//! or show the expected usage (wrong arguments), continue
//! - Input failure (the terminal cannot be read) → reported, the REPL exits
//! without writing the history
//!
//! # REPL Directives
//!
//! In the REPL, a line starting with `:` is a framework directive; any
//! other line is an application command. The two never compete for a name:
//! `:quit` is always the directive, `quit` is always looked up among the
//! application's commands.
//!
//! | Directive | Aliases | Action |
//! |--------------------|------------|---------------------------------------------|
//! | `:help [command]` | `:h`, `:?` | Application help, or the help of `command` |
//! | `:load <path>` | | Run every line of a script file |
//! | `:quit` | `:q` | Write the session history, then leave |
//! | `:exit` | | Leave without writing the session history |
//!
//! These are the default names. An application can rename a directive,
//! replace its aliases or its description through the `directives:` section
//! of its configuration, for instance to translate them; it can neither add
//! nor disable one. See [`crate::config::directive`].
//!
//! Leaving the REPL:
//!
//! - `:quit` and end of input (Ctrl-D) write the session history, then
//! leave.
//! - `:exit` leaves without writing it. Any other termination (a handler
//! calling `std::process::exit`, a panic, the process being killed) also
//! leaves the history file as it was when the session started.
//! - `exit` or `quit` typed without `:` is an ordinary command name. When
//! the application has no command of that name, the error suggests the
//! matching directive (`:quit`).
//!
//! Inside a `:load` script, `:quit` and `:exit` are reported and skipped;
//! the other lines run.
//!
//! `:help` lists the directives after the application help, under their
//! effective names (see
//! [`HelpFormatter::format_directives`](crate::help::HelpFormatter::format_directives)).
//! Tab completion after a leading `:` offers the directive names and
//! aliases. `--help` and `-h` work in the REPL as in CLI mode.
//!
//! # Examples
//!
//! ## Complete CLI Application
//!
//! ```no_run
//! use dynamic_cli::prelude::*;
//! use dynamic_cli::config::loader::load_config;
//! use dynamic_cli::interface::CliInterface;
//!
//! // Define context
//! #[derive(Default)]
//! struct AppContext {
//! data: Vec<String>,
//! }
//!
//! impl ExecutionContext for AppContext {
//! fn as_any(&self) -> &dyn std::any::Any { self }
//! fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
//! }
//!
//! // Define handler
//! struct AddCommand;
//!
//! impl CommandHandler for AddCommand {
//! fn execute(
//! &self,
//! context: &mut dyn ExecutionContext,
//! args: &ParsedArgs,
//! ) -> dynamic_cli::Result<()> {
//! let ctx = dynamic_cli::context::downcast_mut::<AppContext>(context).unwrap();
//! let item = args.get_scalar("item").unwrap();
//! ctx.data.push(item.to_string());
//! println!("Added: {}", item);
//! Ok(())
//! }
//! }
//!
//! fn main() -> dynamic_cli::Result<()> {
//! // Load configuration
//! let config = load_config("commands.yaml")?;
//!
//! // Build registry
//! let mut registry = CommandRegistry::new();
//! registry.register_sync(
//! config.commands[0].clone(),
//! Box::new(AddCommand),
//! )?;
//!
//! // Create and run CLI
//! let context = Box::new(AppContext::default());
//! let cli = CliInterface::new(registry, context);
//! cli.run(std::env::args().skip(1).collect())
//! }
//! ```
//!
//! ## Complete REPL Application
//!
//! ```no_run
//! use dynamic_cli::prelude::*;
//! use dynamic_cli::config::loader::load_config;
//! use dynamic_cli::interface::ReplInterface;
//!
//! // Same context and handler as above
//! # #[derive(Default)]
//! # struct AppContext { data: Vec<String> }
//! # impl ExecutionContext for AppContext {
//! # fn as_any(&self) -> &dyn std::any::Any { self }
//! # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
//! # }
//! # struct AddCommand;
//! # impl CommandHandler for AddCommand {
//! # fn execute(&self, context: &mut dyn ExecutionContext, args: &ParsedArgs) -> dynamic_cli::Result<()> {
//! # let ctx = dynamic_cli::context::downcast_mut::<AppContext>(context).unwrap();
//! # let item = args.get_scalar("item").unwrap();
//! # ctx.data.push(item.to_string());
//! # println!("Added: {}", item);
//! # Ok(())
//! # }
//! # }
//!
//! fn main() -> dynamic_cli::Result<()> {
//! // Load configuration
//! let config = load_config("commands.yaml")?;
//!
//! // Build registry
//! let mut registry = CommandRegistry::new();
//! registry.register_sync(
//! config.commands[0].clone(),
//! Box::new(AddCommand),
//! )?;
//!
//! // Create and run REPL
//! let context = Box::new(AppContext::default());
//! let repl = ReplInterface::new(registry, context, "myapp".to_string(), None, None)?;
//! repl.run() // Interactive loop
//! }
//! ```
//!
//! # Module Structure
//!
//! - [`cli`]: CLI interface implementation
//! - [`repl`]: REPL interface implementation
//!
//! [`CliParser`]: crate::parser::CliParser
//! [`ReplParser`]: crate::parser::ReplParser
// Re-export main types for convenience
pub use ;
pub use ReplInterface;