    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
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408
    1409
    1410
    1411
    1412
    1413
    1414
    1415
    1416
    1417
    1418
    1419
    1420
    1421
    1422
    1423
    1424
    1425
    1426
    1427
    1428
    1429
    1430
    1431
    1432
    1433
    1434
    1435
    1436
    1437
    1438
    1439
    1440
    1441
    1442
    1443
    1444
    1445
    1446
    1447
    1448
    1449
    1450
    1451
    1452
    1453
    1454
    1455
    1456
    1457
    1458
    1459
    1460
    1461
    1462
    1463
    1464
    1465
    1466
    1467
    1468
    1469
    1470
    1471
    1472
    1473
    1474
    1475
    1476
    1477
    1478
    1479
    1480
    1481
    1482
    1483
    1484
    1485
    1486
    1487
    1488
    1489
    1490
    1491
    1492
    1493
    1494
    1495
    1496
    1497
    1498
    1499
    1500
    1501
    1502
    1503
    1504
    1505
    1506
    1507
    1508
    1509
    1510
    1511
    1512
    1513
    1514
    1515
    1516
    1517
    1518
    1519
    1520
    1521
    1522
    1523
    1524
    1525
    1526
    1527
    1528
    1529
    1530
    1531
    1532
    1533
    1534
    1535
    1536
    1537
    1538
    1539
    1540
    1541
    1542
    1543
    1544
    1545
    1546
    1547
    1548
    1549
    1550
    1551
    1552
    1553
    1554
    1555
    1556
    1557
    1558
    1559
    1560
    1561
    1562
    1563
    1564
    1565
    1566
    1567
    1568
    1569
    1570
    1571
    1572
    1573
    1574
    1575
    1576
    1577
    1578
    1579
    1580
    1581
    1582
    1583
    1584
    1585
    1586
    1587
    1588
    1589
    1590
    1591
    1592
    1593
    1594
    1595
    1596
    1597
    1598
    1599
    1600
    1601
    1602
    1603
    1604
    1605
    1606
    1607
    1608
    1609
    1610
    1611
    1612
    1613
    1614
    1615
    1616
    1617
    1618
    1619
    1620
    1621
    1622
    1623
    1624
    1625
    1626
    1627
    1628
    1629
    1630
    1631
    1632
    1633
    1634
    1635
    1636
    1637
    1638
    1639
    1640
    1641
    1642
    1643

    // Std
    use std::{
        cell::Cell,
        ffi::{OsStr, OsString},
    };

    // Third Party
    use clap_lex::RawOsStr;
    use clap_lex::RawOsString;

    // Internal
    use crate::builder::{Arg, Command};
    use crate::error::Error as ClapError;
    use crate::error::Result as ClapResult;
    use crate::mkeymap::KeyType;
    use crate::output::Usage;
    use crate::parser::features::suggestions;
    use crate::parser::AnyValue;
    use crate::parser::{ArgMatcher, SubCommand};
    use crate::parser::{Validator, ValueSource};
    use crate::util::Id;
    use crate::ArgAction;
    use crate::INTERNAL_ERROR_MSG;

    pub(crate) struct Parser<'cmd> {
        cmd: &'cmd mut Command,
        cur_idx: Cell<usize>,
        /// Index of the previous flag subcommand in a group of flags.
        flag_subcmd_at: Option<usize>,
        /// Counter indicating the number of items to skip
        /// when revisiting the group of flags which includes the flag subcommand.
        flag_subcmd_skip: usize,
    }

    // Initializing Methods
    impl<'cmd> Parser<'cmd> {
        pub(crate) fn new(cmd: &'cmd mut Command) -> Self {
            Parser {
                cmd,
                cur_idx: Cell::new(0),
                flag_subcmd_at: None,
                flag_subcmd_skip: 0,
            }
        }
    }

    // Parsing Methods
    impl<'cmd> Parser<'cmd> {
        // The actual parsing function
        #[allow(clippy::cognitive_complexity)]
        pub(crate) fn get_matches_with(
            &mut self,
            matcher: &mut ArgMatcher,
            raw_args: &mut clap_lex::RawArgs,
            mut args_cursor: clap_lex::ArgCursor,
        ) -> ClapResult<()> {
            debug!("Parser::get_matches_with");
            // Verify all positional assertions pass

            let mut subcmd_name: Option<String> = None;
            let mut keep_state = false;
            let mut parse_state = ParseState::ValuesDone;
            let mut pos_counter = 1;

            // Already met any valid arg(then we shouldn't expect subcommands after it).
            let mut valid_arg_found = false;
            // If the user already passed '--'. Meaning only positional args follow.
            let mut trailing_values = false;

            // Count of positional args
            let positional_count = self
                .cmd
                .get_keymap()
                .keys()
                .filter(|x| x.is_position())
                .count();
            // If any arg sets .last(true)
            let contains_last = self.cmd.get_arguments().any(|x| x.is_last_set());

            while let Some(arg_os) = raw_args.next(&mut args_cursor) {
                // Recover the replaced items if any.
                if let Some(replaced_items) = arg_os
                    .to_value()
                    .ok()
                    .and_then(|a| self.cmd.get_replacement(a))
                {
                    debug!(
                        "Parser::get_matches_with: found replacer: {:?}, target: {:?}",
                        arg_os, replaced_items
                    );
                    raw_args.insert(&args_cursor, replaced_items);
                    continue;
                }

                debug!(
                    "Parser::get_matches_with: Begin parsing '{:?}' ({:?})",
                    arg_os.to_value_os(),
                    arg_os.to_value_os().as_raw_bytes()
                );

                // Has the user already passed '--'? Meaning only positional args follow
                if !trailing_values {
                    if self.cmd.is_subcommand_precedence_over_arg_set()
                        || !matches!(parse_state, ParseState::Opt(_) | ParseState::Pos(_))
                    {
                        // Does the arg match a subcommand name, or any of its aliases (if defined)
                        let sc_name = self.possible_subcommand(arg_os.to_value(), valid_arg_found);
                        debug!("Parser::get_matches_with: sc={:?}", sc_name);
                        if let Some(sc_name) = sc_name {
                            if sc_name == "help" && !self.cmd.is_disable_help_subcommand_set() {
                                ok!(self.parse_help_subcommand(raw_args.remaining(&mut args_cursor)));
                                unreachable!("`parse_help_subcommand` always errors");
                            } else {
                                subcmd_name = Some(sc_name.to_owned());
                            }
                            break;
                        }
                    }

                    if arg_os.is_escape() {
                        if matches!(&parse_state, ParseState::Opt(opt) | ParseState::Pos(opt) if
                            self.cmd[opt].is_allow_hyphen_values_set())
                        {
                            // ParseResult::MaybeHyphenValue, do nothing
                        } else {
                            debug!("Parser::get_matches_with: setting TrailingVals=true");
                            trailing_values = true;
                            matcher.start_trailing();
                            continue;
                        }
                    } else if let Some((long_arg, long_value)) = arg_os.to_long() {
                        let parse_result = ok!(self.parse_long_arg(
                            matcher,
                            long_arg,
                            long_value,
                            &parse_state,
                            pos_counter,
                            &mut valid_arg_found,
                        ));
                        debug!(
                            "Parser::get_matches_with: After parse_long_arg {:?}",
                            parse_result
                        );
                        match parse_result {
                            ParseResult::NoArg => {
                                unreachable!("`to_long` always has the flag specified")
                            }
                            ParseResult::ValuesDone => {
                                parse_state = ParseState::ValuesDone;
                                continue;
                            }
                            ParseResult::Opt(id) => {
                                parse_state = ParseState::Opt(id);
                                continue;
                            }
                            ParseResult::FlagSubCommand(name) => {
                                debug!(
                                    "Parser::get_matches_with: FlagSubCommand found in long arg {:?}",
                                    &name
                                );
                                subcmd_name = Some(name);
                                break;
                            }
                            ParseResult::EqualsNotProvided { arg } => {
                                let _ = self.resolve_pending(matcher);
                                return Err(ClapError::no_equals(
                                    self.cmd,
                                    arg,
                                    Usage::new(self.cmd).create_usage_with_title(&[]),
                                ));
                            }
                            ParseResult::NoMatchingArg { arg } => {
                                let _ = self.resolve_pending(matcher);
                                let remaining_args: Vec<_> =
                                    raw_args.remaining(&mut args_cursor).collect();
                                return Err(self.did_you_mean_error(
                                    &arg,
                                    matcher,
                                    &remaining_args,
                                    trailing_values,
                                ));
                            }
                            ParseResult::UnneededAttachedValue { rest, used, arg } => {
                                let _ = self.resolve_pending(matcher);
                                return Err(ClapError::too_many_values(
                                    self.cmd,
                                    rest,
                                    arg,
                                    Usage::new(self.cmd).create_usage_with_title(&used),
                                ));
                            }
                            ParseResult::MaybeHyphenValue => {
                                // Maybe a hyphen value, do nothing.
                            }
                            ParseResult::AttachedValueNotConsumed => {
                                unreachable!()
                            }
                        }
                    } else if let Some(short_arg) = arg_os.to_short() {
                        // Arg looks like a short flag, and not a possible number

                        // Try to parse short args like normal, if allow_hyphen_values or
                        // AllowNegativeNumbers is set, parse_short_arg will *not* throw
                        // an error, and instead return Ok(None)
                        let parse_result = ok!(self.parse_short_arg(
                            matcher,
                            short_arg,
                            &parse_state,
                            pos_counter,
                            &mut valid_arg_found,
                        ));
                        // If it's None, we then check if one of those two AppSettings was set
                        debug!(
                            "Parser::get_matches_with: After parse_short_arg {:?}",
                            parse_result
                        );
                        match parse_result {
                            ParseResult::NoArg => {
                                // Is a single dash `-`, try positional.
                            }
                            ParseResult::ValuesDone => {
                                parse_state = ParseState::ValuesDone;
                                continue;
                            }
                            ParseResult::Opt(id) => {
                                parse_state = ParseState::Opt(id);
                                continue;
                            }
                            ParseResult::FlagSubCommand(name) => {
                                // If there are more short flags to be processed, we should keep the state, and later
                                // revisit the current group of short flags skipping the subcommand.
                                keep_state = self
                                    .flag_subcmd_at
                                    .map(|at| {
                                        raw_args
                                            .seek(&mut args_cursor, clap_lex::SeekFrom::Current(-1));
                                        // Since we are now saving the current state, the number of flags to skip during state recovery should
                                        // be the current index (`cur_idx`) minus ONE UNIT TO THE LEFT of the starting position.
                                        self.flag_subcmd_skip = self.cur_idx.get() - at + 1;
                                    })
                                    .is_some();

                                debug!(
                                    "Parser::get_matches_with:FlagSubCommandShort: subcmd_name={}, keep_state={}, flag_subcmd_skip={}",
                                    name,
                                    keep_state,
                                    self.flag_subcmd_skip
                                );

                                subcmd_name = Some(name);
                                break;
                            }
                            ParseResult::EqualsNotProvided { arg } => {
                                let _ = self.resolve_pending(matcher);
                                return Err(ClapError::no_equals(
                                    self.cmd,
                                    arg,
                                    Usage::new(self.cmd).create_usage_with_title(&[]),
                                ));
                            }
                            ParseResult::NoMatchingArg { arg } => {
                                let _ = self.resolve_pending(matcher);
                                // We already know it looks like a flag
                                let suggested_trailing_arg =
                                    !trailing_values && self.cmd.has_positionals();
                                return Err(ClapError::unknown_argument(
                                    self.cmd,
                                    arg,
                                    None,
                                    suggested_trailing_arg,
                                    Usage::new(self.cmd).create_usage_with_title(&[]),
                                ));
                            }
                            ParseResult::MaybeHyphenValue => {
                                // Maybe a hyphen value, do nothing.
                            }
                            ParseResult::UnneededAttachedValue { .. }
                            | ParseResult::AttachedValueNotConsumed => unreachable!(),
                        }
                    }

                    if let ParseState::Opt(id) = &parse_state {
                        // Assume this is a value of a previous arg.

                        // get the option so we can check the settings
                        let arg = &self.cmd[id];
                        let parse_result = if let Some(parse_result) =
                            self.check_terminator(arg, arg_os.to_value_os())
                        {
                            parse_result
                        } else {
                            let trailing_values = false;
                            let arg_values = matcher.pending_values_mut(id, None, trailing_values);
                            arg_values.push(arg_os.to_value_os().to_os_str().into_owned());
                            if matcher.needs_more_vals(arg) {
                                ParseResult::Opt(arg.get_id().clone())
                            } else {
                                ParseResult::ValuesDone
                            }
                        };
                        parse_state = match parse_result {
                            ParseResult::Opt(id) => ParseState::Opt(id),
                            ParseResult::ValuesDone => ParseState::ValuesDone,
                            _ => unreachable!(),
                        };
                        // get the next value from the iterator
                        continue;
                    }
                }

                // Correct pos_counter.
                pos_counter = {
                    let is_second_to_last = pos_counter + 1 == positional_count;

                    // The last positional argument, or second to last positional
                    // argument may be set to .multiple_values(true) or `.multiple_occurrences(true)`
                    let low_index_mults = is_second_to_last
                        && self.cmd.get_positionals().any(|a| {
                            a.is_multiple() && (positional_count != a.get_index().unwrap_or(0))
                        })
                        && self
                            .cmd
                            .get_positionals()
                            .last()
                            .map_or(false, |p_name| !p_name.is_last_set());

                    let missing_pos = self.cmd.is_allow_missing_positional_set()
                        && is_second_to_last
                        && !trailing_values;

                    debug!(
                        "Parser::get_matches_with: Positional counter...{}",
                        pos_counter
                    );
                    debug!(
                        "Parser::get_matches_with: Low index multiples...{:?}",
                        low_index_mults
                    );

                    if low_index_mults || missing_pos {
                        let skip_current = if let Some(n) = raw_args.peek(&args_cursor) {
                            if let Some(arg) = self
                                .cmd
                                .get_positionals()
                                .find(|a| a.get_index() == Some(pos_counter))
                            {
                                // If next value looks like a new_arg or it's a
                                // subcommand, skip positional argument under current
                                // pos_counter(which means current value cannot be a
                                // positional argument with a value next to it), assume
                                // current value matches the next arg.
                                self.is_new_arg(&n, arg)
                                    || self
                                        .possible_subcommand(n.to_value(), valid_arg_found)
                                        .is_some()
                            } else {
                                true
                            }
                        } else {
                            true
                        };

                        if skip_current {
                            debug!("Parser::get_matches_with: Bumping the positional counter...");
                            pos_counter + 1
                        } else {
                            pos_counter
                        }
                    } else if trailing_values
                        && (self.cmd.is_allow_missing_positional_set() || contains_last)
                    {
                        // Came to -- and one positional has .last(true) set, so we go immediately
                        // to the last (highest index) positional
                        debug!("Parser::get_matches_with: .last(true) and --, setting last pos");
                        positional_count
                    } else {
                        pos_counter
                    }
                };

                if let Some(arg) = self.cmd.get_keymap().get(&pos_counter) {
                    if arg.is_last_set() && !trailing_values {
                        let _ = self.resolve_pending(matcher);
                        // Its already considered a positional, we don't need to suggest turning it
                        // into one
                        let suggested_trailing_arg = false;
                        return Err(ClapError::unknown_argument(
                            self.cmd,
                            arg_os.display().to_string(),
                            None,
                            suggested_trailing_arg,
                            Usage::new(self.cmd).create_usage_with_title(&[]),
                        ));
                    }

                    if arg.is_trailing_var_arg_set() {
                        trailing_values = true;
                    }

                    if matcher.pending_arg_id() != Some(arg.get_id()) || !arg.is_multiple_values_set() {
                        ok!(self.resolve_pending(matcher));
                    }
                    if let Some(_parse_result) = self.check_terminator(arg, arg_os.to_value_os()) {
                        debug!(
                            "Parser::get_matches_with: ignoring terminator result {:?}",
                            _parse_result
                        );
                    } else {
                        let arg_values = matcher.pending_values_mut(
                            arg.get_id(),
                            Some(Identifier::Index),
                            trailing_values,
                        );
                        arg_values.push(arg_os.to_value_os().to_os_str().into_owned());
                    }

                    // Only increment the positional counter if it doesn't allow multiples
                    if !arg.is_multiple() {
                        pos_counter += 1;
                        parse_state = ParseState::ValuesDone;
                    } else {
                        parse_state = ParseState::Pos(arg.get_id().clone());
                    }
                    valid_arg_found = true;
                } else if let Some(external_parser) =
                    self.cmd.get_external_subcommand_value_parser().cloned()
                {
                    // Get external subcommand name
                    let sc_name = match arg_os.to_value() {
                        Ok(s) => s.to_owned(),
                        Err(_) => {
                            let _ = self.resolve_pending(matcher);
                            return Err(ClapError::invalid_utf8(
                                self.cmd,
                                Usage::new(self.cmd).create_usage_with_title(&[]),
                            ));
                        }
                    };

                    // Collect the external subcommand args
                    let mut sc_m = ArgMatcher::new(self.cmd);
                    sc_m.start_occurrence_of_external(self.cmd);

                    for raw_val in raw_args.remaining(&mut args_cursor) {
                        let val = ok!(external_parser.parse_ref(self.cmd, None, raw_val));
                        let external_id = Id::from_static_ref(Id::EXTERNAL);
                        sc_m.add_val_to(&external_id, val, raw_val.to_os_string());
                    }

                    matcher.subcommand(SubCommand {
                        name: sc_name,
                        matches: sc_m.into_inner(),
                    });

                    ok!(self.resolve_pending(matcher));
                    #[cfg(feature = "env")]
                    ok!(self.add_env(matcher));
                    ok!(self.add_defaults(matcher));
                    return Validator::new(self.cmd).validate(parse_state, matcher);
                } else {
                    // Start error processing
                    let _ = self.resolve_pending(matcher);
                    return Err(self.match_arg_error(&arg_os, valid_arg_found, trailing_values));
                }
            }

            if let Some(ref pos_sc_name) = subcmd_name {
                let sc_name = self
                    .cmd
                    .find_subcommand(pos_sc_name)
                    .expect(INTERNAL_ERROR_MSG)
                    .get_name()
                    .to_owned();
                ok!(self.parse_subcommand(&sc_name, matcher, raw_args, args_cursor, keep_state));
            }

            ok!(self.resolve_pending(matcher));
            #[cfg(feature = "env")]
            ok!(self.add_env(matcher));
            ok!(self.add_defaults(matcher));
            Validator::new(self.cmd).validate(parse_state, matcher)
        }

        fn match_arg_error(
            &self,
            arg_os: &clap_lex::ParsedArg<'_>,
            valid_arg_found: bool,
            trailing_values: bool,
        ) -> ClapError {
            // If argument follows a `--`
            if trailing_values {
                // If the arg matches a subcommand name, or any of its aliases (if defined)
                if self
                    .possible_subcommand(arg_os.to_value(), valid_arg_found)
                    .is_some()
                {
                    return ClapError::unnecessary_double_dash(
                        self.cmd,
                        arg_os.display().to_string(),
                        Usage::new(self.cmd).create_usage_with_title(&[]),
                    );
                }
            }

            if !(self.cmd.is_args_conflicts_with_subcommands_set() && valid_arg_found) {
                let candidates = suggestions::did_you_mean(
                    &arg_os.display().to_string(),
                    self.cmd.all_subcommand_names(),
                );
                // If the argument looks like a subcommand.
                if !candidates.is_empty() {
                    return ClapError::invalid_subcommand(
                        self.cmd,
                        arg_os.display().to_string(),
                        candidates,
                        self.cmd
                            .get_bin_name()
                            .unwrap_or_else(|| self.cmd.get_name())
                            .to_owned(),
                        Usage::new(self.cmd).create_usage_with_title(&[]),
                    );
                }

                // If the argument must be a subcommand.
                if self.cmd.has_subcommands()
                    && (!self.cmd.has_positionals() || self.cmd.is_infer_subcommands_set())
                {
                    return ClapError::unrecognized_subcommand(
                        self.cmd,
                        arg_os.display().to_string(),
                        Usage::new(self.cmd).create_usage_with_title(&[]),
                    );
                }
            }

            let suggested_trailing_arg = !trailing_values
                && self.cmd.has_positionals()
                && (arg_os.is_long() || arg_os.is_short());
            ClapError::unknown_argument(
                self.cmd,
                arg_os.display().to_string(),
                None,
                suggested_trailing_arg,
                Usage::new(self.cmd).create_usage_with_title(&[]),
            )
        }

        // Checks if the arg matches a subcommand name, or any of its aliases (if defined)
        fn possible_subcommand(
            &self,
            arg: Result<&str, &RawOsStr>,
            valid_arg_found: bool,
        ) -> Option<&str> {
            debug!("Parser::possible_subcommand: arg={:?}", arg);
            let arg = some!(arg.ok());

            if !(self.cmd.is_args_conflicts_with_subcommands_set() && valid_arg_found) {
                if self.cmd.is_infer_subcommands_set() {
                    // For subcommand `test`, we accepts it's prefix: `t`, `te`,
                    // `tes` and `test`.
                    let v = self
                        .cmd
                        .all_subcommand_names()
                        .filter(|s| s.starts_with(arg))
                        .collect::<Vec<_>>();

                    if v.len() == 1 {
                        return Some(v[0]);
                    }

                    // If there is any ambiguity, fallback to non-infer subcommand
                    // search.
                }
                if let Some(sc) = self.cmd.find_subcommand(arg) {
                    return Some(sc.get_name());
                }
            }
            None
        }

        // Checks if the arg matches a long flag subcommand name, or any of its aliases (if defined)
        fn possible_long_flag_subcommand(&self, arg: &str) -> Option<&str> {
            debug!("Parser::possible_long_flag_subcommand: arg={:?}", arg);
            if self.cmd.is_infer_subcommands_set() {
                let options = self
                    .cmd
                    .get_subcommands()
                    .fold(Vec::new(), |mut options, sc| {
                        if let Some(long) = sc.get_long_flag() {
                            if long.starts_with(arg) {
                                options.push(long);
                            }
                            options.extend(sc.get_all_aliases().filter(|alias| alias.starts_with(arg)))
                        }
                        options
                    });
                if options.len() == 1 {
                    return Some(options[0]);
                }

                for sc in options {
                    if sc == arg {
                        return Some(sc);
                    }
                }
            } else if let Some(sc_name) = self.cmd.find_long_subcmd(arg) {
                return Some(sc_name);
            }
            None
        }

        fn parse_help_subcommand(
            &self,
            cmds: impl Iterator<Item = &'cmd OsStr>,
        ) -> ClapResult<std::convert::Infallible> {
            debug!("Parser::parse_help_subcommand");

            let mut cmd = self.cmd.clone();
            let sc = {
                let mut sc = &mut cmd;

                for cmd in cmds {
                    sc = if let Some(sc_name) =
                        sc.find_subcommand(cmd).map(|sc| sc.get_name().to_owned())
                    {
                        sc._build_subcommand(&sc_name).unwrap()
                    } else {
                        return Err(ClapError::unrecognized_subcommand(
                            sc,
                            cmd.to_string_lossy().into_owned(),
                            Usage::new(sc).create_usage_with_title(&[]),
                        ));
                    };
                }

                sc
            };
            let parser = Parser::new(sc);

            Err(parser.help_err(true))
        }

        fn is_new_arg(&self, next: &clap_lex::ParsedArg<'_>, current_positional: &Arg) -> bool {
            #![allow(clippy::needless_bool)] // Prefer consistent if/else-if ladder

            debug!(
                "Parser::is_new_arg: {:?}:{}",
                next.to_value_os(),
                current_positional.get_id()
            );

            if self.cmd[current_positional.get_id()].is_allow_hyphen_values_set()
                || (self.cmd[current_positional.get_id()].is_allow_negative_numbers_set()
                    && next.is_number())
            {
                // If allow hyphen, this isn't a new arg.
                debug!("Parser::is_new_arg: Allow hyphen");
                false
            } else if next.is_long() {
                // If this is a long flag, this is a new arg.
                debug!("Parser::is_new_arg: --<something> found");
                true
            } else if next.is_short() {
                // If this is a short flag, this is a new arg. But a singe '-' by
                // itself is a value and typically means "stdin" on unix systems.
                debug!("Parser::is_new_arg: -<something> found");
                true
            } else {
                // Nothing special, this is a value.
                debug!("Parser::is_new_arg: value");
                false
            }
        }

        fn parse_subcommand(
            &mut self,
            sc_name: &str,
            matcher: &mut ArgMatcher,
            raw_args: &mut clap_lex::RawArgs,
            args_cursor: clap_lex::ArgCursor,
            keep_state: bool,
        ) -> ClapResult<()> {
            debug!("Parser::parse_subcommand");

            let partial_parsing_enabled = self.cmd.is_ignore_errors_set();

            if let Some(sc) = self.cmd._build_subcommand(sc_name) {
                let mut sc_matcher = ArgMatcher::new(sc);

                debug!(
                    "Parser::parse_subcommand: About to parse sc={}",
                    sc.get_name()
                );

                {
                    let mut p = Parser::new(sc);
                    // HACK: maintain indexes between parsers
                    // FlagSubCommand short arg needs to revisit the current short args, but skip the subcommand itself
                    if keep_state {
                        p.cur_idx.set(self.cur_idx.get());
                        p.flag_subcmd_at = self.flag_subcmd_at;
                        p.flag_subcmd_skip = self.flag_subcmd_skip;
                    }
                    if let Err(error) = p.get_matches_with(&mut sc_matcher, raw_args, args_cursor) {
                        if partial_parsing_enabled {
                            debug!(
                                "Parser::parse_subcommand: ignored error in subcommand {}: {:?}",
                                sc_name, error
                            );
                        } else {
                            return Err(error);
                        }
                    }
                }
                matcher.subcommand(SubCommand {
                    name: sc.get_name().to_owned(),
                    matches: sc_matcher.into_inner(),
                });
            }
            Ok(())
        }

        fn parse_long_arg(
            &mut self,
            matcher: &mut ArgMatcher,
            long_arg: Result<&str, &RawOsStr>,
            long_value: Option<&RawOsStr>,
            parse_state: &ParseState,
            pos_counter: usize,
            valid_arg_found: &mut bool,
        ) -> ClapResult<ParseResult> {
            // maybe here lifetime should be 'a
            debug!("Parser::parse_long_arg");

            #[allow(clippy::blocks_in_if_conditions)]
            if matches!(parse_state, ParseState::Opt(opt) | ParseState::Pos(opt) if
                self.cmd[opt].is_allow_hyphen_values_set())
            {
                debug!("Parser::parse_long_arg: prior arg accepts hyphenated values",);
                return Ok(ParseResult::MaybeHyphenValue);
            }

            debug!("Parser::parse_long_arg: Does it contain '='...");
            let long_arg = match long_arg {
                Ok(long_arg) => long_arg,
                Err(long_arg) => {
                    return Ok(ParseResult::NoMatchingArg {
                        arg: long_arg.to_str_lossy().into_owned(),
                    });
                }
            };
            if long_arg.is_empty() {
                debug_assert!(
                    long_value.is_some(),
                    "`--` should be filtered out before this point"
                );
            }

            let arg = if let Some(arg) = self.cmd.get_keymap().get(long_arg) {
                debug!("Parser::parse_long_arg: Found valid arg or flag '{}'", arg);
                Some((long_arg, arg))
            } else if self.cmd.is_infer_long_args_set() {
                self.cmd.get_arguments().find_map(|a| {
                    if let Some(long) = a.get_long() {
                        if long.starts_with(long_arg) {
                            return Some((long, a));
                        }
                    }
                    a.aliases
                        .iter()
                        .find_map(|(alias, _)| alias.starts_with(long_arg).then(|| (alias.as_str(), a)))
                })
            } else {
                None
            };

            if let Some((_long_arg, arg)) = arg {
                let ident = Identifier::Long;
                *valid_arg_found = true;
                if arg.is_takes_value_set() {
                    debug!(
                        "Parser::parse_long_arg({:?}): Found an arg with value '{:?}'",
                        long_arg, &long_value
                    );
                    let has_eq = long_value.is_some();
                    self.parse_opt_value(ident, long_value, arg, matcher, has_eq)
                } else if let Some(rest) = long_value {
                    let required = self.cmd.required_graph();
                    debug!(
                        "Parser::parse_long_arg({:?}): Got invalid literal `{:?}`",
                        long_arg, rest
                    );
                    let mut used: Vec<Id> = matcher
                        .arg_ids()
                        .filter(|arg_id| {
                            matcher.check_explicit(arg_id, &crate::builder::ArgPredicate::IsPresent)
                        })
                        .filter(|&n| {
                            self.cmd.find(n).map_or(true, |a| {
                                !(a.is_hide_set() || required.contains(a.get_id()))
                            })
                        })
                        .cloned()
                        .collect();
                    used.push(arg.get_id().clone());

                    Ok(ParseResult::UnneededAttachedValue {
                        rest: rest.to_str_lossy().into_owned(),
                        used,
                        arg: arg.to_string(),
                    })
                } else {
                    debug!("Parser::parse_long_arg({:?}): Presence validated", long_arg);
                    let trailing_idx = None;
                    self.react(
                        Some(ident),
                        ValueSource::CommandLine,
                        arg,
                        vec![],
                        trailing_idx,
                        matcher,
                    )
                }
            } else if let Some(sc_name) = self.possible_long_flag_subcommand(long_arg) {
                Ok(ParseResult::FlagSubCommand(sc_name.to_string()))
            } else if self
                .cmd
                .get_keymap()
                .get(&pos_counter)
                .map_or(false, |arg| {
                    arg.is_allow_hyphen_values_set() && !arg.is_last_set()
                })
            {
                debug!(
                    "Parser::parse_long_args: positional at {} allows hyphens",
                    pos_counter
                );
                Ok(ParseResult::MaybeHyphenValue)
            } else {
                Ok(ParseResult::NoMatchingArg {
                    arg: long_arg.to_owned(),
                })
            }
        }

        fn parse_short_arg(
            &mut self,
            matcher: &mut ArgMatcher,
            mut short_arg: clap_lex::ShortFlags<'_>,
            parse_state: &ParseState,
            // change this to possible pos_arg when removing the usage of &mut Parser.
            pos_counter: usize,
            valid_arg_found: &mut bool,
        ) -> ClapResult<ParseResult> {
            debug!("Parser::parse_short_arg: short_arg={:?}", short_arg);

            #[allow(clippy::blocks_in_if_conditions)]
            if matches!(parse_state, ParseState::Opt(opt) | ParseState::Pos(opt)
                    if self.cmd[opt].is_allow_hyphen_values_set() || (self.cmd[opt].is_allow_negative_numbers_set() && short_arg.is_number()))
            {
                debug!("Parser::parse_short_args: prior arg accepts hyphenated values",);
                return Ok(ParseResult::MaybeHyphenValue);
            } else if self
                .cmd
                .get_keymap()
                .get(&pos_counter)
                .map_or(false, |arg| arg.is_allow_negative_numbers_set())
                && short_arg.is_number()
            {
                debug!("Parser::parse_short_arg: negative number");
                return Ok(ParseResult::MaybeHyphenValue);
            } else if self
                .cmd
                .get_keymap()
                .get(&pos_counter)
                .map_or(false, |arg| {
                    arg.is_allow_hyphen_values_set() && !arg.is_last_set()
                })
                && short_arg
                    .clone()
                    .any(|c| !c.map(|c| self.cmd.contains_short(c)).unwrap_or_default())
            {
                debug!(
                    "Parser::parse_short_args: positional at {} allows hyphens",
                    pos_counter
                );
                return Ok(ParseResult::MaybeHyphenValue);
            }

            let mut ret = ParseResult::NoArg;

            let skip = self.flag_subcmd_skip;
            self.flag_subcmd_skip = 0;
            let res = short_arg.advance_by(skip);
            debug_assert_eq!(
                res,
                Ok(()),
                "tracking of `flag_subcmd_skip` is off for `{:?}`",
                short_arg
            );
            while let Some(c) = short_arg.next_flag() {
                let c = match c {
                    Ok(c) => c,
                    Err(rest) => {
                        return Ok(ParseResult::NoMatchingArg {
                            arg: format!("-{}", rest.to_str_lossy()),
                        });
                    }
                };
                debug!("Parser::parse_short_arg:iter:{}", c);

                // Check for matching short options, and return the name if there is no trailing
                // concatenated value: -oval
                // Option: -o
                // Value: val
                if let Some(arg) = self.cmd.get_keymap().get(&c) {
                    let ident = Identifier::Short;
                    debug!(
                        "Parser::parse_short_arg:iter:{}: Found valid opt or flag",
                        c
                    );
                    *valid_arg_found = true;
                    if !arg.is_takes_value_set() {
                        let arg_values = Vec::new();
                        let trailing_idx = None;
                        ret = ok!(self.react(
                            Some(ident),
                            ValueSource::CommandLine,
                            arg,
                            arg_values,
                            trailing_idx,
                            matcher,
                        ));
                        continue;
                    }

                    // Check for trailing concatenated value
                    //
                    // Cloning the iterator, so we rollback if it isn't there.
                    let val = short_arg.clone().next_value_os().unwrap_or_default();
                    debug!(
                        "Parser::parse_short_arg:iter:{}: val={:?} (bytes), val={:?} (ascii), short_arg={:?}",
                        c, val, val.as_raw_bytes(), short_arg
                    );
                    let val = Some(val).filter(|v| !v.is_empty());

                    // Default to "we're expecting a value later".
                    //
                    // If attached value is not consumed, we may have more short
                    // flags to parse, continue.
                    //
                    // e.g. `-xvf`, when require_equals && x.min_vals == 0, we don't
                    // consume the `vf`, even if it's provided as value.
                    let (val, has_eq) = if let Some(val) = val.and_then(|v| v.strip_prefix('=')) {
                        (Some(val), true)
                    } else {
                        (val, false)
                    };
                    match ok!(self.parse_opt_value(ident, val, arg, matcher, has_eq)) {
                        ParseResult::AttachedValueNotConsumed => continue,
                        x => return Ok(x),
                    }
                }

                return if let Some(sc_name) = self.cmd.find_short_subcmd(c) {
                    debug!("Parser::parse_short_arg:iter:{}: subcommand={}", c, sc_name);
                    // Make sure indices get updated before reading `self.cur_idx`
                    ok!(self.resolve_pending(matcher));
                    self.cur_idx.set(self.cur_idx.get() + 1);
                    debug!("Parser::parse_short_arg: cur_idx:={}", self.cur_idx.get());

                    let name = sc_name.to_string();
                    // Get the index of the previously saved flag subcommand in the group of flags (if exists).
                    // If it is a new flag subcommand, then the formentioned index should be the current one
                    // (ie. `cur_idx`), and should be registered.
                    let cur_idx = self.cur_idx.get();
                    self.flag_subcmd_at.get_or_insert(cur_idx);
                    let done_short_args = short_arg.is_empty();
                    if done_short_args {
                        self.flag_subcmd_at = None;
                    }
                    Ok(ParseResult::FlagSubCommand(name))
                } else {
                    Ok(ParseResult::NoMatchingArg {
                        arg: format!("-{}", c),
                    })
                };
            }
            Ok(ret)
        }

        fn parse_opt_value(
            &self,
            ident: Identifier,
            attached_value: Option<&RawOsStr>,
            arg: &Arg,
            matcher: &mut ArgMatcher,
            has_eq: bool,
        ) -> ClapResult<ParseResult> {
            debug!(
                "Parser::parse_opt_value; arg={}, val={:?}, has_eq={:?}",
                arg.get_id(),
                attached_value,
                has_eq
            );
            debug!("Parser::parse_opt_value; arg.settings={:?}", arg.settings);

            debug!("Parser::parse_opt_value; Checking for val...");
            // require_equals is set, but no '=' is provided, try throwing error.
            if arg.is_require_equals_set() && !has_eq {
                if arg.get_min_vals() == 0 {
                    debug!("Requires equals, but min_vals == 0");
                    let arg_values = Vec::new();
                    let trailing_idx = None;
                    let react_result = ok!(self.react(
                        Some(ident),
                        ValueSource::CommandLine,
                        arg,
                        arg_values,
                        trailing_idx,
                        matcher,
                    ));
                    debug_assert_eq!(react_result, ParseResult::ValuesDone);
                    if attached_value.is_some() {
                        Ok(ParseResult::AttachedValueNotConsumed)
                    } else {
                        Ok(ParseResult::ValuesDone)
                    }
                } else {
                    debug!("Requires equals but not provided. Error.");
                    Ok(ParseResult::EqualsNotProvided {
                        arg: arg.to_string(),
                    })
                }
            } else if let Some(v) = attached_value {
                let arg_values = vec![v.to_os_str().into_owned()];
                let trailing_idx = None;
                let react_result = ok!(self.react(
                    Some(ident),
                    ValueSource::CommandLine,
                    arg,
                    arg_values,
                    trailing_idx,
                    matcher,
                ));
                debug_assert_eq!(react_result, ParseResult::ValuesDone);
                // Attached are always done
                Ok(ParseResult::ValuesDone)
            } else {
                debug!("Parser::parse_opt_value: More arg vals required...");
                ok!(self.resolve_pending(matcher));
                let trailing_values = false;
                matcher.pending_values_mut(arg.get_id(), Some(ident), trailing_values);
                Ok(ParseResult::Opt(arg.get_id().clone()))
            }
        }

        fn check_terminator(&self, arg: &Arg, val: &RawOsStr) -> Option<ParseResult> {
            if Some(val)
                == arg
                    .terminator
                    .as_ref()
                    .map(|s| RawOsStr::from_str(s.as_str()))
            {
                debug!("Parser::check_terminator: terminator={:?}", arg.terminator);
                Some(ParseResult::ValuesDone)
            } else {
                None
            }
        }

        fn push_arg_values(
            &self,
            arg: &Arg,
            raw_vals: Vec<OsString>,
            matcher: &mut ArgMatcher,
        ) -> ClapResult<()> {
            debug!("Parser::push_arg_values: {:?}", raw_vals);

            for raw_val in raw_vals {
                // update the current index because each value is a distinct index to clap
                self.cur_idx.set(self.cur_idx.get() + 1);
                debug!(
                    "Parser::add_single_val_to_arg: cur_idx:={}",
                    self.cur_idx.get()
                );
                let value_parser = arg.get_value_parser();
                let val = ok!(value_parser.parse_ref(self.cmd, Some(arg), &raw_val));

                matcher.add_val_to(arg.get_id(), val, raw_val);
                matcher.add_index_to(arg.get_id(), self.cur_idx.get());
            }

            Ok(())
        }

        fn resolve_pending(&self, matcher: &mut ArgMatcher) -> ClapResult<()> {
            let pending = match matcher.take_pending() {
                Some(pending) => pending,
                None => {
                    return Ok(());
                }
            };

            debug!("Parser::resolve_pending: id={:?}", pending.id);
            let arg = self.cmd.find(&pending.id).expect(INTERNAL_ERROR_MSG);
            let _ = ok!(self.react(
                pending.ident,
                ValueSource::CommandLine,
                arg,
                pending.raw_vals,
                pending.trailing_idx,
                matcher,
            ));

            Ok(())
        }

        fn react(
            &self,
            ident: Option<Identifier>,
            source: ValueSource,
            arg: &Arg,
            mut raw_vals: Vec<OsString>,
            mut trailing_idx: Option<usize>,
            matcher: &mut ArgMatcher,
        ) -> ClapResult<ParseResult> {
            ok!(self.resolve_pending(matcher));

            debug!(
                "Parser::react action={:?}, identifier={:?}, source={:?}",
                arg.get_action(),
                ident,
                source
            );

            // Process before `default_missing_values` to avoid it counting as values from the command
            // line
            if source == ValueSource::CommandLine {
                ok!(self.verify_num_args(arg, &raw_vals));
            }

            if raw_vals.is_empty() {
                // We assume this case is valid: require equals, but min_vals == 0.
                if !arg.default_missing_vals.is_empty() {
                    debug!("Parser::react: has default_missing_vals");
                    trailing_idx = None;
                    raw_vals.extend(
                        arg.default_missing_vals
                            .iter()
                            .map(|s| s.as_os_str().to_owned()),
                    );
                }
            }

            if let Some(val_delim) = arg.get_value_delimiter() {
                if self.cmd.is_dont_delimit_trailing_values_set() && trailing_idx == Some(0) {
                    // Nothing to do
                } else {
                    let mut split_raw_vals = Vec::with_capacity(raw_vals.len());
                    for (i, raw_val) in raw_vals.into_iter().enumerate() {
                        let raw_val = RawOsString::new(raw_val);
                        if !raw_val.contains(val_delim)
                            || (self.cmd.is_dont_delimit_trailing_values_set()
                                && trailing_idx == Some(i))
                        {
                            split_raw_vals.push(raw_val.into_os_string());
                        } else {
                            split_raw_vals
                                .extend(raw_val.split(val_delim).map(|x| x.to_os_str().into_owned()));
                        }
                    }
                    raw_vals = split_raw_vals
                }
            }

            match arg.get_action() {
                ArgAction::Set => {
                    if source == ValueSource::CommandLine
                        && matches!(ident, Some(Identifier::Short) | Some(Identifier::Long))
                    {
                        // Record flag's index
                        self.cur_idx.set(self.cur_idx.get() + 1);
                        debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
                    }
                    if matcher.remove(arg.get_id())
                        && !(self.cmd.is_args_override_self() || arg.overrides.contains(arg.get_id()))
                    {
                        return Err(ClapError::argument_conflict(
                            self.cmd,
                            arg.to_string(),
                            vec![arg.to_string()],
                            Usage::new(self.cmd).create_usage_with_title(&[]),
                        ));
                    }
                    self.start_custom_arg(matcher, arg, source);
                    ok!(self.push_arg_values(arg, raw_vals, matcher));
                    if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
                        debug!(
                            "Parser::react not enough values passed in, leaving it to the validator to complain",
                        );
                    }
                    Ok(ParseResult::ValuesDone)
                }
                ArgAction::Append => {
                    if source == ValueSource::CommandLine
                        && matches!(ident, Some(Identifier::Short) | Some(Identifier::Long))
                    {
                        // Record flag's index
                        self.cur_idx.set(self.cur_idx.get() + 1);
                        debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
                    }
                    self.start_custom_arg(matcher, arg, source);
                    ok!(self.push_arg_values(arg, raw_vals, matcher));
                    if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
                        debug!(
                            "Parser::react not enough values passed in, leaving it to the validator to complain",
                        );
                    }
                    Ok(ParseResult::ValuesDone)
                }
                ArgAction::SetTrue => {
                    let raw_vals = if raw_vals.is_empty() {
                        vec![OsString::from("true")]
                    } else {
                        raw_vals
                    };

                    if matcher.remove(arg.get_id())
                        && !(self.cmd.is_args_override_self() || arg.overrides.contains(arg.get_id()))
                    {
                        return Err(ClapError::argument_conflict(
                            self.cmd,
                            arg.to_string(),
                            vec![arg.to_string()],
                            Usage::new(self.cmd).create_usage_with_title(&[]),
                        ));
                    }
                    self.start_custom_arg(matcher, arg, source);
                    ok!(self.push_arg_values(arg, raw_vals, matcher));
                    Ok(ParseResult::ValuesDone)
                }
                ArgAction::SetFalse => {
                    let raw_vals = if raw_vals.is_empty() {
                        vec![OsString::from("false")]
                    } else {
                        raw_vals
                    };

                    if matcher.remove(arg.get_id())
                        && !(self.cmd.is_args_override_self() || arg.overrides.contains(arg.get_id()))
                    {
                        return Err(ClapError::argument_conflict(
                            self.cmd,
                            arg.to_string(),
                            vec![arg.to_string()],
                            Usage::new(self.cmd).create_usage_with_title(&[]),
                        ));
                    }
                    self.start_custom_arg(matcher, arg, source);
                    ok!(self.push_arg_values(arg, raw_vals, matcher));
                    Ok(ParseResult::ValuesDone)
                }
                ArgAction::Count => {
                    let raw_vals = if raw_vals.is_empty() {
                        let existing_value = *matcher
                            .get_one::<crate::builder::CountType>(arg.get_id().as_str())
                            .unwrap_or(&0);
                        let next_value = existing_value.saturating_add(1);
                        vec![OsString::from(next_value.to_string())]
                    } else {
                        raw_vals
                    };

                    matcher.remove(arg.get_id());
                    self.start_custom_arg(matcher, arg, source);
                    ok!(self.push_arg_values(arg, raw_vals, matcher));
                    Ok(ParseResult::ValuesDone)
                }
                ArgAction::Help => {
                    let use_long = match ident {
                        Some(Identifier::Long) => true,
                        Some(Identifier::Short) => false,
                        Some(Identifier::Index) => true,
                        None => true,
                    };
                    debug!("Help: use_long={}", use_long);
                    Err(self.help_err(use_long))
                }
                ArgAction::Version => {
                    let use_long = match ident {
                        Some(Identifier::Long) => true,
                        Some(Identifier::Short) => false,
                        Some(Identifier::Index) => true,
                        None => true,
                    };
                    debug!("Version: use_long={}", use_long);
                    Err(self.version_err(use_long))
                }
            }
        }

        fn verify_num_args(&self, arg: &Arg, raw_vals: &[OsString]) -> ClapResult<()> {
            if self.cmd.is_ignore_errors_set() {
                return Ok(());
            }

            let actual = raw_vals.len();
            let expected = arg.get_num_args().expect(INTERNAL_ERROR_MSG);

            if 0 < expected.min_values() && actual == 0 {
                // Issue 665 (https://github.com/clap-rs/clap/issues/665)
                // Issue 1105 (https://github.com/clap-rs/clap/issues/1105)
                return Err(ClapError::empty_value(
                    self.cmd,
                    &super::get_possible_values_cli(arg)
                        .iter()
                        .filter(|pv| !pv.is_hide_set())
                        .map(|n| n.get_name().to_owned())
                        .collect::<Vec<_>>(),
                    arg.to_string(),
                ));
            } else if let Some(expected) = expected.num_values() {
                if expected != actual {
                    debug!("Validator::validate_arg_num_vals: Sending error WrongNumberOfValues");
                    return Err(ClapError::wrong_number_of_values(
                        self.cmd,
                        arg.to_string(),
                        expected,
                        actual,
                        Usage::new(self.cmd).create_usage_with_title(&[]),
                    ));
                }
            } else if actual < expected.min_values() {
                return Err(ClapError::too_few_values(
                    self.cmd,
                    arg.to_string(),
                    expected.min_values(),
                    actual,
                    Usage::new(self.cmd).create_usage_with_title(&[]),
                ));
            } else if expected.max_values() < actual {
                debug!("Validator::validate_arg_num_vals: Sending error TooManyValues");
                return Err(ClapError::too_many_values(
                    self.cmd,
                    raw_vals
                        .last()
                        .expect(INTERNAL_ERROR_MSG)
                        .to_string_lossy()
                        .into_owned(),
                    arg.to_string(),
                    Usage::new(self.cmd).create_usage_with_title(&[]),
                ));
            }

            Ok(())
        }

        fn remove_overrides(&self, arg: &Arg, matcher: &mut ArgMatcher) {
            debug!("Parser::remove_overrides: id={:?}", arg.id);
            for override_id in &arg.overrides {
                debug!("Parser::remove_overrides:iter:{:?}: removing", override_id);
                matcher.remove(override_id);
            }

            // Override anything that can override us
            let mut transitive = Vec::new();
            for arg_id in matcher.arg_ids() {
                if let Some(overrider) = self.cmd.find(arg_id) {
                    if overrider.overrides.contains(arg.get_id()) {
                        transitive.push(overrider.get_id());
                    }
                }
            }
            for overrider_id in transitive {
                debug!("Parser::remove_overrides:iter:{:?}: removing", overrider_id);
                matcher.remove(overrider_id);
            }
        }

        #[cfg(feature = "env")]
        fn add_env(&mut self, matcher: &mut ArgMatcher) -> ClapResult<()> {
            debug!("Parser::add_env");

            for arg in self.cmd.get_arguments() {
                // Use env only if the arg was absent among command line args,
                // early return if this is not the case.
                if matcher.contains(&arg.id) {
                    debug!("Parser::add_env: Skipping existing arg `{}`", arg);
                    continue;
                }

                debug!("Parser::add_env: Checking arg `{}`", arg);
                if let Some((_, Some(ref val))) = arg.env {
                    debug!("Parser::add_env: Found an opt with value={:?}", val);
                    let arg_values = vec![val.to_owned()];
                    let trailing_idx = None;
                    let _ = ok!(self.react(
                        None,
                        ValueSource::EnvVariable,
                        arg,
                        arg_values,
                        trailing_idx,
                        matcher,
                    ));
                }
            }

            Ok(())
        }

        fn add_defaults(&self, matcher: &mut ArgMatcher) -> ClapResult<()> {
            debug!("Parser::add_defaults");

            for arg in self.cmd.get_arguments() {
                debug!("Parser::add_defaults:iter:{}:", arg.get_id());
                ok!(self.add_default_value(arg, matcher));
            }

            Ok(())
        }

        fn add_default_value(&self, arg: &Arg, matcher: &mut ArgMatcher) -> ClapResult<()> {
            if !arg.default_vals_ifs.is_empty() {
                debug!("Parser::add_default_value: has conditional defaults");
                if !matcher.contains(arg.get_id()) {
                    for (id, val, default) in arg.default_vals_ifs.iter() {
                        let add = if let Some(a) = matcher.get(id) {
                            match val {
                                crate::builder::ArgPredicate::Equals(v) => {
                                    a.raw_vals_flatten().any(|value| v == value)
                                }
                                crate::builder::ArgPredicate::IsPresent => true,
                            }
                        } else {
                            false
                        };

                        if add {
                            if let Some(default) = default {
                                let arg_values = vec![default.to_os_string()];
                                let trailing_idx = None;
                                let _ = ok!(self.react(
                                    None,
                                    ValueSource::DefaultValue,
                                    arg,
                                    arg_values,
                                    trailing_idx,
                                    matcher,
                                ));
                            }
                            return Ok(());
                        }
                    }
                }
            } else {
                debug!("Parser::add_default_value: doesn't have conditional defaults");
            }

            if !arg.default_vals.is_empty() {
                debug!(
                    "Parser::add_default_value:iter:{}: has default vals",
                    arg.get_id()
                );
                if matcher.contains(arg.get_id()) {
                    debug!("Parser::add_default_value:iter:{}: was used", arg.get_id());
                // do nothing
                } else {
                    debug!(
                        "Parser::add_default_value:iter:{}: wasn't used",
                        arg.get_id()
                    );
                    let arg_values: Vec<_> = arg
                        .default_vals
                        .iter()
                        .map(crate::builder::OsStr::to_os_string)
                        .collect();
                    let trailing_idx = None;
                    let _ = ok!(self.react(
                        None,
                        ValueSource::DefaultValue,
                        arg,
                        arg_values,
                        trailing_idx,
                        matcher,
                    ));
                }
            } else {
                debug!(
                    "Parser::add_default_value:iter:{}: doesn't have default vals",
                    arg.get_id()
                );

                // do nothing
            }

            Ok(())
        }

        fn start_custom_arg(&self, matcher: &mut ArgMatcher, arg: &Arg, source: ValueSource) {
            if source == ValueSource::CommandLine {
                // With each new occurrence, remove overrides from prior occurrences
                self.remove_overrides(arg, matcher);
            }
            matcher.start_custom_arg(arg, source);
            if source.is_explicit() {
                for group in self.cmd.groups_for_arg(arg.get_id()) {
                    matcher.start_custom_group(group.clone(), source);
                    matcher.add_val_to(
                        &group,
                        AnyValue::new(arg.get_id().clone()),
                        OsString::from(arg.get_id().as_str()),
                    );
                }
            }
        }
    }

    // Error, Help, and Version Methods
    impl<'cmd> Parser<'cmd> {
        /// Is only used for the long flag(which is the only one needs fuzzy searching)
        fn did_you_mean_error(
            &mut self,
            arg: &str,
            matcher: &mut ArgMatcher,
            remaining_args: &[&OsStr],
            trailing_values: bool,
        ) -> ClapError {
            debug!("Parser::did_you_mean_error: arg={}", arg);
            // Didn't match a flag or option
            let longs = self
                .cmd
                .get_keymap()
                .keys()
                .filter_map(|x| match x {
                    KeyType::Long(l) => Some(l.to_string_lossy().into_owned()),
                    _ => None,
                })
                .collect::<Vec<_>>();
            debug!("Parser::did_you_mean_error: longs={:?}", longs);

            let did_you_mean = suggestions::did_you_mean_flag(
                arg,
                remaining_args,
                longs.iter().map(|x| &x[..]),
                self.cmd.get_subcommands_mut(),
            );

            // Add the arg to the matches to build a proper usage string
            if let Some((name, _)) = did_you_mean.as_ref() {
                if let Some(arg) = self.cmd.get_keymap().get(&name.as_ref()) {
                    self.start_custom_arg(matcher, arg, ValueSource::CommandLine);
                }
            }

            let required = self.cmd.required_graph();
            let used: Vec<Id> = matcher
                .arg_ids()
                .filter(|arg_id| {
                    matcher.check_explicit(arg_id, &crate::builder::ArgPredicate::IsPresent)
                })
                .filter(|n| self.cmd.find(n).map_or(true, |a| !a.is_hide_set()))
                .cloned()
                .collect();

            // `did_you_mean` is a lot more likely and should cause us to skip the `--` suggestion
            //
            // In theory, this is only called for `--long`s, so we don't need to check
            let suggested_trailing_arg =
                did_you_mean.is_none() && !trailing_values && self.cmd.has_positionals();
            ClapError::unknown_argument(
                self.cmd,
                format!("--{}", arg),
                did_you_mean,
                suggested_trailing_arg,
                Usage::new(self.cmd)
                    .required(&required)
                    .create_usage_with_title(&used),
            )
        }

        fn help_err(&self, use_long: bool) -> ClapError {
            let styled = self.cmd.write_help_err(use_long);
            ClapError::display_help(self.cmd, styled)
        }

        fn version_err(&self, use_long: bool) -> ClapError {
            let styled = self.cmd.write_version_err(use_long);
            ClapError::display_version(self.cmd, styled)
        }
    }

    #[derive(Debug, PartialEq, Eq)]
    pub(crate) enum ParseState {
        ValuesDone,
        Opt(Id),
        Pos(Id),
    }

    /// Recoverable Parsing results.
    #[derive(Debug, PartialEq, Clone)]
    #[must_use]
    enum ParseResult {
        FlagSubCommand(String),
        Opt(Id),
        ValuesDone,
        /// Value attached to the short flag is not consumed(e.g. 'u' for `-cu` is
        /// not consumed).
        AttachedValueNotConsumed,
        /// This long flag doesn't need a value but is provided one.
        UnneededAttachedValue {
            rest: String,
            used: Vec<Id>,
            arg: String,
        },
        /// This flag might be an hyphen Value.
        MaybeHyphenValue,
        /// Equals required but not provided.
        EqualsNotProvided {
            arg: String,
        },
        /// Failed to match a Arg.
        NoMatchingArg {
            arg: String,
        },
        /// No argument found e.g. parser is given `-` when parsing a flag.
        NoArg,
    }

    #[derive(Clone, Debug, PartialEq, Eq)]
    pub(crate) struct PendingArg {
        pub(crate) id: Id,
        pub(crate) ident: Option<Identifier>,
        pub(crate) raw_vals: Vec<OsString>,
        pub(crate) trailing_idx: Option<usize>,
    }

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub(crate) enum Identifier {
        Short,
        Long,
        Index,
    }