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
use crate::cli_error::{CliError, CliErrorKind};
use std::collections::HashMap;
/// Represents a parsed command with its name and arguments.
///
/// The `Command` struct holds the parsed command-line arguments in a structured format.
/// It provides convenient methods for accessing arguments with type conversion and error handling.
///
/// # Examples
///
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("serve --port 8080 --host localhost").unwrap();
/// assert_eq!(cmd.name, "serve");
/// assert_eq!(cmd.get_argument("port"), Some("8080"));
/// assert_eq!(cmd.get_argument("host"), Some("localhost"));
/// ```
#[derive(Debug, Clone)]
pub struct Command {
/// The command name (first non-flag argument)
pub name: String,
/// Parsed arguments as a map of argument names to their values
pub arguments: HashMap<String, Box<[String]>>,
}
impl Command {
/// Gets the nth parameter of an argument as a `usize`, if it exists and can be parsed.
///
/// # Arguments
/// * `argument` - The name of the argument
/// * `nth` - The zero-based index of the parameter
///
/// # Returns
/// * `Some(usize)` - If the parameter exists and can be parsed as a usize
/// * `None` - If the argument doesn't exist or the parameter can't be parsed
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--numbers 1 2 3").unwrap();
/// assert_eq!(cmd.get_argument_nth_parameter_usize("numbers", 0), Some(1));
/// assert_eq!(cmd.get_argument_nth_parameter_usize("numbers", 1), Some(2));
/// assert_eq!(cmd.get_argument_nth_parameter_usize("numbers", 5), None);
/// ```
pub fn get_argument_nth_parameter_usize(&self, argument: &str, nth: usize) -> Option<usize> {
match self.arguments.get(argument) {
Some(params) if params.len() > nth => usize::from_str_radix(¶ms[nth], 10).ok(),
_ => None,
}
}
/// Gets the nth parameter of an argument as a string slice, if it exists.
///
/// # Arguments
/// * `argument` - The name of the argument
/// * `nth` - The zero-based index of the parameter
///
/// # Returns
/// * `Some(&str)` - If the parameter exists
/// * `None` - If the argument doesn't exist or the parameter is out of bounds
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--files a.txt b.txt c.txt").unwrap();
/// assert_eq!(cmd.get_argument_nth_parameter("files", 0), Some("a.txt"));
/// assert_eq!(cmd.get_argument_nth_parameter("files", 1), Some("b.txt"));
/// assert_eq!(cmd.get_argument_nth_parameter("files", 5), None);
/// ```
pub fn get_argument_nth_parameter(&self, argument: &str, nth: usize) -> Option<&str> {
match self.arguments.get(argument) {
Some(params) if params.len() > nth => Some(¶ms[nth]),
_ => None,
}
}
/// Gets the nth parameter of an argument as a string slice, returning an error if missing.
///
/// # Arguments
/// * `argument` - The name of the argument
/// * `nth` - The zero-based index of the parameter
///
/// # Returns
/// * `Ok(&str)` - If the parameter exists
/// * `Err(CliError)` - If the argument or parameter is missing
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--files a.txt b.txt").unwrap();
/// assert_eq!(cmd.get_argument_nth_param_mandatory("files", 0).unwrap(), "a.txt");
/// assert_eq!(cmd.get_argument_nth_param_mandatory("files", 1).unwrap(), "b.txt");
/// ```
pub fn get_argument_nth_param_mandatory(
&self,
argument: &str,
nth: usize,
) -> Result<&str, CliError> {
let params = self.get_argument_mandatory_all(argument)?;
if params.len() > nth {
Ok(¶ms[nth])
} else if nth > 0 {
Err(CliErrorKind::MissingParameter(argument.to_string(), nth).into())
} else {
Err(CliErrorKind::MissingArgument(argument.to_string()).into())
}
}
/// Gets the nth parameter of an argument as a `usize`, returning an error if missing or invalid.
///
/// # Arguments
/// * `argument` - The name of the argument
/// * `nth` - The zero-based index of the parameter
///
/// # Returns
/// * `Ok(usize)` - If the parameter exists and can be parsed
/// * `Err(CliError)` - If the argument is missing or the parameter can't be parsed
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--numbers 1 2 3").unwrap();
/// assert_eq!(cmd.get_argument_nth_param_mandatory_usize("numbers", 0).unwrap(), 1);
/// assert_eq!(cmd.get_argument_nth_param_mandatory_usize("numbers", 1).unwrap(), 2);
/// ```
pub fn get_argument_nth_param_mandatory_usize(
&self,
argument: &str,
nth: usize,
) -> Result<usize, CliError> {
let param = self.get_argument_nth_param_mandatory(argument, nth)?;
Ok(usize::from_str_radix(param, 10)?)
}
/// Checks if an argument exists (regardless of whether it has values).
///
/// # Arguments
/// * `argument` - The name of the argument to check
///
/// # Returns
/// * `true` - If the argument exists
/// * `false` - If the argument doesn't exist
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--verbose --port 8080").unwrap();
/// assert!(cmd.contains_argument("verbose"));
/// assert!(cmd.contains_argument("port"));
/// assert!(!cmd.contains_argument("debug"));
/// ```
pub fn contains_argument(&self, argument: &str) -> bool {
self.arguments.contains_key(argument)
}
/// Gets the first parameter of an argument as a string slice, if it exists.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Some(&str)` - If the argument exists and has at least one parameter
/// * `None` - If the argument doesn't exist or has no parameters
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--host localhost --port 8080").unwrap();
/// assert_eq!(cmd.get_argument("host"), Some("localhost"));
/// assert_eq!(cmd.get_argument("port"), Some("8080"));
/// assert_eq!(cmd.get_argument("debug"), None);
/// ```
pub fn get_argument(&self, argument: &str) -> Option<&str> {
self.get_argument_nth_parameter(argument, 0)
}
/// Gets the first parameter of an argument as a string slice, returning an error if missing.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Ok(&str)` - If the argument exists and has at least one parameter
/// * `Err(CliError)` - If the argument is missing
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--host localhost").unwrap();
/// assert_eq!(cmd.get_argument_mandatory("host").unwrap(), "localhost");
/// ```
pub fn get_argument_mandatory(&self, argument: &str) -> Result<&str, CliError> {
self.get_argument_nth_param_mandatory(argument, 0)
}
/// Gets the first parameter of an argument as a `usize`, returning an error if missing or invalid.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Ok(usize)` - If the argument exists and can be parsed as a usize
/// * `Err(CliError)` - If the argument is missing or can't be parsed
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--port 8080").unwrap();
/// assert_eq!(cmd.get_argument_mandatory_usize("port").unwrap(), 8080);
/// ```
pub fn get_argument_mandatory_usize(&self, argument: &str) -> Result<usize, CliError> {
let value = self.get_argument_mandatory(argument)?;
Ok(usize::from_str_radix(value, 10)?)
}
/// Gets the first parameter of an argument as a `usize`, if it exists and can be parsed.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Some(usize)` - If the argument exists and can be parsed as a usize
/// * `None` - If the argument doesn't exist or can't be parsed
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--port 8080 --workers 4").unwrap();
/// assert_eq!(cmd.get_argument_usize("port"), Some(8080));
/// assert_eq!(cmd.get_argument_usize("workers"), Some(4));
/// assert_eq!(cmd.get_argument_usize("debug"), None);
/// ```
pub fn get_argument_usize(&self, argument: &str) -> Option<usize> {
match self.get_argument(argument) {
Some(v) => match usize::from_str_radix(v, 10) {
Ok(v) => Some(v),
Err(_) => None,
},
None => None,
}
}
/// Gets the first parameter of an argument as an `i32`, if it exists and can be parsed.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Some(i32)` - If the argument exists and can be parsed as an i32
/// * `None` - If the argument doesn't exist or can't be parsed
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--port 8080 --timeout 30").unwrap();
/// assert_eq!(cmd.get_argument_i32("port"), Some(8080));
/// assert_eq!(cmd.get_argument_i32("timeout"), Some(30));
/// assert_eq!(cmd.get_argument_i32("debug"), None);
/// ```
pub fn get_argument_i32(&self, argument: &str) -> Option<i32> {
match self.get_argument(argument) {
Some(v) => v.parse().ok(),
None => None,
}
}
/// Gets the first parameter of an argument as a `f64`, if it exists and can be parsed.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Some(f64)` - If the argument exists and can be parsed as an f64
/// * `None` - If the argument doesn't exist or can't be parsed
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--ratio 0.5 --threshold 1.23").unwrap();
/// assert_eq!(cmd.get_argument_f64("ratio"), Some(0.5));
/// assert_eq!(cmd.get_argument_f64("threshold"), Some(1.23));
/// assert_eq!(cmd.get_argument_f64("debug"), None);
/// ```
pub fn get_argument_f64(&self, argument: &str) -> Option<f64> {
match self.get_argument(argument) {
Some(v) => v.parse().ok(),
None => None,
}
}
/// Gets the first parameter of an argument as a `bool`, if it exists and can be parsed.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Some(bool)` - If the argument exists and can be parsed as a bool
/// * `None` - If the argument doesn't exist or can't be parsed
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--enabled true --disabled false").unwrap();
/// assert_eq!(cmd.get_argument_bool("enabled"), Some(true));
/// assert_eq!(cmd.get_argument_bool("disabled"), Some(false));
/// assert_eq!(cmd.get_argument_bool("debug"), None);
/// ```
pub fn get_argument_bool(&self, argument: &str) -> Option<bool> {
match self.get_argument(argument) {
Some(v) => v.parse().ok(),
None => None,
}
}
/// Gets all parameters of an argument as a slice of strings, if it exists.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Some(&Box<[String]>)` - If the argument exists
/// * `None` - If the argument doesn't exist
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--files a.txt b.txt c.txt").unwrap();
/// let files = cmd.get_argument_all("files").unwrap();
/// assert_eq!(files.len(), 3);
/// assert_eq!(&files[0], "a.txt");
/// assert_eq!(&files[1], "b.txt");
/// assert_eq!(&files[2], "c.txt");
/// ```
pub fn get_argument_all(&self, argument: &str) -> Option<&Box<[String]>> {
self.arguments.get(argument)
}
/// Gets all parameters of an argument as a slice of strings, returning an error if missing.
///
/// # Arguments
/// * `argument` - The name of the argument
///
/// # Returns
/// * `Ok(&Box<[String]>)` - If the argument exists
/// * `Err(CliError)` - If the argument is missing
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--files a.txt b.txt").unwrap();
/// let files = cmd.get_argument_mandatory_all("files").unwrap();
/// assert_eq!(files.len(), 2);
/// ```
pub fn get_argument_mandatory_all(&self, argument: &str) -> Result<&Box<[String]>, CliError> {
self.get_argument_all(argument)
.ok_or(CliErrorKind::MissingArgument(argument.to_string()).into())
}
/// Gets the first parameter of an argument with type conversion, or returns a default value if missing.
///
/// This method attempts to parse the argument value as the specified type `T`. If the argument
/// is missing, it returns the provided default value instead of an error.
///
/// # Arguments
/// * `argument` - The name of the argument
/// * `default` - The default value to return if the argument is missing
///
/// # Returns
/// * `Ok(T)` - If the argument exists and can be parsed, or if missing (returns default)
/// * `Err(CliError)` - If the argument exists but can't be parsed as the target type
///
/// # Examples
/// ```rust
/// use cli_command::{Command, parse_command_string};
///
/// let cmd = parse_command_string("--port 8080").unwrap();
/// let port: u16 = cmd.get_argument_or_default("port", 3000).unwrap();
/// assert_eq!(port, 8080);
///
/// let timeout: u64 = cmd.get_argument_or_default("timeout", 30).unwrap();
/// assert_eq!(timeout, 30); // Uses default since timeout not provided
/// ```
pub fn get_argument_or_default<T>(&self, argument: &str, default: T) -> Result<T, CliError>
where
T: std::str::FromStr,
T::Err: std::error::Error + Send + Sync + 'static,
{
match self.get_argument_mandatory(argument) {
Ok(value) => T::from_str(value).map_err(|e| CliError::new_inner(Box::new(e))),
Err(_) => Ok(default),
}
}
}
#[cfg(test)]
mod tests {
use crate::cli_error::{CliError, CliErrorKind};
use crate::parse::parse_command_string;
use std::net::SocketAddr;
use std::str::FromStr;
#[test]
fn test_get_argument_first_value_or_default() -> Result<(), CliError> {
let cmd = parse_command_string("--socket 172.20.3.1:7618")?;
assert_eq!(&cmd.arguments.get("socket").unwrap()[0], "172.20.3.1:7618");
let socket =
cmd.get_argument_or_default("socket", SocketAddr::from_str("127.0.0.1:1000")?)?;
assert_eq!(socket, SocketAddr::from_str("172.20.3.1:7618")?);
let cmd = parse_command_string("--streams 2")?;
let streams = cmd.get_argument_or_default("streams", 5)?;
assert_eq!(streams, 2);
assert_eq!(cmd.get_argument_or_default("components", 5)?, 5);
Ok(())
}
#[test]
fn test_basic_argument_parsing() -> Result<(), CliError> {
let cmd = parse_command_string("serve --port 8080 --host localhost --verbose")?;
assert_eq!(cmd.name, "serve");
assert_eq!(cmd.get_argument("port"), Some("8080"));
assert_eq!(cmd.get_argument("host"), Some("localhost"));
assert!(cmd.contains_argument("verbose"));
assert!(!cmd.contains_argument("debug"));
Ok(())
}
#[test]
fn test_multiple_values() -> Result<(), CliError> {
let cmd = parse_command_string("build --files a.txt b.txt c.txt --output dist/")?;
assert_eq!(cmd.name, "build");
let files = cmd.get_argument_all("files").unwrap();
assert_eq!(files.len(), 3);
assert_eq!(&files[0], "a.txt");
assert_eq!(&files[1], "b.txt");
assert_eq!(&files[2], "c.txt");
assert_eq!(cmd.get_argument("output"), Some("dist/"));
Ok(())
}
#[test]
fn test_usize_conversion() -> Result<(), CliError> {
let cmd = parse_command_string("--port 8080 --workers 4 --timeout 30")?;
assert_eq!(cmd.get_argument_usize("port"), Some(8080));
assert_eq!(cmd.get_argument_usize("workers"), Some(4));
assert_eq!(cmd.get_argument_usize("timeout"), Some(30));
assert_eq!(cmd.get_argument_usize("missing"), None);
Ok(())
}
#[test]
fn test_mandatory_arguments() -> Result<(), CliError> {
let cmd = parse_command_string("--required value")?;
assert_eq!(cmd.get_argument_mandatory("required")?, "value");
let cmd = parse_command_string("")?;
let result = cmd.get_argument_mandatory("missing");
assert!(result.is_err());
if let Err(CliError {
kind: CliErrorKind::MissingArgument(arg),
..
}) = result
{
assert_eq!(arg, "missing");
} else {
panic!("Expected MissingArgument error");
}
Ok(())
}
#[test]
fn test_nth_parameter_access() -> Result<(), CliError> {
let cmd = parse_command_string("--numbers 1 2 3 4 5")?;
assert_eq!(cmd.get_argument_nth_parameter("numbers", 0), Some("1"));
assert_eq!(cmd.get_argument_nth_parameter("numbers", 2), Some("3"));
assert_eq!(cmd.get_argument_nth_parameter("numbers", 4), Some("5"));
assert_eq!(cmd.get_argument_nth_parameter("numbers", 5), None);
assert_eq!(cmd.get_argument_nth_parameter("missing", 0), None);
Ok(())
}
#[test]
fn test_nth_parameter_usize() -> Result<(), CliError> {
let cmd = parse_command_string("--values 10 20 30")?;
assert_eq!(cmd.get_argument_nth_parameter_usize("values", 0), Some(10));
assert_eq!(cmd.get_argument_nth_parameter_usize("values", 1), Some(20));
assert_eq!(cmd.get_argument_nth_parameter_usize("values", 2), Some(30));
assert_eq!(cmd.get_argument_nth_parameter_usize("values", 3), None);
assert_eq!(cmd.get_argument_nth_parameter_usize("missing", 0), None);
Ok(())
}
#[test]
fn test_mandatory_nth_parameter() -> Result<(), CliError> {
let cmd = parse_command_string("--files a.txt b.txt c.txt")?;
assert_eq!(cmd.get_argument_nth_param_mandatory("files", 0)?, "a.txt");
assert_eq!(cmd.get_argument_nth_param_mandatory("files", 1)?, "b.txt");
assert_eq!(cmd.get_argument_nth_param_mandatory("files", 2)?, "c.txt");
let result = cmd.get_argument_nth_param_mandatory("files", 5);
assert!(result.is_err());
if let Err(CliError {
kind: CliErrorKind::MissingParameter(arg, pos),
..
}) = result
{
assert_eq!(arg, "files");
assert_eq!(pos, 5);
} else {
panic!("Expected MissingParameter error");
}
Ok(())
}
#[test]
fn test_mandatory_nth_parameter_usize() -> Result<(), CliError> {
let cmd = parse_command_string("--numbers 1 2 3")?;
assert_eq!(cmd.get_argument_nth_param_mandatory_usize("numbers", 0)?, 1);
assert_eq!(cmd.get_argument_nth_param_mandatory_usize("numbers", 1)?, 2);
assert_eq!(cmd.get_argument_nth_param_mandatory_usize("numbers", 2)?, 3);
Ok(())
}
#[test]
fn test_type_conversion_with_defaults() -> Result<(), CliError> {
let cmd = parse_command_string("--port 8080")?;
let port: u16 = cmd.get_argument_or_default("port", 3000)?;
assert_eq!(port, 8080);
let timeout: u64 = cmd.get_argument_or_default("timeout", 30)?;
assert_eq!(timeout, 30); // Uses default
let host: String = cmd.get_argument_or_default("host", "localhost".to_string())?;
assert_eq!(host, "localhost"); // Uses default
Ok(())
}
#[test]
fn test_short_and_long_arguments() -> Result<(), CliError> {
let cmd = parse_command_string("-v --verbose --port 8080 -h localhost")?;
assert!(cmd.contains_argument("v"));
assert!(cmd.contains_argument("verbose"));
assert_eq!(cmd.get_argument("port"), Some("8080"));
assert_eq!(cmd.get_argument("h"), Some("localhost"));
Ok(())
}
#[test]
fn test_boolean_flags() -> Result<(), CliError> {
let cmd = parse_command_string("--enable-feature --no-cache --debug")?;
assert!(cmd.contains_argument("enable-feature"));
assert!(cmd.contains_argument("no-cache"));
assert!(cmd.contains_argument("debug"));
assert!(!cmd.contains_argument("missing"));
Ok(())
}
#[test]
fn test_empty_command() -> Result<(), CliError> {
let cmd = parse_command_string("")?;
assert_eq!(cmd.name, "");
assert!(cmd.arguments.is_empty());
Ok(())
}
#[test]
fn test_command_without_arguments() -> Result<(), CliError> {
let cmd = parse_command_string("help")?;
assert_eq!(cmd.name, "help");
assert!(cmd.arguments.is_empty());
Ok(())
}
#[test]
fn test_invalid_usize_parsing() -> Result<(), CliError> {
let cmd = parse_command_string("--port abc --workers 123")?;
assert_eq!(cmd.get_argument_usize("port"), None); // Invalid number
assert_eq!(cmd.get_argument_usize("workers"), Some(123)); // Valid number
assert_eq!(cmd.get_argument_usize("missing"), None); // Missing argument
Ok(())
}
#[test]
fn test_error_handling() -> Result<(), CliError> {
let cmd = parse_command_string("--port 8080")?;
let result = cmd.get_argument_mandatory("missing");
assert!(result.is_err());
let result = cmd.get_argument_nth_param_mandatory("port", 5);
assert!(result.is_err());
Ok(())
}
}