rust-args-parser 2.0.0

Tiny, fast, callback-based CLI argument 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
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
use std::ffi::{OsStr, OsString};

/// Color mode for help rendering.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ColorMode {
    Auto,
    Always,
    Never,
}

/// Global environment for a parse/render session.
#[derive(Clone, Copy, Debug)]
pub struct Env {
    /// Wrap columns for help. `0` means no wrapping.
    pub wrap_cols: usize,
    /// Whether to colorize help (honors `NO_COLOR` when `color` feature is enabled).
    pub color: ColorMode,
    /// Whether to compute suggestions on errors (if enabled).
    pub suggest: bool,
    /// Built-ins
    pub auto_help: bool,
    pub version: Option<&'static str>,
    pub author: Option<&'static str>,
}
impl Default for Env {
    fn default() -> Self {
        Self { wrap_cols: 0, color: ColorMode::Auto, suggest: true, auto_help: true, version: None, author: None }
    }
}

/// Whether an option may be repeated.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Repeat {
    Single,
    Many,
}

/// Group rule (applies to a set of options sharing the same group name).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GroupMode {
    Xor,
    ReqOne,
}

/// Provenance of a value in `Matches`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Source {
    Cli,
    Env,
    Default,
}

/// User-pluggable validator for a single value (OsStr-based, cross-platform).
///
/// The validator is expected to return a human-readable error message on failure.
/// If you want to propagate a typed error, use `validator_try(...)`.
pub type ValueValidatorFn<'a> = dyn Fn(&OsStr) -> crate::Result<()> + 'a;

/// Command-level validator that can inspect the final `Matches`.
pub type CmdValidatorFn<'a> = dyn Fn(&crate::Matches) -> crate::Result<()> + 'a;

/// Command handler (executed for the **leaf** command after callbacks).
pub type CmdHandlerFn<'a, Ctx> = dyn Fn(&crate::Matches, &mut Ctx) -> crate::Result<()> + 'a;

/// Callback to apply a value/flag into user context.
pub type OnValueFn<'a, Ctx> = dyn Fn(&OsStr, &mut Ctx) -> crate::Result<()> + 'a;
pub type OnFlagFn<'a, Ctx> = dyn Fn(&mut Ctx) -> crate::Result<()> + 'a;

/// Option (flag or value-bearing).
pub struct OptSpec<'a, Ctx: ?Sized> {
    name: &'a str,
    short: Option<char>,
    long: Option<&'a str>,
    metavar: Option<&'a str>,
    help: Option<&'a str>,
    env: Option<&'a str>,
    default: Option<OsString>,
    group: Option<&'a str>,
    repeat: Repeat,
    takes_value: bool,
    on_value: Option<Box<OnValueFn<'a, Ctx>>>, // value setter
    on_flag: Option<Box<OnFlagFn<'a, Ctx>>>,   // flag setter
    validator: Option<Box<ValueValidatorFn<'a>>>,
}

impl<'a, Ctx: ?Sized> OptSpec<'a, Ctx> {
    /// Create a **flag** option. Other fields are set via builder methods.
    pub fn flag<F>(name: &'a str, cb: F) -> Self
    where
        F: Fn(&mut Ctx) + 'a,
    {
        Self {
            name,
            short: None,
            long: None,
            metavar: None,
            help: None,
            env: None,
            default: None,
            group: None,
            repeat: Repeat::Single,
            takes_value: false,
            on_value: None,
            on_flag: Some(Box::new(move |ctx| {
                cb(ctx);
                Ok(())
            })),
            validator: None,
        }
    }

    /// Create a **flag** option with a fallible callback.
    pub fn flag_try<F, E>(name: &'a str, cb: F) -> Self
    where
        F: Fn(&mut Ctx) -> core::result::Result<(), E> + 'a,
        E: std::error::Error + Send + Sync + 'static,
    {
        Self {
            name,
            short: None,
            long: None,
            metavar: None,
            help: None,
            env: None,
            default: None,
            group: None,
            repeat: Repeat::Single,
            takes_value: false,
            on_value: None,
            on_flag: Some(Box::new(move |ctx| cb(ctx).map_err(crate::Error::user))),
            validator: None,
        }
    }

    /// Create a **value** option. Other fields are set via builder methods.
    pub fn value<F>(name: &'a str, cb: F) -> Self
    where
        F: Fn(&OsStr, &mut Ctx) + 'a,
    {
        Self {
            name,
            short: None,
            long: None,
            metavar: None,
            help: None,
            env: None,
            default: None,
            group: None,
            repeat: Repeat::Single,
            takes_value: true,
            on_value: Some(Box::new(move |v, ctx| {
                cb(v, ctx);
                Ok(())
            })),
            on_flag: None,
            validator: None,
        }
    }

    /// Create a **value** option with a fallible callback.
    pub fn value_try<F, E>(name: &'a str, cb: F) -> Self
    where
        F: Fn(&OsStr, &mut Ctx) -> core::result::Result<(), E> + 'a,
        E: std::error::Error + Send + Sync + 'static,
    {
        Self {
            name,
            short: None,
            long: None,
            metavar: None,
            help: None,
            env: None,
            default: None,
            group: None,
            repeat: Repeat::Single,
            takes_value: true,
            on_value: Some(Box::new(move |v, ctx| cb(v, ctx).map_err(crate::Error::user))),
            on_flag: None,
            validator: None,
        }
    }

    // --- builders ---
    #[must_use]
    pub fn short(mut self, s: char) -> Self {
        self.short = Some(s);
        self
    }
    #[must_use]
    pub fn long(mut self, l: &'a str) -> Self {
        self.long = Some(l);
        self
    }
    #[must_use]
    pub fn metavar(mut self, mv: &'a str) -> Self {
        self.metavar = Some(mv);
        self
    }
    #[must_use]
    pub fn help(mut self, h: &'a str) -> Self {
        self.help = Some(h);
        self
    }
    #[must_use]
    pub fn env(mut self, name: &'a str) -> Self {
        self.env = Some(name);
        self
    }
    #[must_use]
    pub fn default(mut self, val: impl Into<OsString>) -> Self {
        self.default = Some(val.into());
        self
    }
    #[must_use]
    pub fn group(mut self, g: &'a str) -> Self {
        self.group = Some(g);
        self
    }
    #[must_use]
    pub fn single(mut self) -> Self {
        self.repeat = Repeat::Single;
        self
    }
    #[must_use]
    pub fn repeatable(mut self) -> Self {
        self.repeat = Repeat::Many;
        self
    }

    /// Value validator that returns a displayable error (converted into `Error::User`).
    #[must_use]
    pub fn validator<F, E>(mut self, v: F) -> Self
    where
        F: Fn(&OsStr) -> core::result::Result<(), E> + 'a,
        E: core::fmt::Display,
    {
        self.validator = Some(Box::new(move |s| v(s).map_err(|e| crate::Error::User(e.to_string()))));
        self
    }

    /// Value validator that returns a typed error (boxed into `Error::UserAny`).
    #[must_use]
    pub fn validator_try<F, E>(mut self, v: F) -> Self
    where
        F: Fn(&OsStr) -> core::result::Result<(), E> + 'a,
        E: std::error::Error + Send + Sync + 'static,
    {
        self.validator = Some(Box::new(move |s| v(s).map_err(crate::Error::user)));
        self
    }

    // --- getters (get_*; booleans use is_*) ---
    #[must_use]
    pub fn get_name(&self) -> &str {
        self.name
    }
    #[must_use]
    pub fn get_short(&self) -> Option<char> {
        self.short
    }
    #[must_use]
    pub fn get_long(&self) -> Option<&str> {
        self.long
    }
    #[must_use]
    pub fn get_metavar(&self) -> Option<&str> {
        self.metavar
    }
    #[must_use]
    pub fn get_help(&self) -> Option<&str> {
        self.help
    }
    #[must_use]
    pub fn get_env(&self) -> Option<&str> {
        self.env
    }
    #[must_use]
    pub fn get_default(&self) -> Option<&OsString> {
        self.default.as_ref()
    }
    #[must_use]
    pub fn get_group(&self) -> Option<&str> {
        self.group
    }
    #[must_use]
    pub fn is_value(&self) -> bool {
        self.takes_value
    }
    #[must_use]
    pub fn get_repeat(&self) -> Repeat {
        self.repeat
    }
    #[must_use]
    pub fn get_on_value(&self) -> Option<&OnValueFn<'a, Ctx>> {
        self.on_value.as_deref()
    }
    #[must_use]
    pub fn get_on_flag(&self) -> Option<&OnFlagFn<'a, Ctx>> {
        self.on_flag.as_deref()
    }
    #[must_use]
    pub fn get_validator(&self) -> Option<&ValueValidatorFn<'a>> {
        self.validator.as_deref()
    }
}

/// Positional cardinality.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PosCardinality {
    One { required: bool },
    Many,
    Range { min: usize, max: usize },
}

/// Positional argument specification.
pub struct PosSpec<'a, Ctx: ?Sized> {
    name: &'a str,
    help: Option<&'a str>,
    card: PosCardinality,
    on_value: Box<OnValueFn<'a, Ctx>>,
    validator: Option<Box<ValueValidatorFn<'a>>>,
}
impl<'a, Ctx: ?Sized> PosSpec<'a, Ctx> {
    pub fn new<F>(name: &'a str, cb: F) -> Self
    where
        F: Fn(&OsStr, &mut Ctx) + 'a,
    {
        Self {
            name,
            help: None,
            card: PosCardinality::One { required: false },
            on_value: Box::new(move |v, ctx| {
                cb(v, ctx);
                Ok(())
            }),
            validator: None,
        }
    }

    pub fn new_try<F, E>(name: &'a str, cb: F) -> Self
    where
        F: Fn(&OsStr, &mut Ctx) -> core::result::Result<(), E> + 'a,
        E: std::error::Error + Send + Sync + 'static,
    {
        Self {
            name,
            help: None,
            card: PosCardinality::One { required: false },
            on_value: Box::new(move |v, ctx| cb(v, ctx).map_err(crate::Error::user)),
            validator: None,
        }
    }

    // builders
    #[must_use]
    pub fn help(mut self, h: &'a str) -> Self {
        self.help = Some(h);
        self
    }
    #[must_use]
    pub fn required(mut self) -> Self {
        self.card = PosCardinality::One { required: true };
        self
    }
    #[must_use]
    pub fn many(mut self) -> Self {
        self.card = PosCardinality::Many;
        self
    }
    #[must_use]
    pub fn range(mut self, min: usize, max: usize) -> Self {
        self.card = PosCardinality::Range { min, max };
        self
    }

    /// Positional validator that returns a displayable error (converted into `Error::User`).
    #[must_use]
    pub fn validator<F, E>(mut self, v: F) -> Self
    where
        F: Fn(&OsStr) -> core::result::Result<(), E> + 'a,
        E: core::fmt::Display,
    {
        self.validator = Some(Box::new(move |s| v(s).map_err(|e| crate::Error::User(e.to_string()))));
        self
    }

    /// Positional validator that returns a typed error (boxed into `Error::UserAny`).
    #[must_use]
    pub fn validator_try<F, E>(mut self, v: F) -> Self
    where
        F: Fn(&OsStr) -> core::result::Result<(), E> + 'a,
        E: std::error::Error + Send + Sync + 'static,
    {
        self.validator = Some(Box::new(move |s| v(s).map_err(crate::Error::user)));
        self
    }

    // getters
    #[must_use]
    pub fn get_name(&self) -> &str {
        self.name
    }
    #[must_use]
    pub fn get_help(&self) -> Option<&str> {
        self.help
    }
    #[must_use]
    pub fn get_cardinality(&self) -> PosCardinality {
        self.card
    }
    #[must_use]
    pub fn is_required(&self) -> bool {
        matches!(self.card, PosCardinality::One { required: true })
            || matches!(self.card, PosCardinality::Range { min, .. } if min > 0)
    }
    #[must_use]
    pub fn is_multiple(&self) -> bool {
        !matches!(self.card, PosCardinality::One { .. })
    }
    pub fn get_on_value(&self) -> &OnValueFn<'a, Ctx> {
        &*self.on_value
    }
    #[must_use]
    pub fn get_validator(&self) -> Option<&ValueValidatorFn<'a>> {
        self.validator.as_deref()
    }
}

/// Group declaration.
pub struct GroupDecl<'a> {
    pub name: &'a str,
    pub mode: GroupMode,
}

/// Command specification.
pub struct CmdSpec<'a, Ctx: ?Sized> {
    name: &'a str,
    help: Option<&'a str>,
    aliases: Vec<&'a str>,
    opts: Vec<OptSpec<'a, Ctx>>,
    positionals: Vec<PosSpec<'a, Ctx>>,
    subcommands: Vec<CmdSpec<'a, Ctx>>,
    groups: Vec<GroupDecl<'a>>,
    validate_cmd: Option<Box<CmdValidatorFn<'a>>>,
    handler: Option<Box<CmdHandlerFn<'a, Ctx>>>, // leaf command handler
}
impl<'a, Ctx: ?Sized> CmdSpec<'a, Ctx> {
    #[must_use]
    pub fn new(name: &'a str) -> Self {
        Self {
            name,
            help: None,
            aliases: Vec::new(),
            opts: Vec::new(),
            positionals: Vec::new(),
            subcommands: Vec::new(),
            groups: Vec::new(),
            validate_cmd: None,
            handler: None,
        }
    }
    // builders
    #[must_use]
    pub fn help(mut self, s: &'a str) -> Self {
        self.help = Some(s);
        self
    }
    #[must_use]
    pub fn alias(mut self, a: &'a str) -> Self {
        self.aliases.push(a);
        self
    }
    #[must_use]
    pub fn opt(mut self, o: OptSpec<'a, Ctx>) -> Self {
        self.opts.push(o);
        self
    }
    #[must_use]
    pub fn pos(mut self, p: PosSpec<'a, Ctx>) -> Self {
        self.positionals.push(p);
        self
    }
    #[must_use]
    pub fn subcmd(mut self, c: Self) -> Self {
        self.subcommands.push(c);
        self
    }
    #[must_use]
    pub fn group(mut self, name: &'a str, mode: GroupMode) -> Self {
        self.groups.push(GroupDecl { name, mode });
        self
    }

    /// Set per-command validator returning a displayable error (converted into `Error::User`).
    #[must_use]
    pub fn validator<F, E>(mut self, cb: F) -> Self
    where
        F: Fn(&crate::Matches) -> core::result::Result<(), E> + 'a,
        E: core::fmt::Display,
    {
        self.validate_cmd = Some(Box::new(move |m| cb(m).map_err(|e| crate::Error::User(e.to_string()))));
        self
    }

    /// Set per-command validator returning a typed error (boxed into `Error::UserAny`).
    #[must_use]
    pub fn validator_try<F, E>(mut self, cb: F) -> Self
    where
        F: Fn(&crate::Matches) -> core::result::Result<(), E> + 'a,
        E: std::error::Error + Send + Sync + 'static,
    {
        self.validate_cmd = Some(Box::new(move |m| cb(m).map_err(crate::Error::user)));
        self
    }

    /// Set a leaf command handler. Only the **selected leaf** handler is executed.
    #[must_use]
    pub fn handler<F>(mut self, cb: F) -> Self
    where
        F: Fn(&crate::Matches, &mut Ctx) + 'a,
    {
        self.handler = Some(Box::new(move |m, ctx| {
            cb(m, ctx);
            Ok(())
        }));
        self
    }

    /// Set a leaf command handler with a typed error (boxed into `Error::UserAny`).
    #[must_use]
    pub fn handler_try<F, E>(mut self, cb: F) -> Self
    where
        F: Fn(&crate::Matches, &mut Ctx) -> core::result::Result<(), E> + 'a,
        E: std::error::Error + Send + Sync + 'static,
    {
        self.handler = Some(Box::new(move |m, ctx| cb(m, ctx).map_err(crate::Error::user)));
        self
    }

    // getters
    #[must_use]
    pub fn get_name(&self) -> &str {
        self.name
    }
    #[must_use]
    pub fn get_help(&self) -> Option<&str> {
        self.help
    }
    #[must_use]
    pub fn get_aliases(&self) -> &[&'a str] {
        &self.aliases
    }
    #[must_use]
    pub fn get_opts(&self) -> &[OptSpec<'a, Ctx>] {
        &self.opts
    }
    #[must_use]
    pub fn get_positionals(&self) -> &[PosSpec<'a, Ctx>] {
        &self.positionals
    }
    #[must_use]
    pub fn get_subcommands(&self) -> &[Self] {
        &self.subcommands
    }
    #[must_use]
    pub fn get_groups(&self) -> &[GroupDecl<'a>] {
        &self.groups
    }
    #[must_use]
    pub fn get_validator(&self) -> Option<&CmdValidatorFn<'a>> {
        self.validate_cmd.as_deref()
    }
    #[must_use]
    pub fn get_handler(&self) -> Option<&CmdHandlerFn<'a, Ctx>> {
        self.handler.as_deref()
    }
    #[must_use]
    pub fn find_sub(&self, needle: &str) -> Option<&Self> {
        self.subcommands.iter().find(|c| c.name == needle || c.aliases.iter().any(|a| *a == needle))
    }
}