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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// Internal
use crate::{
    build::{AppSettings as AS, Arg, ArgSettings},
    output::Usage,
    parse::{
        errors::{Error, ErrorKind, Result as ClapResult},
        ArgMatcher, MatchedArg, ParseResult, Parser, ValueType,
    },
    util::{ChildGraph, Id},
    INTERNAL_ERROR_MSG, INVALID_UTF8,
};

pub(crate) struct Validator<'help, 'app, 'parser> {
    p: &'parser mut Parser<'help, 'app>,
    c: ChildGraph<Id>,
}

impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
    pub(crate) fn new(p: &'parser mut Parser<'help, 'app>) -> Self {
        Validator {
            p,
            c: ChildGraph::with_capacity(5),
        }
    }

    pub(crate) fn validate(
        &mut self,
        needs_val_of: ParseResult,
        is_subcmd: bool,
        matcher: &mut ArgMatcher,
    ) -> ClapResult<()> {
        debug!("Validator::validate");
        let mut reqs_validated = false;
        self.p.add_env(matcher)?;
        self.p.add_defaults(matcher)?;
        if let ParseResult::Opt(a) = needs_val_of {
            debug!("Validator::validate: needs_val_of={:?}", a);
            self.validate_required(matcher)?;

            let o = &self.p.app[&a];
            reqs_validated = true;
            let should_err = if let Some(v) = matcher.0.args.get(&o.id) {
                v.vals.is_empty() && !(o.min_vals.is_some() && o.min_vals.unwrap() == 0)
            } else {
                true
            };
            if should_err {
                return Err(Error::empty_value(
                    o,
                    Usage::new(self.p).create_usage_with_title(&[]),
                    self.p.app.color(),
                ));
            }
        }

        if matcher.is_empty()
            && matcher.subcommand_name().is_none()
            && self.p.is_set(AS::ArgRequiredElseHelp)
        {
            let message = self.p.write_help_err()?;
            return Err(Error {
                message,
                kind: ErrorKind::MissingArgumentOrSubcommand,
                info: vec![],
            });
        }
        self.validate_conflicts(matcher)?;
        if !(self.p.is_set(AS::SubcommandsNegateReqs) && is_subcmd || reqs_validated) {
            self.validate_required(matcher)?;
            self.validate_required_unless(matcher)?;
        }
        self.validate_matched_args(matcher)?;

        Ok(())
    }

    fn validate_arg_values(
        &self,
        arg: &Arg,
        ma: &MatchedArg,
        matcher: &ArgMatcher,
    ) -> ClapResult<()> {
        debug!("Validator::validate_arg_values: arg={:?}", arg.name);
        for val in &ma.vals {
            if self.p.is_set(AS::StrictUtf8) && val.to_str().is_none() {
                debug!(
                    "Validator::validate_arg_values: invalid UTF-8 found in val {:?}",
                    val
                );
                return Err(Error::invalid_utf8(
                    Usage::new(self.p).create_usage_with_title(&[]),
                    self.p.app.color(),
                ));
            }
            if !arg.possible_vals.is_empty() {
                debug!(
                    "Validator::validate_arg_values: possible_vals={:?}",
                    arg.possible_vals
                );
                let val_str = val.to_string_lossy();
                let ok = if arg.is_set(ArgSettings::IgnoreCase) {
                    arg.possible_vals
                        .iter()
                        .any(|pv| pv.eq_ignore_ascii_case(&val_str))
                } else {
                    arg.possible_vals.contains(&&*val_str)
                };
                if !ok {
                    let used: Vec<Id> = matcher
                        .arg_names()
                        .filter(|&n| {
                            self.p.app.find(n).map_or(true, |a| {
                                !(a.is_set(ArgSettings::Hidden) || self.p.required.contains(&a.id))
                            })
                        })
                        .cloned()
                        .collect();
                    return Err(Error::invalid_value(
                        val_str.to_string(),
                        &arg.possible_vals,
                        arg,
                        Usage::new(self.p).create_usage_with_title(&used),
                        self.p.app.color(),
                    ));
                }
            }
            if !arg.is_set(ArgSettings::AllowEmptyValues)
                && val.is_empty()
                && matcher.contains(&arg.id)
            {
                debug!("Validator::validate_arg_values: illegal empty val found");
                return Err(Error::empty_value(
                    arg,
                    Usage::new(self.p).create_usage_with_title(&[]),
                    self.p.app.color(),
                ));
            }

            // FIXME: `(&mut *vtor)(args...)` can be simplified to `vtor(args...)`
            //      once https://github.com/rust-lang/rust/pull/72280 is landed on stable
            //      (about 10 weeks from now)
            if let Some(ref vtor) = arg.validator {
                debug!("Validator::validate_arg_values: checking validator...");
                let mut vtor = vtor.lock().unwrap();
                if let Err(e) = (&mut *vtor)(&*val.to_string_lossy()) {
                    debug!("error");
                    return Err(Error::value_validation(
                        arg.to_string(),
                        val.to_string_lossy().to_string(),
                        e,
                        self.p.app.color(),
                    ));
                } else {
                    debug!("good");
                }
            }
            if let Some(ref vtor) = arg.validator_os {
                debug!("Validator::validate_arg_values: checking validator_os...");
                let mut vtor = vtor.lock().unwrap();
                if let Err(e) = (&mut *vtor)(val) {
                    debug!("error");
                    return Err(Error::value_validation(
                        arg.to_string(),
                        val.to_string_lossy().into(),
                        e,
                        self.p.app.color(),
                    ));
                } else {
                    debug!("good");
                }
            }
        }
        Ok(())
    }

    fn build_conflict_err_usage(
        &self,
        matcher: &ArgMatcher,
        retained_arg: &Arg,
        conflicting_key: &Id,
    ) -> String {
        let retained_blacklist = &retained_arg.blacklist;
        let used_filtered: Vec<Id> = matcher
            .arg_names()
            .filter(|key| *key != conflicting_key)
            .filter(|key| !retained_blacklist.contains(key))
            .cloned()
            .collect();
        let required: Vec<Id> = used_filtered
            .iter()
            .filter_map(|key| self.p.app.find(key))
            .flat_map(|key_arg| key_arg.requires.iter().map(|item| &item.1))
            .filter(|item| !used_filtered.contains(item))
            .filter(|key| *key != conflicting_key)
            .filter(|key| !retained_blacklist.contains(key))
            .chain(used_filtered.iter())
            .cloned()
            .collect();
        Usage::new(self.p).create_usage_with_title(&required)
    }

    fn build_conflict_err(&self, name: &Id, matcher: &ArgMatcher) -> ClapResult<()> {
        debug!("Validator::build_conflict_err: name={:?}", name);
        if let Some(checked_arg) = self.p.app.find(name) {
            for k in matcher.arg_names() {
                if let Some(a) = self.p.app.find(k) {
                    if a.blacklist.contains(&name) {
                        let (_former, former_arg, latter, latter_arg) = {
                            let name_pos = matcher.arg_names().position(|key| key == name);
                            let k_pos = matcher.arg_names().position(|key| key == k);
                            if name_pos < k_pos {
                                (name, checked_arg, k, a)
                            } else {
                                (k, a, name, checked_arg)
                            }
                        };
                        let usg = self.build_conflict_err_usage(matcher, former_arg, latter);
                        return Err(Error::argument_conflict(
                            latter_arg,
                            Some(former_arg.to_string()),
                            usg,
                            self.p.app.color(),
                        ));
                    }
                }
            }
        } else if let Some(g) = self.p.app.groups.iter().find(|x| x.id == *name) {
            let usg = Usage::new(self.p).create_usage_with_title(&[]);
            let args_in_group = self.p.app.unroll_args_in_group(&g.id);
            let first = matcher
                .arg_names()
                .find(|x| args_in_group.contains(x))
                .expect(INTERNAL_ERROR_MSG);
            let c_with = matcher
                .arg_names()
                .find(|x| x != &first && args_in_group.contains(x))
                .map(|x| self.p.app[x].to_string());
            debug!("Validator::build_conflict_err: c_with={:?}:group", c_with);
            return Err(Error::argument_conflict(
                &self.p.app[first],
                c_with,
                usg,
                self.p.app.color(),
            ));
        }

        panic!(INTERNAL_ERROR_MSG);
    }

    fn validate_conflicts(&mut self, matcher: &mut ArgMatcher) -> ClapResult<()> {
        debug!("Validator::validate_conflicts");
        self.validate_exclusive(matcher)?;
        self.gather_conflicts(matcher);

        for name in self.c.iter() {
            debug!("Validator::validate_conflicts:iter:{:?}", name);
            let mut should_err = false;
            if let Some(g) = self
                .p
                .app
                .groups
                .iter()
                .find(|g| !g.multiple && &g.id == name)
            {
                let conf_with_self = self
                    .p
                    .app
                    .unroll_args_in_group(&g.id)
                    .iter()
                    .filter(|&a| matcher.contains(a))
                    .count()
                    > 1;

                let conf_with_arg = g.conflicts.iter().any(|x| matcher.contains(x));

                let arg_conf_with_gr = matcher
                    .arg_names()
                    .filter_map(|x| self.p.app.find(x))
                    .any(|x| x.blacklist.iter().any(|c| *c == g.id));

                should_err = conf_with_self || conf_with_arg || arg_conf_with_gr;
            } else if let Some(ma) = matcher.get(name) {
                debug!(
                    "Validator::validate_conflicts:iter:{:?}: matcher contains it...",
                    name
                );
                should_err = ma.occurs > 0;
            }
            if should_err {
                return self.build_conflict_err(name, matcher);
            }
        }
        Ok(())
    }

    fn validate_exclusive(&mut self, matcher: &mut ArgMatcher) -> ClapResult<()> {
        debug!("Validator::validate_exclusive");
        let args_count = matcher.arg_names().count();
        for name in matcher.arg_names() {
            debug!("Validator::validate_exclusive:iter:{:?}", name);
            if let Some(arg) = self.p.app.find(name) {
                if arg.exclusive && args_count > 1 {
                    let c_with: Option<String> = None;
                    return Err(Error::argument_conflict(
                        arg,
                        c_with,
                        Usage::new(self.p).create_usage_with_title(&[]),
                        self.p.app.color(),
                    ));
                }
            }
        }
        Ok(())
    }

    // Gathers potential conflicts based on used argument, but without considering requirements
    // and such
    fn gather_conflicts(&mut self, matcher: &mut ArgMatcher) {
        debug!("Validator::gather_conflicts");
        for name in matcher.arg_names() {
            debug!("Validator::gather_conflicts:iter: id={:?}", name);
            // if arg is "present" only because it got default value
            // it doesn't conflict with anything
            //
            // TODO: @refactor Do it in a more elegant way
            if matcher
                .get(name)
                .map_or(false, |a| a.ty == ValueType::DefaultValue)
            {
                debug!("Validator::gather_conflicts:iter: This is default value, skipping.",);
                continue;
            }

            if let Some(arg) = self.p.app.find(name) {
                // Since an arg was used, every arg it conflicts with is added to the conflicts
                for conf in &arg.blacklist {
                    if self.p.app.find(conf).is_some() {
                        if conf != name {
                            self.c.insert(conf.clone());
                        }
                    } else {
                        // for g_arg in self.p.app.unroll_args_in_group(conf) {
                        //     if &g_arg != name {
                        self.c.insert(conf.clone()); // TODO ERROR is here - groups allow one arg but this line disallows all group args
                                                     //     }
                                                     // }
                    }
                }

                // Now we need to know which groups this arg was a member of, to add all other
                // args in that group to the conflicts, as well as any args those args conflict
                // with

                for grp in self.p.app.groups_for_arg(&name) {
                    if let Some(g) = self
                        .p
                        .app
                        .groups
                        .iter()
                        .find(|g| !g.multiple && g.id == grp)
                    {
                        // for g_arg in self.p.app.unroll_args_in_group(&g.name) {
                        //     if &g_arg != name {
                        self.c.insert(g.id.clone());
                        //     }
                        // }
                    }
                }
            } else if let Some(g) = self
                .p
                .app
                .groups
                .iter()
                .find(|g| !g.multiple && g.id == *name)
            {
                debug!("Validator::gather_conflicts:iter:{:?}:group", name);
                self.c.insert(g.id.clone());
            }
        }
    }

    fn gather_requirements(&mut self, matcher: &ArgMatcher) {
        debug!("Validator::gather_requirements");
        for name in matcher.arg_names() {
            debug!("Validator::gather_requirements:iter:{:?}", name);
            if let Some(arg) = self.p.app.find(name) {
                for req in self.p.app.unroll_requirements_for_arg(&arg.id, matcher) {
                    self.p.required.insert(req);
                }
            } else if let Some(g) = self.p.app.groups.iter().find(|grp| grp.id == *name) {
                debug!("Validator::gather_conflicts:iter:{:?}:group", name);
                for r in &g.requires {
                    self.p.required.insert(r.clone());
                }
            }
        }
    }

    fn validate_matched_args(&self, matcher: &mut ArgMatcher) -> ClapResult<()> {
        debug!("Validator::validate_matched_args");
        for (name, ma) in matcher.iter() {
            debug!(
                "Validator::validate_matched_args:iter:{:?}: vals={:#?}",
                name, ma.vals
            );
            if let Some(arg) = self.p.app.find(name) {
                self.validate_arg_num_vals(arg, ma)?;
                self.validate_arg_values(arg, ma, matcher)?;
                self.validate_arg_requires(arg, ma, matcher)?;
                self.validate_arg_num_occurs(arg, ma)?;
            } else {
                let grp = self
                    .p
                    .app
                    .groups
                    .iter()
                    .find(|g| g.id == *name)
                    .expect(INTERNAL_ERROR_MSG);
                if grp.requires.iter().any(|n| !matcher.contains(n)) {
                    return self.missing_required_error(matcher, Some(name));
                }
            }
        }
        Ok(())
    }

    fn validate_arg_num_occurs(&self, a: &Arg, ma: &MatchedArg) -> ClapResult<()> {
        debug!(
            "Validator::validate_arg_num_occurs: {:?}={}",
            a.name, ma.occurs
        );
        if ma.occurs > 1 && !a.is_set(ArgSettings::MultipleOccurrences) {
            // Not the first time, and we don't allow multiples
            return Err(Error::unexpected_multiple_usage(
                a,
                Usage::new(self.p).create_usage_with_title(&[]),
                self.p.app.color(),
            ));
        }
        Ok(())
    }

    fn validate_arg_num_vals(&self, a: &Arg, ma: &MatchedArg) -> ClapResult<()> {
        debug!("Validator::validate_arg_num_vals");
        if let Some(num) = a.num_vals {
            debug!("Validator::validate_arg_num_vals: num_vals set...{}", num);
            let should_err = if a.is_set(ArgSettings::MultipleValues) {
                ((ma.vals.len() as u64) % num) != 0
            } else {
                num != (ma.vals.len() as u64)
            };
            if should_err {
                debug!("Validator::validate_arg_num_vals: Sending error WrongNumberOfValues");
                return Err(Error::wrong_number_of_values(
                    a,
                    num,
                    if a.is_set(ArgSettings::MultipleValues) {
                        ma.vals.len() % num as usize
                    } else {
                        ma.vals.len()
                    },
                    Usage::new(self.p).create_usage_with_title(&[]),
                    self.p.app.color(),
                ));
            }
        }
        if let Some(num) = a.max_vals {
            debug!("Validator::validate_arg_num_vals: max_vals set...{}", num);
            if (ma.vals.len() as u64) > num {
                debug!("Validator::validate_arg_num_vals: Sending error TooManyValues");
                return Err(Error::too_many_values(
                    ma.vals
                        .iter()
                        .last()
                        .expect(INTERNAL_ERROR_MSG)
                        .to_str()
                        .expect(INVALID_UTF8)
                        .to_string(),
                    a,
                    Usage::new(self.p).create_usage_with_title(&[]),
                    self.p.app.color(),
                ));
            }
        }
        let min_vals_zero = if let Some(num) = a.min_vals {
            debug!("Validator::validate_arg_num_vals: min_vals set: {}", num);
            if (ma.vals.len() as u64) < num && num != 0 {
                debug!("Validator::validate_arg_num_vals: Sending error TooFewValues");
                return Err(Error::too_few_values(
                    a,
                    num,
                    ma.vals.len(),
                    Usage::new(self.p).create_usage_with_title(&[]),
                    self.p.app.color(),
                ));
            }
            num == 0
        } else {
            false
        };
        // Issue 665 (https://github.com/kbknapp/clap-rs/issues/665)
        // Issue 1105 (https://github.com/kbknapp/clap-rs/issues/1105)
        if a.is_set(ArgSettings::TakesValue) && !min_vals_zero && ma.vals.is_empty() {
            return Err(Error::empty_value(
                a,
                Usage::new(self.p).create_usage_with_title(&[]),
                self.p.app.color(),
            ));
        }
        Ok(())
    }

    fn validate_arg_requires(
        &self,
        a: &Arg<'help>,
        ma: &MatchedArg,
        matcher: &ArgMatcher,
    ) -> ClapResult<()> {
        debug!("Validator::validate_arg_requires:{:?}", a.name);
        for (val, name) in &a.requires {
            if let Some(val) = val {
                let missing_req = |v| v == val && !matcher.contains(&name);
                if ma.vals.iter().any(missing_req) {
                    return self.missing_required_error(matcher, Some(&a.id));
                }
            } else if !matcher.contains(&name) {
                return self.missing_required_error(matcher, Some(&name));
            }
        }
        Ok(())
    }

    fn validate_required(&mut self, matcher: &ArgMatcher) -> ClapResult<()> {
        debug!(
            "Validator::validate_required: required={:?}",
            self.p.required
        );
        self.gather_requirements(matcher);

        for arg_or_group in self.p.required.iter().filter(|r| !matcher.contains(r)) {
            debug!("Validator::validate_required:iter:aog={:?}", arg_or_group);
            if let Some(arg) = self.p.app.find(&arg_or_group) {
                debug!("Validator::validate_required:iter: This is an arg");
                if !self.is_missing_required_ok(arg, matcher) {
                    return self.missing_required_error(matcher, None);
                }
            } else if let Some(group) = self.p.app.groups.iter().find(|g| g.id == *arg_or_group) {
                debug!("Validator::validate_required:iter: This is a group");
                if !self
                    .p
                    .app
                    .unroll_args_in_group(&group.id)
                    .iter()
                    .any(|a| matcher.contains(a))
                {
                    return self.missing_required_error(matcher, None);
                }
            }
        }

        // Validate the conditionally required args
        for a in self.p.app.args.args.iter() {
            for (other, val) in &a.r_ifs {
                if let Some(ma) = matcher.get(other) {
                    if ma.contains_val(val) && !matcher.contains(&a.id) {
                        return self.missing_required_error(matcher, Some(&a.id));
                    }
                }
            }
        }
        Ok(())
    }

    fn is_missing_required_ok(&self, a: &Arg<'help>, matcher: &ArgMatcher) -> bool {
        debug!("Validator::is_missing_required_ok: {}", a.name);
        self.validate_arg_conflicts(a, matcher) || self.p.overridden.contains(&a.id)
    }

    fn validate_arg_conflicts(&self, a: &Arg<'help>, matcher: &ArgMatcher) -> bool {
        debug!("Validator::validate_arg_conflicts: a={:?}", a.name);
        a.blacklist.iter().any(|conf| {
            matcher.contains(conf)
                || self
                    .p
                    .app
                    .groups
                    .iter()
                    .find(|g| g.id == *conf)
                    .map_or(false, |g| g.args.iter().any(|arg| matcher.contains(arg)))
        })
    }

    fn validate_required_unless(&self, matcher: &ArgMatcher) -> ClapResult<()> {
        debug!("Validator::validate_required_unless");
        for a in self
            .p
            .app
            .args
            .args
            .iter()
            .filter(|a| !a.r_unless.is_empty())
            .filter(|a| !matcher.contains(&a.id))
        {
            debug!("Validator::validate_required_unless:iter:{}", a.name);
            if self.fails_arg_required_unless(a, matcher) {
                return self.missing_required_error(matcher, Some(&a.id));
            }
        }

        Ok(())
    }

    // Failing a required unless means, the arg's "unless" wasn't present, and neither were they
    fn fails_arg_required_unless(&self, a: &Arg<'help>, matcher: &ArgMatcher) -> bool {
        debug!("Validator::fails_arg_required_unless: a={:?}", a.name);
        if a.is_set(ArgSettings::RequiredUnlessAll) {
            debug!("Validator::fails_arg_required_unless:{}:All", a.name);
            !a.r_unless.iter().all(|id| matcher.contains(id))
        } else {
            debug!("Validator::fails_arg_required_unless:{}:Any", a.name);
            !a.r_unless.iter().any(|id| matcher.contains(id))
        }
    }

    // `incl`: an arg to include in the error even if not used
    fn missing_required_error(&self, matcher: &ArgMatcher, incl: Option<&Id>) -> ClapResult<()> {
        debug!("Validator::missing_required_error; incl={:?}", incl);
        debug!(
            "Validator::missing_required_error: reqs={:?}",
            self.p.required
        );

        let usg = Usage::new(self.p);

        let req_args = if let Some(x) = incl {
            usg.get_required_usage_from(&[x.clone()], Some(matcher), true)
        } else {
            usg.get_required_usage_from(&[], Some(matcher), true)
        };

        debug!(
            "Validator::missing_required_error: req_args={:#?}",
            req_args
        );

        let used: Vec<Id> = matcher
            .arg_names()
            .filter(|n| {
                self.p.app.find(n).map_or(true, |a| {
                    !(a.is_set(ArgSettings::Hidden) || self.p.required.contains(&a.id))
                })
            })
            .cloned()
            .chain(incl.cloned())
            .collect();

        Err(Error::missing_required_argument(
            req_args,
            usg.create_usage_with_title(&*used),
            self.p.app.color(),
        ))
    }
}