argp 0.4.0

Derive-based argument parser optimized for code size
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
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: 2023 Jakub Jirutka <jakub@jirutka.cz>
// SPDX-FileCopyrightText: 2020 Google LLC

//! Items in this module are all used by the generated code, and should not be
//! considered part of this library's public API surface.

use std::ffi::{OsStr, OsString};

use crate::error::Error;
use crate::help::{CommandInfo, HelpInfo, OptionArgInfo};
use crate::EarlyExit;

/// This function implements argument parsing for structs.
///
/// - `cmd_name`: The identifier for the current command.
/// - `args`: The command line arguments.
/// - `parse_options`: Helper to parse optional arguments.
/// - `parse_positionals`: Helper to parse positional arguments.
/// - `parse_subcommand`: Helper to parse a subcommand.
/// - `help`: The [`Help`] instance for generating a help message.
#[doc(hidden)]
pub fn parse_struct_args(
    cmd_name: &[&str],
    args: &[&OsStr],
    mut parse_options: ParseStructOptions<'_, '_>,
    mut parse_positionals: ParseStructPositionals<'_>,
    mut parse_subcommand: Option<ParseStructSubCommand<'_>>,
    help: &'static HelpInfo,
) -> Result<(), EarlyExit> {
    let mut help_requested = false;
    let mut help_cmd = false;
    let mut remaining_args = args;
    let mut positional_index = 0;
    let mut options_ended = false;

    'parse_args: while let Some(&next_arg_os) = remaining_args.first() {
        remaining_args = &remaining_args[1..];
        let next_arg = next_arg_os.to_str().unwrap_or("");

        if matches!(next_arg, "--help" | "-h" | "help") && !options_ended {
            help_requested = true;
            help_cmd = next_arg_os == "help";
            continue;
        }

        if next_arg.starts_with('-') && !options_ended {
            if next_arg_os == "--" {
                options_ended = true;
                continue;
            }

            if help_cmd {
                return Err(Error::OptionsAfterHelp.into());
            }

            // Handle combined short options; `-ab` is parsed as `-a -b`,
            // `-an 5` as `-a -n 5`, but `-na 5` would fail.
            if next_arg.len() > 2 && &next_arg[1..2] != "-" {
                let mut chars = next_arg[1..].chars().peekable();

                while let Some(short) = chars.next() {
                    // Only the last option can accept a value.
                    if chars.peek().is_some() {
                        parse_options.parse(&format!("-{}", short), &mut (&[] as &[&OsStr]))?;
                    } else {
                        parse_options.parse(&format!("-{}", short), &mut remaining_args)?;
                    }
                }
            } else {
                parse_options.parse(next_arg, &mut remaining_args)?;
            }

            continue;
        }

        if let Some(ref mut parse_subcommand) = parse_subcommand {
            if parse_subcommand.parse(
                help_requested,
                cmd_name,
                next_arg,
                remaining_args,
                &mut parse_options,
            )? {
                // Unset `help`, since we handled it in the subcommand
                help_requested = false;
                break 'parse_args;
            }
        }

        options_ended |= parse_positionals.parse(&mut positional_index, next_arg_os)?;
    }

    if help_requested {
        let global_options = parse_options
            .parent
            .map_or_else(Vec::new, |p| p.global_options());

        Err(EarlyExit::Help(help.help(cmd_name.join(" "), global_options)))
    } else {
        Ok(())
    }
}

#[doc(hidden)]
pub struct ParseStructOptions<'a, 'p> {
    /// A mapping from option string literals to the entry in the output table.
    /// This may contain multiple entries mapping to the same location in the
    /// table if both a short and long version of the option exist (`-z` and
    /// `--zoo`).
    pub arg_to_slot: &'static [(&'static str, usize)],

    /// The storage for argument output data.
    pub slots: &'a mut [ParseStructOption<'a>],

    /// A boolean flag for each element of the `slots` slice that specifies
    /// whether the option(s) associated with the slot is a global option.
    pub slots_global: &'static [bool],

    /// A reference to the [`Help`] struct in the associated [`FromArgs`]. This
    /// is used to collect global options for generating a help message.
    pub help: &'static HelpInfo,

    /// If this struct represents options of a subcommand, then `parent` is an
    /// indirect reference to the previous [`ParseStructOptions`] in the chain,
    /// used for parsing global options.
    pub parent: Option<&'p mut dyn ParseGlobalOptions>,
}

impl<'a, 'p> ParseStructOptions<'a, 'p> {
    /// Parses a command-line option. If the option is not found in this
    /// instance, it tries to parse it as a global option in the parent
    /// instance, recursively. If it's not found even there, returns
    /// `Err("Unrecognized argument: {arg}")`.
    ///
    /// - `arg`: The current option argument being parsed (e.g. `--foo`).
    /// - `remaining_args`: The remaining command line arguments. This slice
    ///    will be advanced forwards if the option takes a value argument.
    fn parse(&mut self, arg: &str, remaining_args: &mut &[&OsStr]) -> Result<(), Error> {
        match self.arg_to_slot.iter().find(|(name, _)| *name == arg) {
            Some((_, pos)) => Self::fill_slot(&mut self.slots[*pos], arg, remaining_args),
            None => self
                .try_parse_global(arg, remaining_args)
                .unwrap_or_else(|| Err(Error::UnknownArgument(OsString::from(arg)))),
        }
    }

    fn fill_slot(
        slot: &mut ParseStructOption<'a>,
        arg: &str,
        remaining_args: &mut &[&OsStr],
    ) -> Result<(), Error> {
        match slot {
            ParseStructOption::Flag(ref mut b) => b.set_flag(arg),
            ParseStructOption::Value(ref mut pvs) => {
                let value = remaining_args
                    .first()
                    .ok_or_else(|| Error::MissingArgValue(arg.to_owned()))?;
                *remaining_args = &remaining_args[1..];
                pvs.fill_slot(arg, value)?;
            }
        }
        Ok(())
    }
}

#[doc(hidden)]
pub trait ParseGlobalOptions {
    /// Parses a global command-line option. If the option is not found in
    /// _self_, it recursively calls this function on the parent. If the option
    /// is still not found, it returns `None`.
    ///
    /// - `arg`: The current option argument being parsed (e.g. `--foo`).
    /// - `remaining_args`: The remaining command line arguments. This slice
    ///    will be advanced forwards if the option takes a value argument.
    fn try_parse_global(
        &mut self,
        arg: &str,
        remaining_args: &mut &[&OsStr],
    ) -> Option<Result<(), Error>>;

    /// Returns a vector representing global options specified on this instance
    /// and recursively on the parent. This is used for generating a help
    /// message.
    fn global_options(&self) -> Vec<&'static OptionArgInfo>;
}

impl<'a, 'p> ParseGlobalOptions for ParseStructOptions<'a, 'p> {
    fn try_parse_global(
        &mut self,
        arg: &str,
        remaining_args: &mut &[&OsStr],
    ) -> Option<Result<(), Error>> {
        self.arg_to_slot
            .iter()
            .find(|(name, pos)| *name == arg && self.slots_global[*pos])
            .map(|(_, pos)| Self::fill_slot(&mut self.slots[*pos], arg, remaining_args))
            .or_else(|| {
                self.parent
                    .as_mut()
                    .and_then(|p| p.try_parse_global(arg, remaining_args))
            })
    }

    fn global_options(&self) -> Vec<&'static OptionArgInfo> {
        let mut opts = self
            .parent
            .as_ref()
            .map_or_else(Vec::new, |p| p.global_options());
        opts.extend(self.help.options.iter().filter(|o| o.global));
        opts
    }
}

/// `--` or `-` options, including a mutable reference to their value.
#[doc(hidden)]
pub enum ParseStructOption<'a> {
    /// A flag which is set to `true` when provided.
    Flag(&'a mut dyn ParseFlag),
    /// A value which is parsed from the string following the `--` argument,
    /// e.g. `--foo bar`.
    Value(&'a mut dyn ParseValueSlot),
}

#[doc(hidden)]
pub struct ParseStructPositionals<'a> {
    pub positionals: &'a mut [ParseStructPositional<'a>],
    pub last_is_repeating: bool,
    pub last_is_greedy: bool,
}

impl<'a> ParseStructPositionals<'a> {
    /// Parses the next positional argument.
    ///
    /// - `index`: The index of the argument.
    /// - `arg`: The argument supplied by the user.
    ///
    /// Returns `true` if non-positional argument parsing should stop after this
    /// one.
    fn parse(&mut self, index: &mut usize, arg: &OsStr) -> Result<bool, Error> {
        if *index < self.positionals.len() {
            self.positionals[*index].parse(arg)?;

            if self.last_is_repeating && *index == self.positionals.len() - 1 {
                // Don't increment position if we're at the last arg *and* the
                // last arg is repeating. If it's also remainder, halt
                // non-option processing after this.
                Ok(self.last_is_greedy)
            } else {
                // If it is repeating, though, increment the index and continue
                // processing options.
                *index += 1;
                Ok(false)
            }
        } else {
            Err(Error::UnknownArgument(arg.to_owned()))
        }
    }
}

#[doc(hidden)]
pub struct ParseStructPositional<'a> {
    /// The positional's name
    pub name: &'static str,

    /// The function to parse the positional.
    pub slot: &'a mut dyn ParseValueSlot,
}

impl<'a> ParseStructPositional<'a> {
    /// Parses a positional argument.
    ///
    /// - `arg`: The argument supplied by the user.
    fn parse(&mut self, arg: &OsStr) -> Result<(), Error> {
        self.slot.fill_slot(self.name, arg)
    }
}

/// A type to simplify parsing struct subcommands.
///
/// This indirection is necessary to allow abstracting over [`FromArgs`]
/// instances with different generic parameters.
#[doc(hidden)]
pub struct ParseStructSubCommand<'a> {
    /// The subcommand commands.
    pub subcommands: &'static [&'static CommandInfo],

    pub dynamic_subcommands: &'a [&'static CommandInfo],

    /// The function to parse the subcommand arguments.
    #[allow(clippy::type_complexity)]
    pub parse_func: &'a mut dyn FnMut(
        &[&str],
        &[&OsStr],
        Option<&mut dyn ParseGlobalOptions>,
    ) -> Result<(), EarlyExit>,
}

impl<'a> ParseStructSubCommand<'a> {
    fn parse(
        &mut self,
        help: bool,
        cmd_name: &[&str],
        arg: &str,
        remaining_args: &[&OsStr],
        parse_global_opts: &mut dyn ParseGlobalOptions,
    ) -> Result<bool, EarlyExit> {
        for subcommand in self
            .subcommands
            .iter()
            .chain(self.dynamic_subcommands.iter())
        {
            if subcommand.name == arg {
                let mut command = cmd_name.to_owned();
                command.push(subcommand.name);

                let prepended_help;
                let remaining_args = if help {
                    prepended_help = [&[OsStr::new("help")], remaining_args].concat();
                    &prepended_help
                } else {
                    remaining_args
                };

                (self.parse_func)(&command, remaining_args, Some(parse_global_opts))?;

                return Ok(true);
            }
        }

        Ok(false)
    }
}

#[doc(hidden)]
pub trait ParseFlag {
    fn set_flag(&mut self, arg: &str);
}

impl<T: Flag> ParseFlag for T {
    fn set_flag(&mut self, _arg: &str) {
        <T as Flag>::set_flag(self);
    }
}

/// A trait for for slots that reserve space for a value and know how to parse
/// that value from a command-line `&OsStr` argument.
///
/// This trait is only implemented for the type [`ParseValueSlotTy`]. This
/// indirection is necessary to allow abstracting over [`ParseValueSlotTy`]
/// instances with different generic parameters.
#[doc(hidden)]
pub trait ParseValueSlot {
    fn fill_slot(&mut self, arg: &str, value: &OsStr) -> Result<(), Error>;
}

/// The concrete type implementing the [`ParseValueSlot`] trait.
///
/// - `T` is the type to be parsed from a single string.
/// - `Slot` is the type of the container that can hold a value or values of
///   type `T`.
#[doc(hidden)]
pub struct ParseValueSlotTy<Slot, T> {
    /// The slot for a parsed value.
    pub slot: Slot,
    /// The function to parse the value from a string
    pub parse_func: fn(&str, &OsStr) -> Result<T, String>,
}

/// `ParseValueSlotTy<Option<T>, T>` is used as the slot for all non-repeating
/// arguments, both optional and required.
impl<T> ParseValueSlot for ParseValueSlotTy<Option<T>, T> {
    fn fill_slot(&mut self, arg: &str, value: &OsStr) -> Result<(), Error> {
        if self.slot.is_some() {
            return Err(Error::DuplicateOption(arg.to_owned()));
        }
        let parsed = (self.parse_func)(arg, value).map_err(|e| Error::ParseArgument {
            arg: arg.to_owned(),
            value: value.to_owned(),
            msg: e,
        })?;
        self.slot = Some(parsed);
        Ok(())
    }
}

/// `ParseValueSlotTy<Vec<T>, T>` is used as the slot for repeating arguments.
impl<T> ParseValueSlot for ParseValueSlotTy<Vec<T>, T> {
    fn fill_slot(&mut self, arg: &str, value: &OsStr) -> Result<(), Error> {
        let parsed = (self.parse_func)(arg, value).map_err(|e| Error::ParseArgument {
            arg: arg.to_owned(),
            value: value.to_owned(),
            msg: e,
        })?;
        self.slot.push(parsed);
        Ok(())
    }
}

/// A type which can be the receiver of a [`Flag`].
#[doc(hidden)]
pub trait Flag {
    /// Creates a default instance of the flag value;
    fn default() -> Self
    where
        Self: Sized;

    /// Sets the flag. This function is called when the flag is provided.
    fn set_flag(&mut self);
}

impl Flag for bool {
    fn default() -> Self {
        false
    }
    fn set_flag(&mut self) {
        *self = true;
    }
}

impl Flag for Option<bool> {
    fn default() -> Self {
        None
    }

    fn set_flag(&mut self) {
        *self = Some(true);
    }
}

macro_rules! impl_flag_for_integers {
    ($($ty:ty,)*) => {
        $(
            impl Flag for $ty {
                fn default() -> Self {
                    0
                }
                fn set_flag(&mut self) {
                    *self = self.saturating_add(1);
                }
            }
        )*
    }
}

impl_flag_for_integers![u8, u16, u32, u64, u128, i8, i16, i32, i64, i128,];