fli 1.2.2

The commander.js like CLI Parser for rust
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
use std::default;

use super::parse_state::ParseState;
use super::value_types::{Value, ValueTypes};
use crate::command::FliCommand;
use crate::error::{FliError, Result};

/// Represents elements in the parsed command chain.
///
/// Each element describes what was encountered during parsing:
/// - A subcommand to execute
/// - An option with its value
/// - A positional argument
/// - A preserved option that triggers immediate callback
#[derive(Debug, Clone)]
pub enum CommandChain {
    /// A subcommand was encountered
    SubCommand(String),
    /// An option with its parsed value
    Option(String, ValueTypes),
    /// A positional argument
    Argument(String),
    /// A preserved option that should trigger immediate callback
    IsPreservedOption(String),
}

/// Parses command-line arguments into a structured command chain.
///
/// This is the core parsing engine that:
/// 1. Tokenizes raw arguments
/// 2. Identifies commands, options, and values
/// 3. Validates against expected option types
/// 4. Builds a chain representing the parse tree
///
/// # Examples
///
/// ```rust
/// let args = vec!["copy".to_string(), "-s".to_string(),
///                 "file1.txt".to_string(), "file2.txt".to_string()];
/// let mut parser = InputArgsParser::new("copy".to_string(), args);
/// parser.prepare(&mut command)?;
/// ```
#[derive(Debug, Clone)]
pub struct InputArgsParser {
    pub command: String,
    pub args: Vec<String>,
    pub command_chain: Vec<CommandChain>,
    is_prepared: bool,
}

impl InputArgsParser {
    /// Creates a new argument parser.
    ///
    /// # Arguments
    ///
    /// * `command` - The command name being parsed
    /// * `args` - The raw arguments (without program name)
    ///
    /// # Returns
    ///
    /// An unprepared parser (call `prepare()` before use)
    pub fn new(command: String, args: Vec<String>) -> Self {
        Self {
            command,
            args,
            command_chain: Vec::new(),
            is_prepared: false,
        }
    }

    /// Returns the parsed command chain.
    ///
    /// # Returns
    ///
    /// Reference to the vector of parsed `CommandChain` elements
    ///
    /// # Note
    ///
    /// Only valid after `prepare()` has been called.
    pub fn get_parsed_commands_chain(&self) -> &Vec<CommandChain> {
        &self.command_chain
    }

    /// Parses arguments and validates them against the command definition.
    ///
    /// This is the main parsing method that:
    /// 1. Validates command name matches
    /// 2. Iterates through arguments
    /// 3. Handles state transitions (option -> value -> next option)
    /// 4. Validates required values are provided
    /// 5. Handles special cases like "--" and preserved options
    ///
    /// # Arguments
    ///
    /// * `command` - The command definition with expected options
    ///
    /// # Returns
    ///
    /// * `Ok(&mut Self)` - If parsing succeeded
    /// * `Err(String)` - If parsing failed with error description
    ///
    /// # Errors
    ///
    /// Returns errors for:
    /// - Command name mismatch
    /// - Missing required option values
    /// - Unexpected "--" placement
    /// - Invalid option-value combinations
    /// - Unknown options or subcommands
    ///
    /// # State Machine
    ///
    /// Uses `ParseState` to track current parsing context:
    /// - `Start` -> `InCommand`: After verifying command name
    /// - `InCommand` -> `InOption`: When encountering an option flag
    /// - `InOption` -> `AcceptingValue`: For options that need values
    /// - `AcceptingValue` -> `InOption`: After consuming value(s)
    /// - Any -> `Breaking`: When encountering "--"
    /// - Any -> `End`: When parsing completes
    pub fn prepare(&mut self, command: &mut FliCommand) -> Result<&mut Self> {
        if self.is_prepared {
            // println!("Parser is already prepared");
            return Ok(self);
        }
        let mut state = ParseState::Start;

        // Verify command name matches
        if self.command != *command.get_name() {
            return Err(FliError::command_mismatch(
                command.get_name(),
                &self.command,
            ));
        }

        // Move to InCommand state after verifying command
        state.set_next_mode(ParseState::InCommand)?;

        let mut i = 0;
        while i < self.args.len() {
            let arg = &self.args[i].clone();

            // Handle the break symbol "--"
            if arg == "--" {
                match state {
                    ParseState::InOption | ParseState::AcceptingValue(_, _) => {
                        state.set_next_mode(ParseState::Breaking)?;
                        i += 1;
                        continue;
                    }
                    _ => {
                        return Err(FliError::UnexpectedToken {
                            token: "--".to_string(),
                            position: i,
                        });
                    }
                }
            }

            // If we're in Breaking state, everything after is an argument
            if matches!(state, ParseState::Breaking) {
                self.command_chain
                    .push(CommandChain::Argument(arg.to_string()));
                state.set_next_mode(ParseState::InArgument)?;
                i += 1;
                continue;
            }

            // Handle AcceptingValue state
            if let ParseState::AcceptingValue(option_name, expected_value_type) = &state {
                match expected_value_type {
                    ValueTypes::RequiredSingle(default) => {
                        // Check if next arg is another option (error case)
                        if command.get_option_parser().has_option(arg) {
                            return Err(FliError::missing_value(option_name));
                        }

                        let value = default.clone().replace_with_expected_value(arg)?;
                        // Assign the value
                        self.command_chain.push(CommandChain::Option(
                            option_name.clone(),
                            ValueTypes::RequiredSingle(value.clone()),
                        ));
                        command
                            .get_option_parser()
                            .update_option_value(option_name, ValueTypes::RequiredSingle(value))?;
                        state.set_next_mode(ParseState::InOption)?;
                        i += 1;
                        continue;
                    }
                    ValueTypes::OptionalSingle(default) => {
                        // If next arg is an option, don't consume it as value
                        if command.get_option_parser().has_option(arg) {
                            self.command_chain.push(CommandChain::Option(
                                option_name.clone(),
                                ValueTypes::OptionalSingle(None),
                            ));
                            state.set_next_mode(ParseState::InOption)?;
                            continue; // Don't increment i, process this arg as option
                        }

                        let value = default
                            .clone()
                            .unwrap_or(Value::Str(String::new()))
                            .replace_with_expected_value(arg)?;

                        // Otherwise, consume as value
                        self.command_chain.push(CommandChain::Option(
                            option_name.clone(),
                            ValueTypes::OptionalSingle(Some(value.clone())),
                        ));
                        command.get_option_parser().update_option_value(
                            option_name,
                            ValueTypes::OptionalSingle(Some(value)),
                        )?;
                        state.set_next_mode(ParseState::InOption)?;
                        i += 1;
                        continue;
                    }
                    ValueTypes::RequiredMultiple(default, expected_count) => {
                        let mut values = Vec::new();
                        let max_count = expected_count.unwrap_or(usize::MAX);

                        // Collect values until we hit an option or reach max count
                        while i < self.args.len() && values.len() < max_count {
                            let current_arg = &self.args[i];

                            if command.get_option_parser().has_option(current_arg) {
                                break;
                            }

                            if current_arg == "--" {
                                break;
                            }

                            let current_value = default
                                .get(values.len())
                                .cloned()
                                .unwrap_or(Value::Str(String::new()))
                                .replace_with_expected_value(current_arg)?;

                            values.push(current_value);
                            i += 1;
                        }

                        // Validate we got at least one value
                        if values.is_empty() {
                            return Err(FliError::missing_value(option_name));
                        }

                        // Validate expected count if specified
                        if let Some(expected) = expected_count {
                            if values.len() != *expected {
                                return Err(FliError::missing_value(option_name));
                            }
                        }

                        self.command_chain.push(CommandChain::Option(
                            option_name.clone(),
                            ValueTypes::RequiredMultiple(values.clone(), *expected_count),
                        ));
                        command.get_option_parser().update_option_value(
                            option_name,
                            ValueTypes::RequiredMultiple(values, *expected_count),
                        )?;
                        state.set_next_mode(ParseState::InOption)?;
                        continue; // Don't increment i again, it's already advanced
                    }
                    ValueTypes::OptionalMultiple(default, expected_count) => {
                        let mut values = Vec::new();
                        let max_count = expected_count.unwrap_or(usize::MAX);

                        // Collect values until we hit an option or reach max count
                        while i < self.args.len() && values.len() < max_count {
                            let current_arg = &self.args[i];

                            if command.get_option_parser().has_option(current_arg) {
                                break;
                            }

                            if current_arg == "--" {
                                break;
                            }

                            let current_value = if default.is_some() {
                                default
                                    .as_ref()
                                    .unwrap()
                                    .get(values.len())
                                    .cloned()
                                    .unwrap_or(Value::Str(String::new()))
                                    .replace_with_expected_value(current_arg)?
                            } else {
                                Value::Str(String::new())
                                    .replace_with_expected_value(current_arg)?
                            };

                            values.push(current_value);
                            i += 1;
                        }

                        let option_value = if values.is_empty() {
                            ValueTypes::OptionalMultiple(None, *expected_count)
                        } else {
                            ValueTypes::OptionalMultiple(Some(values), *expected_count)
                        };

                        self.command_chain.push(CommandChain::Option(
                            option_name.clone(),
                            option_value.clone(),
                        ));
                        command
                            .get_option_parser()
                            .update_option_value(option_name, option_value)?;
                        state.set_next_mode(ParseState::InOption)?;
                        continue; // Don't increment i again
                    }
                }
            }

            if let Some(preserved_option) = command.get_preserved_option(arg) {
                // It's a preserved option
                self.command_chain
                    .push(CommandChain::IsPreservedOption(arg.to_string()));
                state.set_next_mode(ParseState::InOption)?;
                i += 1;
                continue;
            }

            // Check if current arg is an option
            let option_parser = command.get_option_parser();
            if option_parser.has_option(arg) {
                let expected_value_type = option_parser
                    .get_option_expected_value_type(arg)
                    .ok_or_else(|| FliError::OptionNotFound(arg.to_string()))?;

                match expected_value_type {
                    ValueTypes::OptionalSingle(Some(Value::Bool(_))) => {
                        // Flag option - set to Bool(true) when encountered, NO value expected
                        let flag_value = ValueTypes::OptionalSingle(Some(Value::Bool(true)));
                        self.command_chain
                            .push(CommandChain::Option(arg.to_string(), flag_value.clone()));
                        command
                            .get_option_parser()
                            .update_option_value(arg, flag_value)?;
                        state.set_next_mode(ParseState::InOption)?;
                    }
                    _ => {
                        // Option requires value(s), transition to AcceptingValue
                        state.set_next_mode(ParseState::InOption)?;
                        state.set_next_mode(ParseState::AcceptingValue(
                            arg.to_string(),
                            expected_value_type.clone(),
                        ))?;
                    }
                }
                i += 1;
                continue;
            }

            // Check if it's a subcommand
            if let Some(command) = command.get_sub_command_mut(arg) {
                // self.command_chain
                //     .push(CommandChain::SubCommand(arg.to_string()));
                // state.set_next_mode(ParseState::InCommand)?;
                // i += 1;
                // continue;
                // if a sub command update the self.args to be the remaining args, then return self.prepare and pass the sub command
                self.command_chain
                    .push(CommandChain::SubCommand(arg.to_string()));
                let remaining_args = self.args[i + 1..].to_vec();
                self.command = arg.to_string();
                self.args = remaining_args;
                return self.prepare(command);
            }

            if command.get_expected_positional_args() <= 0
                && (matches!(state, ParseState::Start | ParseState::InCommand)
                    || matches!(self.command_chain.last(), Some(CommandChain::SubCommand(_))))
            {
                let available: Vec<String> = command.get_sub_commands().keys().cloned().collect();
                return Err(FliError::UnknownCommand(arg.to_string(), available));
            }

            // Otherwise, it's an argument
            self.command_chain
                .push(CommandChain::Argument(arg.to_string()));
            state.set_next_mode(ParseState::InArgument)?;
            i += 1;
        }

        // Final validation - check if we're expecting a required value
        if let ParseState::AcceptingValue(option_name, value_type) = &state {
            match value_type {
                ValueTypes::RequiredSingle(_) | ValueTypes::RequiredMultiple(_, _) => {
                    return Err(FliError::missing_value(option_name));
                }
                ValueTypes::OptionalSingle(_) | ValueTypes::OptionalMultiple(_, _) => {
                    // It's optional, add it with None
                    self.command_chain.push(CommandChain::Option(
                        option_name.clone(),
                        match value_type {
                            ValueTypes::OptionalSingle(_) => ValueTypes::OptionalSingle(None),
                            ValueTypes::OptionalMultiple(_, count) => {
                                ValueTypes::OptionalMultiple(None, *count)
                            }
                            _ => unreachable!(),
                        },
                    ));
                }
                _ => {}
            }
        }

        state.set_next_mode(ParseState::End)?;
        self.is_prepared = true;
        Ok(self)
    }

    /// Creates a parser from an existing command chain.
    ///
    /// Used internally for subcommand handling.
    ///
    /// # Arguments
    ///
    /// * `command` - Command name
    /// * `chain` - Pre-built command chain
    ///
    /// # Returns
    ///
    /// A parser marked as already prepared
    pub fn from_chain(command: String, chain: Vec<CommandChain>) -> Self {
        Self {
            command,
            args: Vec::new(),
            command_chain: chain,
            is_prepared: true,
        }
    }

    /// Creates a new parser with remaining chain elements after an index.
    ///
    /// Used for passing control to subcommands.
    ///
    /// # Arguments
    ///
    /// * `start_idx` - Index to start slicing from
    ///
    /// # Returns
    ///
    /// New parser with remaining chain elements
    pub fn with_remaining_chain(&self, start_idx: usize) -> Self {
        let remaining_chain = if start_idx < self.command_chain.len() {
            self.command_chain[start_idx..].to_vec()
        } else {
            Vec::new()
        };

        Self {
            command: self.command.clone(),
            args: Vec::new(),
            command_chain: remaining_chain,
            is_prepared: true,
        }
    }

    /// Returns the command name being parsed.
    pub fn get_command(&self) -> &String {
        &self.command
    }

    /// Returns the full command chain.
    ///
    /// Alias for `get_parsed_commands_chain()` for convenience.
    pub fn get_command_chain(&self) -> &Vec<CommandChain> {
        &self.command_chain
    }
}