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
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
use std::collections::{BTreeMap, BTreeSet, HashSet, HashMap, VecDeque};
use std::env;
use std::io::{self, BufRead, Write};
use std::path::Path;

use args::{ArgMatches, Arg, SubCommand, MatchedArg};
use args::{FlagBuilder, OptBuilder, PosBuilder};
use args::ArgGroup;
use fmt::Format;

#[cfg(feature = "suggestions")]
use strsim;

const INTERNAL_ERROR_MSG: &'static str = "Internal Error: Failed to write string. Please \
                                          consider filing a bug report!";

/// Produces a string from a given list of possible values which is similar to
/// the passed in value `v` with a certain confidence.
/// Thus in a list of possible values like ["foo", "bar"], the value "fop" will yield
/// `Some("foo")`, whereas "blark" would yield `None`.
#[cfg(feature = "suggestions")]
fn did_you_mean<'a, T, I>(v: &str, possible_values: I) -> Option<&'a str>
                    where T: AsRef<str> + 'a,
                          I: IntoIterator<Item=&'a T> {

    let mut candidate: Option<(f64, &str)> = None;
    for pv in possible_values.into_iter() {
        let confidence = strsim::jaro_winkler(v, pv.as_ref());
        if confidence > 0.8 && (candidate.is_none() ||
                               (candidate.as_ref().unwrap().0 < confidence)) {
            candidate = Some((confidence, pv.as_ref()));
        }
    }
    match candidate {
        None => None,
        Some((_, candidate)) => Some(candidate),
    }
}

#[cfg(not(feature = "suggestions"))]
fn did_you_mean<'a, T, I>(_: &str, _: I) -> Option<&'a str>
                    where T: AsRef<str> + 'a,
                          I: IntoIterator<Item=&'a T> {
    None
}

/// A helper to determine message formatting
enum DidYouMeanMessageStyle {
    /// Suggested value is a long flag
    LongFlag,
    /// Suggested value is one of various possible values
    EnumValue,
}

/// Used to create a representation of a command line program and all possible command line
/// arguments.
///
/// Application settings are set using the "builder pattern" with `.get_matches()` being the
/// terminal method that starts the runtime-parsing process and returns information about
/// the user supplied arguments (or lack there of).
///
/// There aren't any mandatory "options" that one must set. The "options" may also appear in any
/// order (so long as `.get_matches()` is the last method called).
///
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
/// let matches = App::new("myprog")
///                   .author("Me, me@mail.com")
///                   .version("1.0.2")
///                   .about("Explains in brief what the program does")
///                   .arg(
///                            Arg::with_name("in_file").index(1)
///                    )
///                   .after_help("Longer explaination to appear after the options when \
///                                displaying the help information from --help or -h")
///                   .get_matches();
///
/// // Your program logic starts here...
/// ```
pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
    // The name displayed to the user when showing version and help/usage information
    name: String,
    name_slice: &'ar str,
    // A string of author(s) if desired. Displayed when showing help/usage information
    author: Option<&'a str>,
    // The version displayed to the user
    version: Option<&'v str>,
    // A brief explanation of the program that gets displayed to the user when shown help/usage
    // information
    about: Option<&'ab str>,
    // Additional help information
    more_help: Option<&'h str>,
    // A list of possible flags
    flags: BTreeMap<&'ar str, FlagBuilder<'ar>>,
    // A list of possible options
    opts: BTreeMap<&'ar str, OptBuilder<'ar>>,
    // A list of positional arguments
    positionals_idx: BTreeMap<u8, PosBuilder<'ar>>,
    positionals_name: HashMap<&'ar str, u8>,
    // A list of subcommands
    subcommands: BTreeMap<String, App<'a, 'v, 'ab, 'u, 'h, 'ar>>,
    help_short: Option<char>,
    version_short: Option<char>,
    required: HashSet<&'ar str>,
    short_list: HashSet<char>,
    long_list: HashSet<&'ar str>,
    blacklist: HashSet<&'ar str>,
    usage_str: Option<&'u str>,
    bin_name: Option<String>,
    usage: Option<String>,
    groups: HashMap<&'ar str, ArgGroup<'ar, 'ar>>,
    global_args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>,
    help_str: Option<&'u str>,
    no_sc_error: bool,
    wait_on_error: bool,
    help_on_no_args: bool,
    needs_long_help: bool,
    needs_long_version: bool,
    needs_subcmd_help: bool,
    subcmds_neg_reqs: bool,
    help_on_no_sc: bool,
    global_ver: bool,
    // None = not set, Some(true) set for all children, Some(false) = disable version
    versionless_scs: Option<bool>,
    unified_help: bool
}

impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
    /// Creates a new instance of an application requiring a name (such as the binary). The name
    /// will be displayed to the user when they request to print version or help and usage
    /// information. The name should not contain spaces (hyphens '-' are ok).
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// let prog = App::new("myprog")
    /// # .get_matches();
    /// ```
    pub fn new(n: &'ar str) -> Self {
        App {
            name: n.to_owned(),
            name_slice: n,
            author: None,
            about: None,
            more_help: None,
            version: None,
            flags: BTreeMap::new(),
            opts: BTreeMap::new(),
            positionals_idx: BTreeMap::new(),
            positionals_name: HashMap::new(),
            subcommands: BTreeMap::new(),
            needs_long_version: true,
            needs_long_help: true,
            needs_subcmd_help: true,
            help_short: None,
            version_short: None,
            required: HashSet::new(),
            short_list: HashSet::new(),
            long_list: HashSet::new(),
            usage_str: None,
            usage: None,
            blacklist: HashSet::new(),
            bin_name: None,
            groups: HashMap::new(),
            subcmds_neg_reqs: false,
            global_args: vec![],
            no_sc_error: false,
            help_str: None,
            wait_on_error: false,
            help_on_no_args: false,
            help_on_no_sc: false,
            global_ver: false,
            versionless_scs: None,
            unified_help: false
        }
    }

    /// Sets a string of author(s) and will be displayed to the user when they request the help
    /// information with `--help` or `-h`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///      .author("Me, me@mymain.com")
    /// # ;
    /// ```
    pub fn author(mut self, a: &'a str) -> Self {
        self.author = Some(a);
        self
    }

    /// Overrides the system-determined binary name. This should only be used when absolutely
    /// neccessary, such as the binary name for your application is misleading, or perhaps *not*
    /// how the user should invoke your program.
    ///
    /// **NOTE:** This command **should not** be used for SubCommands.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///      .bin_name("my_binary")
    /// # ;
    /// ```
    pub fn bin_name(mut self, a: &str) -> Self {
        self.bin_name = Some(a.to_owned());
        self
    }

    /// Sets a string briefly describing what the program does and will be displayed when
    /// displaying help information.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .about("Does really amazing things to great people")
    /// # ;
    /// ```
    pub fn about(mut self, a: &'ab str) -> Self {
        self.about = Some(a);
        self
    }

    /// Adds additional help information to be displayed in addition to and directly after
    /// auto-generated help. This information is displayed **after** the auto-generated help
    /// information. This additional help is often used to describe how to use the arguments,
    /// or caveats to be noted.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::App;
    /// App::new("myprog")
    ///     .after_help("Does really amazing things to great people")
    /// # ;
    /// ```
    pub fn after_help(mut self, h: &'h str) -> Self {
        self.more_help = Some(h);
        self
    }

    /// Allows subcommands to override all requirements of the parent (this command). For example
    /// if you had a subcommand or even top level application which had a required arguments that
    /// are only required as long as there is no subcommand present.
    ///
    /// **NOTE:** This defaults to false (using subcommand does *not* negate requirements)
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::App;
    /// App::new("myprog")
    ///     .subcommands_negate_reqs(true)
    /// # ;
    /// ```
    pub fn subcommands_negate_reqs(mut self, n: bool) -> Self {
        self.subcmds_neg_reqs = n;
        self
    }

    /// Allows specifying that if no subcommand is present at runtime, error and exit gracefully
    ///
    /// **NOTE:** This defaults to false (subcommands do *not* need to be present)
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::App;
    /// App::new("myprog")
    ///     .subcommand_required(true)
    /// # ;
    /// ```
    pub fn subcommand_required(mut self, n: bool) -> Self {
        self.no_sc_error = n;
        self
    }

    /// Sets a string of the version number to be displayed when displaying version or help
    /// information.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .version("v0.1.24")
    /// # ;
    /// ```
    pub fn version(mut self, v: &'v str) -> Self {
        self.version = Some(v);
        self
    }

    /// Sets a custom usage string to override the auto-generated usage string.
    ///
    /// This will be displayed to the user when errors are found in argument parsing, or when you
    /// call `ArgMatches::usage()`
    ///
    /// **NOTE:** You do not need to specify the "USAGE: \n\t" portion, as that will
    /// still be applied by `clap`, you only need to specify the portion starting
    /// with the binary name.
    ///
    /// **NOTE:** This will not replace the entire help message, *only* the portion
    /// showing the usage.
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .usage("myapp [-clDas] <some_file>")
    /// # ;
    /// ```
    pub fn usage(mut self, u: &'u str) -> Self {
        self.usage_str = Some(u);
        self
    }

    /// Sets a custom help message and overrides the auto-generated one. This should only be used
    /// when the auto-generated message does not suffice.
    ///
    /// This will be displayed to the user when they use the default `--help` or `-h`
    ///
    /// **NOTE:** This replaces the **entire** help message, so nothing will be auto-generated.
    ///
    /// **NOTE:** This **only** replaces the help message for the current command, meaning if you
    /// are using subcommands, those help messages will still be auto-generated unless you
    /// specify a `.help()` for them as well.
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myapp")
    ///     .help("myapp v1.0\n\
    ///            Does awesome things\n\
    ///            (C) me@mail.com\n\n\
    ///
    ///            USAGE: myapp <opts> <comamnd>\n\n\
    ///
    ///            Options:\n\
    ///            -h, --helpe      Dispay this message\n\
    ///            -V, --version    Display version info\n\
    ///            -s <stuff>       Do something with stuff\n\
    ///            -v               Be verbose\n\n\
    ///
    ///            Commmands:\n\
    ///            help             Prints this message\n\
    ///            work             Do some work")
    /// # ;
    /// ```
    pub fn help(mut self, h: &'u str) -> Self {
        self.help_str = Some(h);
        self
    }

    /// Sets the short version of the `help` argument without the preceding `-`.
    ///
    /// By default `clap` automatically assigns `h`, but this can be overridden
    ///
    /// **NOTE:** Any leading `-` characters will be stripped, and only the first
    /// non `-` chacter will be used as the `short` version
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     // Using an uppercase `H` instead of the default lowercase `h`
    ///     .help_short("H")
    /// # ;
    pub fn help_short(mut self, s: &str) -> Self {
        self.help_short = s.trim_left_matches(|c| c == '-')
                           .chars()
                           .nth(0);
        self
    }

    /// Sets the short version of the `version` argument without the preceding `-`.
    ///
    /// By default `clap` automatically assigns `V`, but this can be overridden
    ///
    /// **NOTE:** Any leading `-` characters will be stripped, and only the first
    /// non `-` chacter will be used as the `short` version
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     // Using a lowercase `v` instead of the default capital `V`
    ///     .version_short("v")
    /// # ;
    pub fn version_short(mut self, s: &str) -> Self {
        self.version_short = s.trim_left_matches(|c| c == '-')
                           .chars()
                           .nth(0);
        self
    }

    /// Specifies that the help text sould be displayed (and then exit gracefully), if no
    /// arguments are present at runtime (i.e. an empty run such as, `$ myprog`.
    ///
    /// **NOTE:** Subcommands count as arguments
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .arg_required_else_help(true)
    /// # ;
    /// ```
    pub fn arg_required_else_help(mut self, tf: bool) -> Self {
        self.help_on_no_args = tf;
        self
    }

    /// Uses version of the current command for all subcommands. (Defaults to false; subcommands
    /// have independant version strings)
    ///
    /// **NOTE:** The version for the current command and this setting must be set **prior** to
    /// adding any subcommands
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg, SubCommand};
    /// App::new("myprog")
    ///     .version("v1.1")
    ///     .global_version(true)
    ///     .subcommand(SubCommand::with_name("test"))
    ///     .get_matches();
    /// // running `myprog test --version` will display
    /// // "myprog-test v1.1"
    /// ```
    pub fn global_version(mut self, gv: bool) -> Self {
        self.global_ver = gv;
        self
    }

    /// Disables `-V` and `--version` for all subcommands (Defaults to false; subcommands have
    /// version flags)
    ///
    /// **NOTE:** This setting must be set **prior** adding any subcommands
    ///
    /// **NOTE:** Do not set this value to false, it will have undesired results!
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg, SubCommand};
    /// App::new("myprog")
    ///     .version("v1.1")
    ///     .versionless_subcommands(true)
    ///     .subcommand(SubCommand::with_name("test"))
    ///     .get_matches();
    /// // running `myprog test --version` will display unknown argument error
    /// ```
    pub fn versionless_subcommands(mut self, vers: bool) -> Self {
        self.versionless_scs = Some(vers);
        self
    }

    /// By default the auto-generated help message groups flags, options, and positional arguments
    /// separately. This setting disable that and groups flags and options together presenting a
    /// more unified help message (a la getopts or docopt style).
    ///
    /// **NOTE:** This setting is cosmetic only and does not affect any functionality.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg, SubCommand};
    /// App::new("myprog")
    ///     .unified_help_message(true)
    ///     .get_matches();
    /// // running `myprog --help` will display a unified "docopt" or "getopts" style help message
    /// ```
    pub fn unified_help_message(mut self, uni_help: bool) -> Self {
        self.unified_help = uni_help;
        self
    }

    /// Will display a message "Press [ENTER]/[RETURN] to continue..." and wait user before
    /// exiting
    ///
    /// This is most useful when writing an application which is run from a GUI shortcut, or on
    /// Windows where a user tries to open the binary by double-clicking instead of using the
    /// command line (i.e. set `.arg_required_else_help(true)` and `.wait_on_error(true)` to
    /// display the help in such a case).
    ///
    /// **NOTE:** This setting is **not** recursive with subcommands, meaning if you wish this
    /// behavior for all subcommands, you must set this on each command (needing this is extremely
    /// rare)
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .arg_required_else_help(true)
    /// # ;
    /// ```
    pub fn wait_on_error(mut self, w: bool) -> Self {
        self.wait_on_error = w;
        self
    }

    /// Specifies that the help text sould be displayed (and then exit gracefully), if no
    /// subcommands are present at runtime (i.e. an empty run such as, `$ myprog`.
    ///
    /// **NOTE:** This should *not* be used with `.subcommand_required()` as they do the same
    /// thing, except one prints the help text, and one prints an error.
    ///
    /// **NOTE:** If the user specifies arguments at runtime, but no subcommand the help text will
    /// still be displayed and exit. If this is *not* the desired result, consider using
    /// `.arg_required_else_help()`
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .subcommand_required_else_help(true)
    /// # ;
    /// ```
    pub fn subcommand_required_else_help(mut self, tf: bool) -> Self {
        self.help_on_no_sc = tf;
        self
    }

    /// Adds an argument to the list of valid possibilties manually. This method allows you full
    /// control over the arguments settings and options (as well as dynamic generation). It also
    /// allows you specify several more advanced configuration options such as relational rules
    /// (exclusions and requirements).
    ///
    /// The only disadvantage to this method is that it's more verbose, and arguments must be added
    /// one at a time. Using `Arg::from_usage` helps with the verbosity, and still allows full
    /// control over the advanced configuration options.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     // Adding a single "flag" argument with a short and help text, using Arg::with_name()
    ///     .arg(
    ///         Arg::with_name("debug")
    ///            .short("d")
    ///            .help("turns on debugging mode")
    ///     )
    ///     // Adding a single "option" argument with a short, a long, and help text using the less
    ///     // verbose Arg::from_usage()
    ///     .arg(
    ///         Arg::from_usage("-c --config=[CONFIG] 'Optionally sets a config file to use'")
    ///     )
    /// # ;
    /// ```
    pub fn arg(mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) -> Self {
        self.add_arg(a);
        self
    }

    fn add_arg(&mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) {
        if self.flags.contains_key(a.name) ||
           self.opts.contains_key(a.name) ||
           self.positionals_name.contains_key(a.name) {
            panic!("Argument name must be unique\n\n\t\"{}\" is already in use", a.name);
        }
        if let Some(grp) = a.group {
            let ag = self.groups.entry(grp).or_insert(ArgGroup::with_name(grp));
            ag.args.insert(a.name);
            // Leaving this commented out for now...I'm not sure if having a required argument in
            // a in required group is bad...It has it's uses
            // assert!(!a.required,
            //     format!("Arguments may not be required AND part of a required group\n\n\t{} is \
            //         required and also part of the {} group\n\n\tEither remove the requirement \
            //         from the group, or the argument.", a.name, grp));
        }
        if let Some(s) = a.short {
            // if s == 'V' {
            //     self.version_short = None;
            // } else if s == 'h' {
            //     self.help_short = None;
            // }
            if self.short_list.contains(&s) {
                panic!("Argument short must be unique\n\n\t-{} is already in use", s);
            } else {
                self.short_list.insert(s);
            }
        }
        if let Some(l) = a.long {
            if self.long_list.contains(l) {
                panic!("Argument long must be unique\n\n\t--{} is already in use", l);
            } else {
                self.long_list.insert(l);
            }
            if l == "help" {
                self.needs_long_help = false;
            } else if l == "version" {
                self.needs_long_version = false;
            }
        }
        if a.required {
            self.required.insert(a.name);
        }
        if a.index.is_some() || (a.short.is_none() && a.long.is_none()) {
            let i = if a.index.is_none() {
                (self.positionals_idx.len() + 1) as u8
            } else {
                a.index.unwrap()
            };

            if a.short.is_some() || a.long.is_some() {
                panic!("Argument \"{}\" has conflicting requirements, both index() and short(), \
                    or long(), were supplied", a.name);
            }

            if self.positionals_idx.contains_key(&i) {
                panic!("Argument \"{}\" has the same index as another positional \
                    argument\n\n\tPerhaps try .multiple(true) to allow one positional argument \
                    to take multiple values", a.name);
            }
            if a.takes_value {
                panic!("Argument \"{}\" has conflicting requirements, both index() and \
                    takes_value(true) were supplied\n\n\tArguments with an index automatically \
                    take a value, you do not need to specify it manually", a.name);
            }
            if a.val_names.is_some() {
                panic!("Positional arguments (\"{}\") do not support named values, instead \
                    consider multiple positional arguments", a.name);
            }

            self.positionals_name.insert(a.name, i);
            // Create the Positional Arguemnt Builder with each HashSet = None to only allocate
            // those that require it
            let mut pb = PosBuilder {
                name: a.name,
                index: i,
                required: a.required,
                multiple: a.multiple,
                blacklist: None,
                requires: None,
                possible_vals: None,
                num_vals: a.num_vals,
                min_vals: a.min_vals,
                max_vals: a.max_vals,
                help: a.help,
                global: a.global,
                empty_vals: a.empty_vals
            };
            if pb.min_vals.is_some() && !pb.multiple {
                panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
                    values", pb.name, pb.num_vals.unwrap());
            }
            if pb.max_vals.is_some() && !pb.multiple {
                panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
                    values", pb.name, pb.num_vals.unwrap());
            }
            // Check if there is anything in the blacklist (mutually excludes list) and add any
            // values
            if let Some(ref bl) = a.blacklist {
                let mut bhs = HashSet::new();
                // without derefing n = &&str
                for n in bl { bhs.insert(*n); }
                pb.blacklist = Some(bhs);
            }
            // Check if there is anything in the requires list and add any values
            if let Some(ref r) = a.requires {
                let mut rhs = HashSet::new();
                // without derefing n = &&str
                for n in r {
                    rhs.insert(*n);
                    if pb.required {
                        self.required.insert(*n);
                    }
                }
                pb.requires = Some(rhs);
            }
            // Check if there is anything in the possible values and add those as well
            if let Some(ref p) = a.possible_vals {
                let mut phs = BTreeSet::new();
                // without derefing n = &&str
                for n in p { phs.insert(*n); }
                pb.possible_vals = Some(phs);
            }
            self.positionals_idx.insert(i, pb);
        } else if a.takes_value {
            if a.short.is_none() && a.long.is_none() {
                panic!("Argument \"{}\" has take_value(true), yet neither a short() or long() \
                    were supplied", a.name);
            }
            // No need to check for .index() as that is handled above
            let mut ob = OptBuilder {
                name: a.name,
                short: a.short,
                long: a.long,
                multiple: a.multiple,
                blacklist: None,
                help: a.help,
                global: a.global,
                possible_vals: None,
                num_vals: a.num_vals,
                min_vals: a.min_vals,
                max_vals: a.max_vals,
                val_names: a.val_names.clone(),
                requires: None,
                required: a.required,
                empty_vals: a.empty_vals
            };
            if let Some(ref vec) = ob.val_names {
                ob.num_vals = Some(vec.len() as u8);
            }
            if ob.min_vals.is_some() && !ob.multiple {
                panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
                    values", ob.name, ob.num_vals.unwrap());
            }
            if ob.max_vals.is_some() && !ob.multiple {
                panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
                    values", ob.name, ob.num_vals.unwrap());
            }
            // Check if there is anything in the blacklist (mutually excludes list) and add any
            // values
            if let Some(ref bl) = a.blacklist {
                let mut bhs = HashSet::new();
                // without derefing n = &&str
                for n in bl { bhs.insert(*n); }
                ob.blacklist = Some(bhs);
            }
            // Check if there is anything in the requires list and add any values
            if let Some(ref r) = a.requires {
                let mut rhs = HashSet::new();
                // without derefing n = &&str
                for n in r {
                    rhs.insert(*n);
                    if ob.required {
                        self.required.insert(*n);
                    }
                }
                ob.requires = Some(rhs);
            }
            // Check if there is anything in the possible values and add those as well
            if let Some(ref p) = a.possible_vals {
                let mut phs = BTreeSet::new();
                // without derefing n = &&str
                for n in p { phs.insert(*n); }
                ob.possible_vals = Some(phs);
            }
            self.opts.insert(a.name, ob);
        } else {
            if !a.empty_vals {
                // Empty vals defaults to true, so if it's false it was manually set
                panic!("The argument '{}' cannot have empty_values() set because it is a flag. \
                    Perhaps you mean't to set takes_value(true) as well?", a.name);
            }
            if a.required {
                panic!("The argument '{}' cannot be required(true) because it has no index() or \
                    takes_value(true)", a.name);
            }
            if a.possible_vals.is_some() {
                panic!("The argument '{}' cannot have a specific value set because it doesn't \
                have takes_value(true) set", a.name);
            }
            // No need to check for index() or takes_value() as that is handled above

            let mut fb = FlagBuilder {
                name: a.name,
                short: a.short,
                long: a.long,
                help: a.help,
                blacklist: None,
                global: a.global,
                multiple: a.multiple,
                requires: None,
            };
            // Check if there is anything in the blacklist (mutually excludes list) and add any
            // values
            if let Some(ref bl) = a.blacklist {
                let mut bhs = HashSet::new();
                // without derefing n = &&str
                for n in bl { bhs.insert(*n); }
                fb.blacklist = Some(bhs);
            }
            // Check if there is anything in the requires list and add any values
            if let Some(ref r) = a.requires {
                let mut rhs = HashSet::new();
                // without derefing n = &&str
                for n in r { rhs.insert(*n); }
                fb.requires = Some(rhs);
            }
            self.flags.insert(a.name, fb);
        }
        if a.global {
            if a.required {
                panic!("Global arguments cannot be required.\n\n\t'{}' is marked as global and required", a.name);
            }
            self.global_args.push(a);
        }
    }

    /// Adds multiple arguments to the list of valid possibilties by iterating over a Vec of Args
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .args(
    ///         vec![Arg::from_usage("[debug] -d 'turns on debugging info"),
    ///              Arg::with_name("input").index(1).help("the input file to use")]
    ///     )
    /// # ;
    /// ```
    pub fn args(mut self, args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>)
                -> Self {
        for arg in args.into_iter() {
            self = self.arg(arg);
        }
        self
    }

    /// A convienience method for adding a single basic argument (one without advanced
    /// relational rules) from a usage type string. The string used follows the same rules and
    /// syntax as `Arg::from_usage()`
    ///
    /// The downside to using this method is that you can not set any additional properties of the
    /// `Arg` other than what `Arg::from_usage()` supports.
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .arg_from_usage("-c --conf=<config> 'Sets a configuration file to use'")
    /// # ;
    /// ```
    pub fn arg_from_usage(mut self, usage: &'ar str) -> Self {
        self = self.arg(Arg::from_usage(usage));
        self
    }

    /// Adds multiple arguments at once from a usage string, one per line. See `Arg::from_usage()`
    /// for details on the syntax and rules supported.
    ///
    /// Like `App::arg_from_usage()` the downside is you only set properties for the `Arg`s which
    /// `Arg::from_usage()` supports. But here the benefit is pretty strong, as the readability is
    /// greatly enhanced, especially if you don't need any of the more advanced configuration
    /// options.
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// App::new("myprog")
    ///     .args_from_usage(
    ///         "-c --conf=[config] 'Sets a configuration file to use'
    ///          [debug]... -d 'Sets the debugging level'
    ///          <input> 'The input file to use'"
    ///     )
    /// # ;
    /// ```
    pub fn args_from_usage(mut self, usage: &'ar str) -> Self {
        for l in usage.lines() {
            self = self.arg(Arg::from_usage(l.trim()));
        }
        self
    }

    /// Adds an ArgGroup to the application. ArgGroups are a family of related arguments. By
    /// placing them in a logical group, you make easier requirement and exclusion rules. For
    /// instance, you can make an ArgGroup required, this means that one (and *only* one) argument
    /// from that group must be present. Using more than one argument from an ArgGroup causes a
    /// failure (graceful exit).
    ///
    /// You can also do things such as name an ArgGroup as a confliction, meaning any of the
    /// arguments that belong to that group will cause a failure if present.
    ///
    /// Perhaps the most common use of ArgGroups is to require one and *only* one argument to be
    /// present out of a given set. For example, lets say that you were building an application
    /// where one could set a given version number by supplying a string using an option argument,
    /// such as `--set-ver v1.2.3`, you also wanted to support automatically using a previous
    /// version numer and simply incrementing one of the three numbers, so you create three flags
    /// `--major`, `--minor`, and `--patch`. All of these arguments shouldn't be used at one time
    /// but perhaps you want to specify that *at least one* of them is used. You can create a
    /// group
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, ArgGroup};
    /// # App::new("app")
    /// .args_from_usage("--set-ver [ver] 'set the version manually'
    ///                   --major         'auto increase major'
    ///                   --minor         'auto increase minor'
    ///                   --patch         'auto increase patch")
    /// .arg_group(ArgGroup::with_name("vers")
    ///                     .add_all(vec!["ver", "major", "minor","patch"])
    ///                     .required(true))
    /// # ;
    pub fn arg_group(mut self, group: ArgGroup<'ar, 'ar>) -> Self {
        if group.required {
            self.required.insert(group.name);
            if let Some(ref reqs) = group.requires {
                for r in reqs {
                    self.required.insert(r);
                }
            }
            if let Some(ref bl) = group.conflicts {
                for b in bl {
                    self.blacklist.insert(b);
                }
            }
        }
        let mut found = false;
        if let Some(ref mut grp) = self.groups.get_mut(group.name) {
            for a in group.args.iter() {
                grp.args.insert(a);
            }
            grp.requires = group.requires.clone();
            grp.conflicts = group.conflicts.clone();
            grp.required = group.required;
            found = true;
        }
        if !found {
            self.groups.insert(group.name, group);
        }
        self
    }

    /// Adds a ArgGroups to the application. ArgGroups are a family of related arguments. By
    /// placing them in a logical group, you make easier requirement and exclusion rules. For
    /// instance, you can make an ArgGroup required, this means that one (and *only* one) argument
    /// from that group must be present. Using more than one argument from an ArgGroup causes a
    /// failure (graceful exit).
    ///
    /// You can also do things such as name an ArgGroup as a confliction, meaning any of the
    /// arguments that belong to that group will cause a failure if present.
    ///
    /// Perhaps the most common use of ArgGroups is to require one and *only* one argument to be
    /// present out of a given set. For example, lets say that you were building an application
    /// where one could set a given version number by supplying a string using an option argument,
    /// such as `--set-ver v1.2.3`, you also wanted to support automatically using a previous
    /// version numer and simply incrementing one of the three numbers, so you create three flags
    /// `--major`, `--minor`, and `--patch`. All of these arguments shouldn't be used at one time
    /// but perhaps you want to specify that *at least one* of them is used. You can create a
    /// group
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, ArgGroup};
    /// # App::new("app")
    /// .args_from_usage("--set-ver [ver] 'set the version manually'
    ///                   --major         'auto increase major'
    ///                   --minor         'auto increase minor'
    ///                   --patch         'auto increase patch")
    /// .arg_group(ArgGroup::with_name("vers")
    ///                     .add_all(vec!["ver", "major", "minor","patch"])
    ///                     .required(true))
    /// # ;
    pub fn arg_groups(mut self, groups: Vec<ArgGroup<'ar, 'ar>>) -> Self {
        for g in groups {
            self = self.arg_group(g);
        }
        self
    }

    /// Adds a subcommand to the list of valid possibilties. Subcommands are effectively sub apps,
    /// because they can contain their own arguments, subcommands, version, usage, etc. They also
    /// function just like apps, in that they get their own auto generated help, version, and
    /// usage.
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg, SubCommand};
    /// # App::new("myprog")
    /// .subcommand(SubCommand::with_name("config")
    ///                .about("Controls configuration features")
    ///                .arg_from_usage("<config> 'Required configuration file to use'"))
    ///             // Additional subcommand configuration goes here, such as other arguments...
    /// # ;
    /// ```
    pub fn subcommand(mut self, mut subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
                      -> Self {
        if subcmd.name == "help" { self.needs_subcmd_help = false; }
        if self.versionless_scs.is_some() && self.versionless_scs.unwrap() {
            subcmd.versionless_scs = Some(false);
        }
        if self.global_ver && subcmd.version.is_none() && self.version.is_some() {
            subcmd.version = Some(self.version.unwrap());
        }
        self.subcommands.insert(subcmd.name.clone(), subcmd);
        self
    }

    /// Adds multiple subcommands to the list of valid possibilties by iterating over a Vec of
    /// `SubCommand`s
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg, SubCommand};
    /// # App::new("myprog")
    /// .subcommands( vec![
    ///        SubCommand::with_name("config").about("Controls configuration functionality")
    ///                                 .arg(Arg::with_name("config_file").index(1)),
    ///        SubCommand::with_name("debug").about("Controls debug functionality")])
    /// # ;
    /// ```
    pub fn subcommands(mut self, subcmds: Vec<App<'a, 'v, 'ab, 'u, 'h, 'ar>>)
                       -> Self {
        for subcmd in subcmds.into_iter() {
            self = self.subcommand(subcmd);
        }
        self
    }

    fn get_group_members(&self, group: &str) -> Vec<String> {
        let mut g_vec = vec![];
        let mut args = vec![];

        for n in self.groups.get(group).unwrap().args.iter() {
            if let Some(f) = self.flags.get(n) {
                args.push(f.to_string());
            } else if let Some(f) = self.opts.get(n) {
                args.push(f.to_string());
            } else if self.groups.contains_key(n) {
                g_vec.push(*n);
            } else {
                if let Some(idx) = self.positionals_name.get(n) {
                    if let Some(p) = self.positionals_idx.get(&idx) {
                        args.push(p.to_string());
                    }
                }
            }
        }

        g_vec.dedup();
        if !g_vec.is_empty() {
            for av in g_vec.iter().map(|g| self.get_group_members(g)) {
                for a in av {
                    args.push(a);
                }
            }
        }
        args.dedup();
        args.iter().map(ToOwned::to_owned).collect()
    }

    fn get_group_members_names(&self, group: &'ar str) -> Vec<&'ar str> {
        let mut g_vec = vec![];
        let mut args = vec![];

        for n in self.groups.get(group).unwrap().args.iter() {
            if self.flags.contains_key(n) {
                args.push(*n);
            } else if self.opts.contains_key(n) {
                args.push(*n);
            } else if self.groups.contains_key(n) {
                g_vec.push(*n);
            } else {
                if self.positionals_name.contains_key(n) {
                    args.push(*n);
                }
            }
        }

        g_vec.dedup();
        if !g_vec.is_empty() {
            for av in g_vec.iter().map(|g| self.get_group_members_names(g)) {
                for a in av {
                    args.push(a);
                }
            }
        }
        args.dedup();
        args.iter().map(|s| *s).collect()
    }

    fn get_required_from(&self, mut reqs: Vec<&'ar str>) -> VecDeque<String> {
        reqs.dedup();
        let mut c_flags = vec![];
        let mut c_pos = vec![];
        let mut c_opt = vec![];
        let mut grps = vec![];
        for name in reqs.iter() {
            if self.flags.contains_key(name) {
                c_flags.push(name);
            } else if self.opts.contains_key(name) {
                c_opt.push(name);
            } else if self.groups.contains_key(name) {
                grps.push(*name);
            } else {
                c_pos.push(name);
            }
        }
        let mut tmp_f = vec![];
        for f in c_flags.iter() {
            if let Some(f) = self.flags.get(*f) {
                if let Some(ref rl) = f.requires {
                    for r in rl.iter() {
                        if !reqs.contains(r) {
                            if self.flags.contains_key(r) {
                                tmp_f.push(r);
                            } else if self.opts.contains_key(r) {
                                c_opt.push(r);
                            } else if self.groups.contains_key(r) {
                                grps.push(*r);
                            } else {
                                c_pos.push(r);
                            }
                        }
                    }
                }
            }
        }
        for f in tmp_f.into_iter() {
            c_flags.push(f);
        }
        let mut tmp_o = vec![];
        for f in &c_opt {
            if let Some(f) = self.opts.get(*f) {
                if let Some(ref rl) = f.requires {
                    for r in rl.iter() {
                        if !reqs.contains(r) {
                            if self.flags.contains_key(r) {
                                c_flags.push(r);
                            } else if self.opts.contains_key(r) {
                                tmp_o.push(r);
                            } else if self.groups.contains_key(r) {
                                grps.push(*r);
                            } else {
                                c_pos.push(r);
                            }
                        }
                    }
                }
            }
        }
        for f in tmp_o.into_iter() {
            c_opt.push(f);
        }
        let mut tmp_p = vec![];
        for f in c_pos.iter() {
            if let Some(f) = self.flags.get(*f) {
                if let Some(ref rl) = f.requires {
                    for r in rl.iter() {
                        if !reqs.contains(r) {
                            if self.flags.contains_key(r) {
                                c_flags.push(r);
                            } else if self.opts.contains_key(r) {
                                c_opt.push(r);
                            } else if self.groups.contains_key(r) {
                                grps.push(*r);
                            } else {
                                tmp_p.push(r);
                            }
                        }
                    }
                }
            }
        }
        for f in tmp_p.into_iter() {
            c_flags.push(f);
        }


        let mut ret_val = VecDeque::new();

        let mut pmap = BTreeMap::new();
        for p in c_pos.into_iter() {
            if let Some(idx) = self.positionals_name.get(p) {
                if let Some(ref p) = self.positionals_idx.get(&idx) {
                    pmap.insert(p.index, format!("{}", p));
                }
            }
        }
        pmap.into_iter().map(|(_, s)| ret_val.push_back(s)).collect::<Vec<_>>();
        for f in c_flags.into_iter() {
             ret_val.push_back(format!("{}", self.flags.get(*f).unwrap()));
        }
        for o in c_opt.into_iter() {
             ret_val.push_back(format!("{}", self.opts.get(*o).unwrap()));
        }
        for g in grps.into_iter() {
            let g_string = self.get_group_members(g).iter()
                                                    .fold(String::new(), |acc, s| {
                                                        acc + &format!(" {} |",s)[..]
                                                    });
            ret_val.push_back(format!("[{}]", &g_string[..g_string.len()-1]));
        }

        ret_val
    }

    // Creates a usage string if one was not provided by the user manually. This happens just
    // after all arguments were parsed, but before any subcommands have been parsed (so as to
    // give subcommands their own usage recursively)
    fn create_usage(&self, matches: Option<Vec<&'ar str>>) -> String {
        use ::std::fmt::Write;
        let mut usage = String::with_capacity(75);
        usage.push_str("USAGE:\n\t");
        if let Some(u) = self.usage_str {
            usage.push_str(u);
        } else if let Some(tmp_vec) = matches {
            let mut hs = self.required.iter().map(|n| *n).collect::<Vec<_>>();
            tmp_vec.iter().map(|n| hs.push(*n)).collect::<Vec<_>>();
            let reqs = self.get_required_from(hs);

            let r_string = reqs.iter().fold(String::new(), |acc, s| acc + &format!(" {}", s)[..]);

            write!(&mut usage, "{}{}",
                self.usage.clone().unwrap_or(self.bin_name.clone().unwrap_or(self.name.clone())),
                r_string
            ).ok().expect(INTERNAL_ERROR_MSG);
            if self.no_sc_error {
                write!(&mut usage, " <SUBCOMMAND>").ok().expect(INTERNAL_ERROR_MSG);
            }
        } else {
            usage.push_str(&*self.usage.clone()
                                       .unwrap_or(self.bin_name.clone()
                                                               .unwrap_or(self.name.clone())));

            let mut reqs = self.required.iter().map(|n| *n).collect::<Vec<_>>();
            // If it's required we also need to ensure all previous positionals are required too
            let mut found = false;
            for p in self.positionals_idx.values().rev() {
                if found {
                    reqs.push(p.name);
                    continue;
                }
                if p.required {
                    found = true;
                    reqs.push(p.name);
                }
            }
            let req_strings = self.get_required_from(reqs);
            let req_string = req_strings.iter()
                                        .fold(String::new(), |acc, s| {
                                            acc + &format!(" {}", s)[..]
                                        });


            if !self.flags.is_empty() && !self.unified_help {
                usage.push_str(" [FLAGS]");
            } else {
                usage.push_str(" [OPTIONS]");
            }
            if !self.unified_help 
                && !self.opts.is_empty() && self.opts.values().any(|a| !a.required) {
                usage.push_str(" [OPTIONS]");
            }
            // places a '--' in the usage string if there are args and options 
            // supporting multiple values
            if !self.positionals_idx.is_empty()
                && self.opts.values().any(|a| a.multiple )
                && !self.opts.values().any(|a| a.required)
                && self.subcommands.is_empty() {
                usage.push_str(" [--]")
            }
            if !self.positionals_idx.is_empty() && self.positionals_idx.values()
                                                                       .any(|a| !a.required) {
                usage.push_str(" [ARGS]");
            }

            usage.push_str(&req_string[..]);

            if !self.subcommands.is_empty() && !self.no_sc_error {
                usage.push_str(" [SUBCOMMAND]");
            } else if self.no_sc_error && !self.subcommands.is_empty() {
                usage.push_str(" <SUBCOMMAND>");
            }
        }

        usage.shrink_to_fit();
        usage
    }

    // // Prints the usage statement to the user
    // fn print_usage(&self, more_info: bool, matches: Option<Vec<&str>>) {
    //     print!("{}",self.create_usage(matches));
    //     if more_info {
    //         println!("\n\nFor more information try {}", Format::Good("--help"));
    //     }
    // }

    // Prints the full help message to the user
    fn print_help(&self) {
        if let Some(h) = self.help_str {
            println!("{}", h);
            return
        }

        // Print the version
        print!("{} {}\n", &self.bin_name.clone().unwrap_or(
            self.name.clone())[..].replace(" ", "-"),
            self.version.unwrap_or("")
        );
        let flags = !self.flags.is_empty();
        let pos = !self.positionals_idx.is_empty();
        let opts = !self.opts.is_empty();
        let subcmds = !self.subcommands.is_empty();

        let mut longest_flag = 0;
        for fl in self.flags
            .values()
            .filter(|ref f| f.long.is_some())
            // 2='--'
            .map(|ref a| a.to_string().len() ) {
            if fl > longest_flag { longest_flag = fl; }
        }
        let mut longest_opt= 0;
        for ol in self.opts
            .values()
            // .filter(|ref o| o.long.is_some())
            .map(|ref a|
                a.to_string().len() // + if a.short.is_some() { 4 } else { 0 }
            ) {
            if ol > longest_opt {
                longest_opt = ol;
            }
        }
        let mut longest_pos = 0;
        for pl in self.positionals_idx
            .values()
            .map(|ref f| f.to_string().len() ) {
            if pl > longest_pos {longest_pos = pl;}
        }
        let mut longest_sc = 0;
        for scl in self.subcommands
            .values()
            .map(|ref f| f.name.len() ) {
            if scl > longest_sc {longest_sc = scl;}
        }

        if let Some(author) = self.author {
            print!("{}\n", author);
        }
        if let Some(about) = self.about {
            print!("{}\n", about);
        }

        print!("\n{}", self.create_usage(None));

        if flags || opts || pos || subcmds {
            print!("\n");
        }

        let tab = "    ";
        if flags {
            if !self.unified_help {
                print!("\nFLAGS:\n"); 
            } else {
                print!("\nOPTIONS:\n")
            }
            for v in self.flags.values() {
                print!("{}", tab);
                if let Some(s) = v.short {
                    print!("-{}",s);
                } else { 
                    print!("{}", tab);
                }
                if let Some(l) = v.long {
                    print!("{}--{}",
                        if v.short.is_some() { ", " } else {""},
                        l
                    );
                    self.print_spaces(
                            if !self.unified_help {
                                (longest_flag + 4)
                            } else {
                                (longest_opt + 4)
                            } - (l.len() + 2)
                    );
                } else {
                    // 6 is tab (4) + -- (2)
                    self.print_spaces(
                        if !self.unified_help {
                            (longest_flag + 6)
                        } else {
                            (longest_opt + 6)
                        }
                    );
                }
                if let Some(h) = v.help {
                    if h.contains("{n}") {
                        let mut hel = h.split("{n}");
                        while let Some(part) = hel.next() {
                            print!("{}\n", part);
                            self.print_spaces(
                                if !self.unified_help {
                                    longest_flag
                                } else {
                                    longest_opt
                                } + 12);
                            print!("{}", hel.next().unwrap_or(""));
                        }
                    } else {
                        print!("{}", h);
                    }
                }
                print!("\n");
            }
        }
        if opts {
            if !self.unified_help {
                print!("\nOPTIONS:\n"); 
            } else {
                // maybe erase
            }
            for v in self.opts.values() {
                // if it supports multiple we add '...' i.e. 3 to the name length
                print!("{}", tab);
                if let Some(s) = v.short {
                    print!("-{}",s);
                } else {
                    print!("{}", tab);
                }
                if let Some(l) = v.long {
                    print!("{}--{}", if v.short.is_some() {", "} else {""}, l);
                } 
                if let Some(ref vec) = v.val_names {
                    for val in vec.iter() {
                        print!(" <{}>", val);
                    }
                } else if let Some(num) = v.num_vals {
                    for _ in (0..num) {
                        print!(" <{}>", v.name);
                    }
                } else {
                    print!(" <{}>{}", v.name, if v.multiple{"..."} else {""});
                }
                if v.long.is_some() {
                    self.print_spaces(
                        (longest_opt + 4) - (v.to_string().len())
                    );
                } else {
                    // 8 = tab + '-a, '.len()
                    self.print_spaces((longest_opt + 8) - (v.to_string().len()));
                };
                print_opt_help!(self, v, longest_opt + 12);
                print!("\n");
            }
        }
        if pos {
            print!("\nARGS:\n");
            for v in self.positionals_idx.values() {
                // let mult = if v.multiple { 3 } else { 0 };
                print!("{}", tab);
                print!("{}", v.name);
                if v.multiple {
                    print!("...");
                }
                self.print_spaces((longest_pos + 4) - (v.to_string().len()));
                if let Some(h) = v.help {
                    if h.contains("{n}") {
                        let mut hel = h.split("{n}");
                        while let Some(part) = hel.next() {
                            print!("{}\n", part);
                            self.print_spaces(longest_pos + 6);
                            print!("{}", hel.next().unwrap_or(""));
                        }
                    } else {
                        print!("{}", h);
                    }
                }
                print!("\n");
            }
        }
        if subcmds {
            print!("\nSUBCOMMANDS:\n");
            for sc in self.subcommands.values() {
                print!("{}{}", tab, sc.name);
                self.print_spaces((longest_sc + 4) - (sc.name.len()));
                if let Some(a) = sc.about {
                    if a.contains("{n}") {
                        let mut ab = a.split("{n}");
                        while let Some(part) = ab.next() {
                            print!("{}\n", part);
                            self.print_spaces(longest_sc + 8);
                            print!("{}", ab.next().unwrap_or(""));
                        }
                    } else {
                        print!("{}", a);
                    }
                } 
                print!("\n");
            }
        }

        if let Some(h) = self.more_help {
            print!("\n{}", h);
        }

        // flush the buffer
        println!("");

        self.exit(0);
    }

    // Used when spacing arguments and their help message when displaying help information
    fn print_spaces(&self, num: usize) {
        for _ in (0..num) {
            print!(" ");
        }
    }

    // Prints the version to the user and exits if quit=true
    fn print_version(&self, quit: bool) {
        // Print the binary name if existing, but replace all spaces with hyphens in case we're
        // dealing with subcommands i.e. git mv is translated to git-mv
        println!("{} {}", &self.bin_name.clone().unwrap_or(
            self.name.clone())[..].replace(" ", "-"),
            self.version.unwrap_or("")
        );
        if quit { self.exit(0); }
    }

    // Exits with a status code passed to the OS
    // This is legacy from before std::process::exit() and may be removed evenutally
    fn exit(&self, status: i32) {
        if self.wait_on_error {
            wlnerr!("\nPress [ENTER] / [RETURN] to continue...");
            let mut s = String::new();
            let i = io::stdin();
            i.lock().read_line(&mut s).unwrap();
        }
        ::std::process::exit(status);
    }

    // Reports and error to stderr along with an optional usage statement and optionally quits
    fn report_error(&self, msg: String, quit: bool, matches: Option<Vec<&str>>) {
        wlnerr!("{} {}\n\n{}\n\nFor more information try {}",
            Format::Error(&format!("error:")[..]),
            msg,
            self.create_usage(matches),
            Format::Good("--help")
        );
       if quit { self.exit(1); }
    }

    /// Starts the parsing process. Called on top level parent app **ONLY** then recursively calls
    /// the real parsing function for all subcommands
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// let matches = App::new("myprog")
    ///     // Args and options go here...
    ///     .get_matches();
    /// ```
    pub fn get_matches(self) -> ArgMatches<'ar, 'ar> {
        let args: Vec<_> = env::args().collect();

        self.get_matches_from(args)
    }

    /// Starts the parsing process. Called on top level parent app **ONLY** then recursively calls
    /// the real parsing function for all subcommands
    ///
    /// **NOTE:** The first argument will be parsed as the binary name.
    ///
    /// **NOTE:** This method should only be used when absolutely necessary, such as needing to
    /// parse arguments from something other than `std::env::args()`. If you are unsure, use
    /// `App::get_matches()`
    ///
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use clap::{App, Arg};
    /// let arg_vec = vec!["my_prog", "some", "args", "to", "parse"];
    ///
    /// let matches = App::new("myprog")
    ///     // Args and options go here...
    ///     .get_matches_from(arg_vec);
    /// ```
    pub fn get_matches_from<I, T>(mut self, itr: I)
                                  -> ArgMatches<'ar, 'ar>
                                  where I: IntoIterator<Item=T>,
                                        T: AsRef<str> {
        self.verify_positionals();
        self.propogate_globals();

        let mut matches = ArgMatches::new();

        let mut it = itr.into_iter();
        if let Some(name) = it.next() {
            let p = Path::new(name.as_ref());
            if let Some(f) = p.file_name() {
                if let Ok(s) = f.to_os_string().into_string() {
                    if let None = self.bin_name {
                        self.bin_name = Some(s);
                    }
                }
            }
        }
        self.get_matches_with(&mut matches, &mut it);

        matches
    }

    fn verify_positionals(&mut self) {
        // Because you must wait until all arguments have been supplied, this is the first chance
        // to make assertions on positional argument indexes
        //
        // Firt we verify that the index highest supplied index, is equal to the number of
        // positional arguments to verify there are no gaps (i.e. supplying an index of 1 and 3
        // but no 2)
        //
        // Next we verify that only the highest index has a .multiple(true) (if any)
        if let Some((idx, ref p)) = self.positionals_idx.iter().rev().next() {
            if *idx as usize != self.positionals_idx.len() {
                panic!("Found positional argument \"{}\" who's index is {} but there are only {} \
                    positional arguments defined", p.name, idx, self.positionals_idx.len());
            }
        }
        if let Some(ref p) = self.positionals_idx.values()
                                                 .filter(|ref a| a.multiple)
                                                 .filter(|ref a| {
                                                    a.index as usize != self.positionals_idx.len()
                                                })
                                                 .next() {
            panic!("Found positional argument \"{}\" which accepts multiple values but it's not \
                the last positional argument (i.e. others have a higher index)", p.name);
        }

        // If it's required we also need to ensure all previous positionals are required too
        let mut found = false;
        for (_, p) in self.positionals_idx.iter_mut().rev() {
            if found {
                p.required = true;
                self.required.insert(p.name);
                continue;
            }
            if p.required {
                found = true;
            }
        }
    }

    fn propogate_globals(&mut self) {
        for (_,sc) in self.subcommands.iter_mut() {
            {
                for a in self.global_args.iter() {
                    sc.add_arg(a.into());
                }
            }
            sc.propogate_globals();
        }
    }


    fn possible_values_error(&self,
                             arg: &str,
                             opt: &str,
                             p_vals: &BTreeSet<&str>,
                             matches: &ArgMatches<'ar, 'ar>) {
        let suffix = App::did_you_mean_suffix(arg, p_vals.iter(),
                                              DidYouMeanMessageStyle::EnumValue);

        self.report_error(format!("'{}' isn't a valid value for '{}'{}{}",
                                    Format::Warning(arg),
                                    Format::Warning(opt),
                                    format!("\n\t[valid values:{}]\n",
                                        p_vals.iter()
                                              .fold(String::new(), |acc, name| {
                                                  acc + &format!(" {}",name)[..]
                                              })),
                                    suffix.0),
                                        true,
                                        Some(matches.args.keys().map(|k| *k).collect()));
    }

    // The actual parsing function
    fn get_matches_with<I, T>(&mut self, matches: &mut ArgMatches<'ar, 'ar>, it: &mut I)
                        where I: Iterator<Item=T>,
                              T: AsRef<str> {
        self.create_help_and_version();

        let mut pos_only = false;
        let mut subcmd_name: Option<String> = None;
        let mut needs_val_of: Option<&str> = None;
        let mut pos_counter = 1;
        let mut val_counter = 0;
        while let Some(arg) = it.next() {
            let arg_slice = arg.as_ref();
            let mut skip = false;
            let new_arg = if arg_slice.starts_with("-") {
                if arg_slice.len() == 1 { false } else { true }
            } else {
                false
            };
            if !pos_only && !new_arg && !self.subcommands.contains_key(arg_slice) {
                if let Some(nvo) = needs_val_of {
                    if let Some(ref opt) = self.opts.get(nvo) {
                        if let Some(ref p_vals) = opt.possible_vals {
                            if !p_vals.is_empty() {
                                if !p_vals.contains(arg_slice) {
                                    self.possible_values_error(arg_slice, &opt.to_string(),
                                                                          p_vals, matches);
                                }
                            }
                        }
                        if let Some(num) = opt.num_vals {
                            if let Some(ref ma) = matches.args.get(opt.name) {
                                if let Some(ref vals) = ma.values {
                                    if num == vals.len() as u8 && !opt.multiple {
                                        self.report_error(format!("The argument '{}' was found, \
                                            but '{}' only expects {} values",
                                                Format::Warning(arg.as_ref()),
                                                Format::Warning(opt.to_string()),
                                                Format::Good(vals.len().to_string())),
                                            true,
                                            Some(
                                                matches.args.keys().map(|k| *k).collect()
                                            )
                                        );
                                    }
                                }
                            }
                        }

                        if !opt.empty_vals &&
                            matches.args.contains_key(opt.name) &&
                            arg_slice.is_empty() {
                            self.report_error(format!("The argument '{}' does not allow empty \
                                    values, but one was found.", Format::Warning(opt.to_string())),
                                true,
                                Some(matches.args.keys()
                                                 .map(|k| *k).collect()));
                        }
                        if let Some(ref mut o) = matches.args.get_mut(opt.name) {
                            // Options have values, so we can unwrap()
                            if let Some(ref mut vals) = o.values {
                                let len = vals.len() as u8 + 1;
                                vals.insert(len, arg_slice.to_owned());
                            }

                            // if it's multiple the occurrences are increased when originall found
                            o.occurrences = if opt.multiple {
                                o.occurrences + 1
                            } else {
                                skip = true;
                                1
                            };
                            if let Some(ref vals) = o.values {
                                let len = vals.len() as u8;
                                if let Some(num) = opt.max_vals {
                                    if len != num { continue }
                                } else if let Some(num) = opt.num_vals {
                                    if opt.multiple {
                                        val_counter += 1;
                                        if val_counter != num { 
                                            continue 
                                        } else {
                                            val_counter = 0;
                                        }
                                    } else {
                                        if len != num { continue }
                                    }
                                } else if !skip {
                                    continue
                                }
                            }
                        }
                        skip = true;
                    }
                }
            }
            if skip {
                needs_val_of = None;
                continue;
            } else if let Some(ref name) = needs_val_of {
                if let Some(ref o) = self.opts.get(name) {
                    if !o.multiple {
                        self.report_error(
                            format!("The argument '{}' requires a value but none was supplied",
                                Format::Warning(o.to_string())),
                            true,
                            Some(matches.args.keys().map(|k| *k).collect() ) );
                    }
                }
            }

            if arg_slice.starts_with("--") && !pos_only {
                if arg_slice.len() == 2 {
                    pos_only = true;
                    continue;
                }
                // Single flag, or option long version
                needs_val_of = self.parse_long_arg(matches, arg_slice);
            } else if arg_slice.starts_with("-") && arg_slice.len() != 1 && ! pos_only {
                needs_val_of = self.parse_short_arg(matches, arg_slice);
            } else {
                // Positional or Subcommand
                // If the user pased `--` we don't check for subcommands, because the argument they
                // may be trying to pass might match a subcommand name
                if !pos_only {
                    if self.subcommands.contains_key(arg_slice) {
                        if arg_slice == "help" {
                            self.print_help();
                        }
                        subcmd_name = Some(arg_slice.to_owned());
                        break;
                    }

                    if let Some(candidate_subcommand) = did_you_mean(arg_slice,
                                                                     self.subcommands.keys()) {
                        self.report_error(
                            format!("The subcommand '{}' isn't valid\n\tDid you mean '{}' ?\n\n\
                            If you received this message in error, try \
                            re-running with '{} {} {}'",
                                Format::Warning(arg.as_ref()),
                                Format::Good(candidate_subcommand),
                                self.bin_name.clone().unwrap_or(self.name.clone()),
                                Format::Good("--"),
                                arg_slice),
                            true,
                            None);
                    }
                }

                if self.positionals_idx.is_empty() {
                    self.report_error(
                        format!("Found argument '{}', but {} wasn't expecting any",
                            Format::Warning(arg.as_ref()),
                            self.bin_name.clone().unwrap_or(self.name.clone())),
                        true,
                        Some(matches.args.keys().map(|k| *k).collect()));
                }
                // If we find that an argument requires a positiona, we need to update all the
                // previous positionals too. This will denote where to start
                // let mut req_pos_from_name = None;
                if let Some(p) = self.positionals_idx.get(&pos_counter) {
                    if self.blacklist.contains(p.name) {
                        matches.args.remove(p.name);
                        self.report_error(format!("The argument '{}' cannot be used with {}",
                            Format::Warning(p.to_string()),
                            match self.blacklisted_from(p.name, &matches) {
                                Some(name) => format!("'{}'", Format::Warning(name)),
                                None       => "one or more of the other specified \
                                               arguments".to_owned()
                            }),
                            true,
                            Some(matches.args.keys().map(|k| *k).collect()));
                    }

                    if let Some(ref p_vals) = p.possible_vals {
                        if !p_vals.is_empty() {
                            if !p_vals.contains(arg_slice) {
                                self.possible_values_error(arg_slice, &p.to_string(),
                                                                       p_vals, matches);
                            }
                        }
                    }
                    // Have we made the update yet?
                    let mut done = false;
                    if p.multiple {
                        if let Some(num) = p.num_vals {
                            if let Some(ref ma) = matches.args.get(p.name) {
                                if let Some(ref vals) = ma.values {
                                    if vals.len() as u8 == num {
                                        self.report_error(format!("The argument '{}' was found, \
                                            but '{}' wasn't expecting any more values",
                                                Format::Warning(arg.as_ref()),
                                                Format::Warning(p.to_string())),
                                            true,
                                            Some(matches.args.keys()
                                                             .map(|k| *k).collect()));
                                    }
                                }
                            }
                        }
                        if !p.empty_vals && matches.args.contains_key(p.name)
                            && arg_slice.is_empty()  {
                            self.report_error(format!("The argument '{}' does not allow empty \
                                    values, but one was found.", Format::Warning(p.to_string())),
                                true,
                                Some(matches.args.keys()
                                                 .map(|k| *k).collect()));
                        }
                        // Check if it's already existing and update if so...
                        if let Some(ref mut pos) = matches.args.get_mut(p.name) {
                            done = true;
                            pos.occurrences += 1;
                            if let Some(ref mut vals) = pos.values {
                                let len = (vals.len() + 1) as u8;
                                vals.insert(len, arg_slice.to_owned());
                            }
                        }
                    } else {
                        // Only increment the positional counter if it doesn't allow multiples
                        pos_counter += 1;
                    }
                    // Was an update made, or is this the first occurrence?
                    if !done {
                        let mut bm = BTreeMap::new();
                        if !p.empty_vals && arg_slice.is_empty() {
                            self.report_error(format!("The argument '{}' does not allow empty \
                                values, but one was found.", Format::Warning(p.to_string())),
                                true,
                                Some(matches.args.keys()
                                                 .map(|k| *k).collect()));
                        }
                        bm.insert(1, arg_slice.to_owned());
                        matches.args.insert(p.name, MatchedArg{
                            occurrences: 1,
                            values: Some(bm),
                        });
                    }

                    if let Some(ref bl) = p.blacklist {
                        for name in bl {
                            self.blacklist.insert(name);
                            self.required.remove(name);
                        }
                    }

                    self.required.remove(p.name);
                    if let Some(ref reqs) = p.requires {
                        // Add all required args which aren't already found in matches to the
                        // final required list
                        for n in reqs {
                            if matches.args.contains_key(n) {continue;}

                            self.required.insert(n);
                        }
                    }

                    parse_group_reqs!(self, p);

                } else {
                    self.report_error(format!("The argument '{}' was found, but '{}' wasn't \
                        expecting any", Format::Warning(arg.as_ref()),
                            self.bin_name.clone().unwrap_or(self.name.clone())),
                        true,
                        Some(matches.args.keys().map(|k| *k).collect()));
                }
            }
        }
        match needs_val_of {
            Some(ref a) => {
                if let Some(o) = self.opts.get(a) {
                    if o.multiple && self.required.is_empty() {
                        let should_err = match matches.values_of(o.name) {
                            Some(ref v) => if v.len() == 0 { true } else { false },
                            None        => true,
                        };
                        if should_err {
                            self.report_error(
                                format!("The argument '{}' requires a value but there wasn't any \
                                supplied", Format::Warning(o.to_string())),
                                true,
                                Some(matches.args.keys().map(|k| *k).collect() ) );
                        }
                    }
                    else if !o.multiple {
                        self.report_error(
                            format!("The argument '{}' requires a value but none was supplied",
                                Format::Warning(o.to_string())),
                            true,
                            Some(matches.args.keys().map(|k| *k).collect() ) );
                    }
                    else {
                        self.report_error(format!("The following required arguments were not \
                            supplied:{}",
                            self.get_required_from(self.required.iter()
                                                                .map(|s| *s)
                                                                .collect::<Vec<_>>())
                                .iter()
                                .fold(String::new(), |acc, s| acc + &format!("\n\t'{}'",
                                    Format::Error(s.to_string()))[..])),
                            true,
                            Some(matches.args.keys().map(|k| *k).collect()));
                    }
                } else {
                    self.report_error(
                        format!("The argument '{}' requires a value but none was supplied",
                            Format::Warning(format!("{}", self.positionals_idx.get(
                                self.positionals_name.get(a).unwrap()).unwrap()))),
                            true,
                            Some(matches.args.keys().map(|k| *k).collect()));
                }
            }
            _ => {}
        }

        self.validate_blacklist(matches);
        self.validate_num_args(matches);


        matches.usage = Some(self.create_usage(None));

        if let Some(sc_name) = subcmd_name {
            use ::std::fmt::Write;
            let mut mid_string = String::new();
            if !self.subcmds_neg_reqs {
                let mut hs = self.required.iter().map(|n| *n).collect::<Vec<_>>();
                matches.args.keys().map(|k| hs.push(*k)).collect::<Vec<_>>();
                let reqs = self.get_required_from(hs);

                for s in reqs.iter() {
                    write!(&mut mid_string, " {}", s).ok().expect(INTERNAL_ERROR_MSG);
                }
            }
            mid_string.push_str(" ");
            if let Some(ref mut sc) = self.subcommands.get_mut(&sc_name) {
                let mut new_matches = ArgMatches::new();
                // bin_name should be parent's bin_name + [<reqs>] + the sc's name separated by a
                // space
                sc.usage = Some(format!("{}{}{}",
                    self.bin_name.clone().unwrap_or("".to_owned()),
                    if self.bin_name.is_some() {
                        mid_string
                    } else {
                        "".to_owned()
                    },
                    sc.name.clone()));
                sc.bin_name = Some(format!("{}{}{}",
                    self.bin_name.clone().unwrap_or("".to_owned()),
                    if self.bin_name.is_some() {
                        " "
                    } else {
                        ""
                    },
                    sc.name.clone()));
                sc.get_matches_with(&mut new_matches, it);
                matches.subcommand = Some(Box::new(SubCommand {
                    name: sc.name_slice,
                    matches: new_matches
                }));
            }
        } else if self.no_sc_error {
            let bn = self.bin_name.clone().unwrap_or(self.name.clone());
            self.report_error(format!("'{}' requires a subcommand but none was provided",
                    Format::Warning(&bn[..])),
                true,
                Some(matches.args.keys().map(|k| *k).collect()));
        } else if self.help_on_no_sc {
            self.print_help();
            self.exit(1);
        }
        if !self.required.is_empty() && !self.subcmds_neg_reqs {
            if self.validate_required(&matches) {
                self.report_error(format!("The following required arguments were not \
                    supplied:{}",
                    self.get_required_from(self.required.iter()
                                                        .map(|s| *s)
                                                        .collect::<Vec<_>>())
                        .iter()
                        .fold(String::new(), |acc, s| acc + &format!("\n\t'{}'",
                            Format::Error(s))[..])),
                    true,
                    Some(matches.args.keys().map(|k| *k).collect()));
            }
        }
        if matches.args.is_empty() && matches.subcommand_name().is_none() && self.help_on_no_args {
            self.print_help();
            self.exit(1);
        }
    }

    fn blacklisted_from(&self, name: &str, matches: &ArgMatches) -> Option<String> {
        for k in matches.args.keys() {
            if let Some(f) = self.flags.get(k) {
                if let Some(ref bl) = f.blacklist {
                    if bl.contains(name) {
                        return Some(format!("{}", f))
                    }
                }
            }
            if let Some(o) = self.opts.get(k) {
                if let Some(ref bl) = o.blacklist {
                    if bl.contains(name) {
                        return Some(format!("{}", o))
                    }
                }
            }
            if let Some(idx) = self.positionals_name.get(k) {
                if let Some(pos) = self.positionals_idx.get(idx) {
                    if let Some(ref bl) = pos.blacklist {
                        if bl.contains(name) {
                            return Some(format!("{}", pos))
                        }
                    }
                }
            }
         }
        None
    }

    fn create_help_and_version(&mut self) {
        // name is "hclap_help" because flags are sorted by name
        if self.needs_long_help {
            if self.help_short.is_none() && !self.short_list.contains(&'h') {
                self.help_short = Some('h');
            }
            let arg = FlagBuilder {
                name: "hclap_help",
                short: self.help_short,
                long: Some("help"),
                help: Some("Prints help information"),
                blacklist: None,
                multiple: false,
                global: false,
                requires: None,
            };
            self.long_list.insert("help");
            self.flags.insert("hclap_help", arg);
        }
        if self.needs_long_version 
            && self.versionless_scs.is_none() 
            || (self.versionless_scs.unwrap()) {
            if self.version_short.is_none() && !self.short_list.contains(&'V') {
                self.version_short = Some('V');
            }
            // name is "vclap_version" because flags are sorted by name
            let arg = FlagBuilder {
                name: "vclap_version",
                short: self.version_short,
                long: Some("version"),
                help: Some("Prints version information"),
                blacklist: None,
                multiple: false,
                global: false,
                requires: None,
            };
            self.long_list.insert("version");
            self.flags.insert("vclap_version", arg);
        }
        if self.needs_subcmd_help && !self.subcommands.is_empty() {
            self.subcommands.insert("help".to_owned(), App::new("help")
                                                            .about("Prints this message"));
        }
    }

    fn check_for_help_and_version(&self, arg: char) {
        if let Some(h) = self.help_short {
            if h == arg { self.print_help(); }
        }
        if let Some(v) = self.version_short {
            if v == arg { self.print_version(true); }
        }
    }

    fn parse_long_arg(&mut self, matches: &mut ArgMatches<'ar, 'ar> ,full_arg: &str)
                      -> Option<&'ar str> {
        let mut arg = full_arg.trim_left_matches(|c| c == '-');

        if arg == "help" && self.needs_long_help {
            self.print_help();
        } else if arg == "version" && self.needs_long_version {
            self.print_version(true);
        }

        let mut arg_val: Option<String> = None;

        if arg.contains("=") {
            let arg_vec: Vec<_> = arg.split("=").collect();
            arg = arg_vec[0];
            if let Some(ref v) = self.opts.values()
                                      .filter(|&v| v.long.is_some())
                                      .filter(|&v| v.long.unwrap() == arg).nth(0) {
                // prevents "--config= value" typo
                if arg_vec[1].len() == 0 && !v.empty_vals {
                    matches.args.insert(v.name, MatchedArg {
                        occurrences: 1,
                        values: None
                    });
                    self.report_error(format!("The argument '{}' requires a value, but none was \
                            supplied", Format::Warning(format!("--{}", arg))),
                        true,
                        Some(matches.args.keys().map(|k| *k).collect()));
                }
                arg_val = Some(arg_vec[1].to_owned());
            }
        }

        if let Some(ref v) = self.opts.values()
                                  .filter(|&v| v.long.is_some())
                                  .filter(|&v| v.long.unwrap() == arg).nth(0) {
            // Ensure this option isn't on the master mutually excludes list
            if self.blacklist.contains(v.name) {
                matches.args.remove(v.name);
                self.report_error(format!("The argument '{}' cannot be used with one or more of \
                    the other specified arguments", Format::Warning(format!("--{}", arg))),
                    true,
                    Some(matches.args.keys().map(|k| *k).collect()));
            }

            if matches.args.contains_key(v.name) {
                if !v.multiple {
                    self.report_error(format!("The argument '{}' was supplied more than once, but \
                            does not support multiple values",
                            Format::Warning(format!("--{}", arg))),
                        true,
                        Some(matches.args.keys().map(|k| *k).collect()));
                }
                if let Some(ref p_vals) = v.possible_vals {
                    if let Some(ref av) = arg_val {
                        if !p_vals.contains(&av[..]) {
                            self.possible_values_error(
                                    arg_val.as_ref().map(|v| &**v).unwrap_or(arg),
                                    &v.to_string(), p_vals, matches);
                        }
                    }
                }
                if arg_val.is_some() {
                    if !v.empty_vals && arg.is_empty() && matches.args.contains_key(v.name) {
                        self.report_error(format!("The argument '{}' does not allow empty \
                                values, but one was found.", Format::Warning(v.to_string())),
                            true,
                            Some(matches.args.keys()
                                             .map(|k| *k).collect()));
                    }
                    if let Some(ref mut o) = matches.args.get_mut(v.name) {
                        o.occurrences += 1;
                        if let Some(ref mut vals) = o.values {
                            let len = (vals.len() + 1) as u8;
                            vals.insert(len, arg_val.clone().unwrap());
                        }
                    }
                }
            } else {
                if !v.empty_vals && arg_val.is_some() && arg_val.clone().unwrap().is_empty() {
                    self.report_error(format!("The argument '{}' does not allow empty \
                            values, but one was found.", Format::Warning(v.to_string())),
                        true,
                        Some(matches.args.keys()
                                         .map(|k| *k).collect()));
                }
                matches.args.insert(v.name, MatchedArg{
                    occurrences: if arg_val.is_some() { 1 } else { 0 },
                    values: if arg_val.is_some() {
                        let mut bm = BTreeMap::new();
                        bm.insert(1, arg_val.clone().unwrap());
                        Some(bm)
                    } else {
                        Some(BTreeMap::new())
                    }
                });
            }

            if let Some(ref bl) = v.blacklist {
                for name in bl {
                    self.blacklist.insert(name);
                    self.required.remove(name);
                }
            }

            self.required.remove(v.name);

            if let Some(ref reqs) = v.requires {
                // Add all required args which aren't already found in matches to the
                // final required list
                for n in reqs {
                    if matches.args.contains_key(n) { continue; }

                    self.required.insert(n);
                }
            }

            parse_group_reqs!(self, v);

            match arg_val {
                None => { return Some(v.name); },
                _    => { return None; }
            }
        }

        if let Some(v) = self.flags.values()
                                   .filter(|&v| v.long.is_some())
                                   .filter(|&v| v.long.unwrap() == arg).nth(0) {
            // Ensure this flag isn't on the mutually excludes list
            if self.blacklist.contains(v.name) {
                matches.args.remove(v.name);
                self.report_error(format!("The argument '{}' cannot be used with {}",
                        Format::Warning(v.to_string()),
                        match self.blacklisted_from(v.name, matches) {
                            Some(name) => format!("'{}'", Format::Warning(name)),
                            None       => "one or more of the specified arguments".to_owned()
                        }),
                    true,
                    Some(matches.args.keys().map(|k| *k).collect()));
            }

            // Make sure this isn't one being added multiple times if it doesn't suppor it
            if matches.args.contains_key(v.name) && !v.multiple {
                self.report_error(format!("The argument '{}' was supplied more than once, but does \
                        not support multiple values", Format::Warning(v.to_string())),
                    true,
                    Some(matches.args.keys().map(|k| *k).collect()));
            }

            let mut
            done = false;
            if let Some(ref mut f) = matches.args.get_mut(v.name) {
                done = true;
                f.occurrences = if v.multiple { f.occurrences + 1 } else { 1 };
            }
            if !done {
                matches.args.insert(v.name, MatchedArg{
                    // name: v.name.to_owned(),
                    occurrences: 1,
                    values: None
                });
            }

            // If this flag was requierd, remove it
            // .. even though Flags shouldn't be required
            self.required.remove(v.name);

            // Add all of this flags "mutually excludes" list to the master list
            if let Some(ref bl) = v.blacklist {
                for name in bl {
                    self.blacklist.insert(name);
                    self.required.remove(name);
                }
            }

            // Add all required args which aren't already found in matches to the master list
            if let Some(ref reqs) = v.requires {
                for n in reqs {
                    if matches.args.contains_key(n) { continue; }

                    self.required.insert(n);
                }
            }

            parse_group_reqs!(self, v);

            return None;
        }

        let suffix = App::did_you_mean_suffix(arg,
                                              self.long_list.iter(),
                                              DidYouMeanMessageStyle::LongFlag);
        if let Some(name) = suffix.1 {
            if let Some(ref opt) = self.opts.values()
                                          .filter_map(|ref o| {
                                              if o.long.is_some() && o.long.unwrap() == name {
                                                  Some(o.name)
                                              } else {
                                                  None
                                              }
                                          })
                                          .next() {
                matches.args.insert(opt, MatchedArg {
                    occurrences: 0,
                    values: None
                });
            } else if let Some(ref flg) = self.flags.values()
                                          .filter_map(|ref f| {
                                              if f.long.is_some() && f.long.unwrap() == name {
                                                  Some(f.name)
                                              } else {
                                                  None
                                              }
                                          })
                                          .next() {
                matches.args.insert(flg, MatchedArg {
                    occurrences: 0,
                    values: None
                });
            }
        }

        self.report_error(format!("The argument '{}' isn't valid{}",
                Format::Warning(format!("--{}", arg)),
                suffix.0),
            true,
            Some(matches.args.keys().map(|k| *k).collect()));

        unreachable!();
    }

    fn parse_short_arg(&mut self, matches: &mut ArgMatches<'ar, 'ar> ,full_arg: &str)
                       -> Option<&'ar str> {
        let arg = &full_arg[..].trim_left_matches(|c| c == '-');
        if arg.len() > 1 {
            // Multiple flags using short i.e. -bgHlS
            for c in arg.chars() {
                self.check_for_help_and_version(c);
                if !self.parse_single_short_flag(matches, c) {
                    self.report_error(format!("The argument '{}' isn't valid",
                            Format::Warning(format!("-{}", c))),
                        true,
                        Some(matches.args.keys().map(|k| *k).collect()));
                }
            }
            return None;
        }
        // Short flag or opt
        let arg_c = arg.chars().nth(0).unwrap();

        // Ensure the arg in question isn't a help or version flag
        self.check_for_help_and_version(arg_c);

        // Check for a matching flag, and return none if found
        if self.parse_single_short_flag(matches, arg_c) { return None; }

        // Check for matching short in options, and return the name
        // (only ones with shorts, of course)
        if let Some(v) = self.opts.values()
                             .filter(|&v| v.short.is_some())
                             .filter(|&v| v.short.unwrap() == arg_c).nth(0) {
            // Ensure this option isn't on the master mutually excludes list
            if self.blacklist.contains(v.name) {
                matches.args.remove(v.name);
                self.report_error(format!("The argument '{}' cannot be used with {}",
                            Format::Warning(format!("-{}", arg)),
                        match self.blacklisted_from(v.name, matches) {
                            Some(name) => format!("'{}'", Format::Warning(name)),
                            None       => "one or more of the other specified arguments".to_owned()
                        }),
                    true,
                    Some(matches.args.keys().map(|k| *k).collect()));
            }

            if matches.args.contains_key(v.name) {
                if !v.multiple {
                    self.report_error(format!("The argument '{}' was supplied more than once, but \
                        does not support multiple values",
                            Format::Warning(format!("-{}", arg))),
                        true,
                        Some(matches.args.keys().map(|k| *k).collect()));
                }
            } else {
                matches.args.insert(v.name, MatchedArg{
                    // occurrences will be incremented on getting a value
                    occurrences: 0,
                    values: Some(BTreeMap::new())
                });
            }
            if let Some(ref bl) = v.blacklist {
                for name in bl {
                    self.blacklist.insert(name);
                    self.required.remove(name);
                }
            }

            self.required.remove(v.name);

            if let Some(ref reqs) = v.requires {
                // Add all required args which aren't already found in matches to the
                // final required list
                for n in reqs {
                    if matches.args.contains_key(n) { continue; }

                    self.required.insert(n);
                }
            }

            parse_group_reqs!(self, v);

            return Some(v.name)
        }

        // Didn't match a flag or option, must be invalid
        self.report_error(format!("The argument '{}' isn't valid",
                            Format::Warning(format!("-{}", arg_c))),
            true,
            Some(matches.args.keys().map(|k| *k).collect()));

        unreachable!();
    }

    fn parse_single_short_flag(&mut self, matches: &mut ArgMatches<'ar, 'ar>, arg: char) -> bool {
        for v in self.flags.values()
                           .filter(|&v| v.short.is_some())
                           .filter(|&v| v.short.unwrap() == arg) {
            // Ensure this flag isn't on the mutually excludes list
            if self.blacklist.contains(v.name) {
                matches.args.remove(v.name);
                self.report_error(format!("The argument '{}' cannot be used {}",
                            Format::Warning(format!("-{}", arg)),
                        match self.blacklisted_from(v.name, matches) {
                            Some(name) => format!("'{}'", Format::Warning(name)),
                            None       => "with one or more of the other specified \
                                arguments".to_owned()
                        }),
                    true,
                    Some(matches.args.keys().map(|k| *k).collect()));
            }

            // Make sure this isn't one being added multiple times if it doesn't suppor it
            if matches.args.contains_key(v.name) && !v.multiple {
                self.report_error(format!("The argument '{}' was supplied more than once, but does \
                        not support multiple values",
                            Format::Warning(format!("-{}", arg))),
                    true,
                    Some(matches.args.keys().map(|k| *k).collect()));
            }

            let mut done = false;
            if let Some(ref mut f) = matches.args.get_mut(v.name) {
                done = true;
                f.occurrences = if v.multiple { f.occurrences + 1 } else { 1 };
            }
            if !done {
                matches.args.insert(v.name, MatchedArg{
                    // name: v.name.to_owned(),
                    occurrences: 1,
                    values: None
                });
            }

            // If this flag was requierd, remove it
            // .. even though Flags shouldn't be required
            self.required.remove(v.name);

            // Add all of this flags "mutually excludes" list to the master list
            if let Some(ref bl) = v.blacklist {
                for name in bl {
                    self.blacklist.insert(name);
                    self.required.remove(name);
                }
            }

            // Add all required args which aren't already found in matches to the master list
            if let Some(ref reqs) = v.requires {
                for n in reqs {
                    if matches.args.contains_key(n) { continue; }

                    self.required.insert(n);
                }
            }

            parse_group_reqs!(self, v);

            return true;
        }
        false
    }

    fn validate_blacklist(&self, matches: &mut ArgMatches<'ar, 'ar>) {
        for name in self.blacklist.iter() {
            if matches.args.contains_key(name) {
                matches.args.remove(name);
                self.report_error(format!("The argument '{}' cannot be used with {}",
                    if let Some(ref flag) = self.flags.get(name) {
                        format!("{}", Format::Warning(flag.to_string()))
                    } else if let Some(ref opt) = self.opts.get(name) {
                        format!("{}", Format::Warning(opt.to_string()))
                    } else {
                        match self.positionals_idx.values().filter(|p| p.name == *name).next() {
                            Some(pos) => format!("{}", Format::Warning(pos.to_string())),
                            None      => format!("\"{}\"", Format::Warning(name))
                        }
                    }, match self.blacklisted_from(name, matches) {
                        Some(name) => format!("'{}'", Format::Warning(name)),
                        None       => "one or more of the other specified arguments".to_owned()
                    }),
                true,
                Some(matches.args.keys().map(|k| *k).collect()));
            } else if self.groups.contains_key(name) {
                for n in self.get_group_members_names(name) {
                    if matches.args.contains_key(n) {
                        matches.args.remove(n);
                        self.report_error(format!("The argument '{}' cannot be used with one or \
                                more of the other specified arguments",
                                if let Some(ref flag) = self.flags.get(n) {
                                    format!("{}", Format::Warning(flag.to_string()))
                                } else if let Some(ref opt) = self.opts.get(n) {
                                    format!("{}", Format::Warning(opt.to_string()))
                                } else {
                                    match self.positionals_idx.values()
                                                              .filter(|p| p.name == *name)
                                                              .next() {
                                        Some(pos) => format!("{}", Format::Warning(pos.to_string())),
                                        None      => format!("\"{}\"", Format::Warning(n))
                                    }
                                }),
                            true,
                            Some(matches.args.keys().map(|k| *k).collect()));
                    }
                }
            }
        }
    }

    fn validate_num_args(&self, matches: &mut ArgMatches<'ar, 'ar>) {
        for (name, ma) in matches.args.iter() {
            if let Some(ref vals) = ma.values {
                if let Some(f) = self.opts.get(name) {
                    if let Some(num) = f.num_vals {
                        let should_err = if f.multiple {
                            ((vals.len() as u8) % num) != 0
                        } else {
                            num != (vals.len() as u8)
                        };
                        if should_err {
                            self.report_error(format!("The argument '{}' requires {} values, \
                                    but {} w{} provided",
                                    Format::Warning(f.to_string()),
                                    Format::Good(num.to_string()),
                                    Format::Error(if f.multiple {
                                        (vals.len() % num as usize).to_string()
                                    } else {
                                        vals.len().to_string()
                                    }),
                                    if vals.len() == 1 ||
                                        ( f.multiple &&
                                            ( vals.len() % num as usize) == 1) {"as"}else{"ere"}),
                                true,
                                Some(matches.args.keys().map(|k| *k).collect()));
                        }
                    }
                    if let Some(num) = f.max_vals {
                        if (vals.len() as u8) > num {
                            self.report_error(format!("The argument '{}' requires no more than {} \
                                    values, but {} w{} provided",
                                    Format::Warning(f.to_string()),
                                    Format::Good(num.to_string()),
                                    Format::Error(vals.len().to_string()),
                                    if vals.len() == 1 {"as"}else{"ere"}),
                                true,
                                Some(matches.args.keys().map(|k| *k).collect()));
                        }
                    }
                    if let Some(num) = f.min_vals {
                        if (vals.len() as u8) < num {
                            self.report_error(format!("The argument '{}' requires at least {} \
                                    values, but {} w{} provided",
                                    Format::Warning(f.to_string()),
                                    Format::Good(num.to_string()),
                                    Format::Error(vals.len().to_string()),
                                    if vals.len() == 1 {"as"}else{"ere"}),
                                true,
                                Some(matches.args.keys().map(|k| *k).collect()));
                        }
                    }
                } else if let Some(f) = self.positionals_idx.get(
                    self.positionals_name.get(name).unwrap()) {
                    if let Some(num) = f.num_vals {
                        if num != vals.len() as u8 {
                            self.report_error(format!("The argument '{}' requires {} values, \
                                    but {} w{} provided",
                                    Format::Warning(f.to_string()),
                                    Format::Good(num.to_string()),
                                    Format::Error(vals.len().to_string()),
                                    if vals.len() == 1 {"as"}else{"ere"}),
                                true,
                                Some(matches.args.keys().map(|k| *k).collect()));
                        }
                    }
                    if let Some(num) = f.max_vals {
                        if num > vals.len() as u8 {
                            self.report_error(format!("The argument '{}' requires no more than {} \
                                    values, but {} w{} provided",
                                    Format::Warning(f.to_string()),
                                    Format::Good(num.to_string()),
                                    Format::Error(vals.len().to_string()),
                                    if vals.len() == 1 {"as"}else{"ere"}),
                                true,
                                Some(matches.args.keys().map(|k| *k).collect()));
                        }
                    }
                    if let Some(num) = f.min_vals {
                        if num < vals.len() as u8 {
                            self.report_error(format!("The argument '{}' requires at least {} \
                                    values, but {} w{} provided",
                                    Format::Warning(f.to_string()),
                                    Format::Good(num.to_string()),
                                    Format::Error(vals.len().to_string()),
                                    if vals.len() == 1 {"as"}else{"ere"}),
                                true,
                                Some(matches.args.keys().map(|k| *k).collect()));
                        }
                    }
                }
            }
        }
    }

    fn validate_required(&self, matches: &ArgMatches<'ar, 'ar>) -> bool{
        for name in self.required.iter() {
            validate_reqs!(self, flags, matches, name);

            validate_reqs!(self, opts, matches, name);

            // because positions use different keys, we dont use the macro
            match self.positionals_idx.values().filter(|ref p| &p.name == name).next() {
                Some(p) =>{
                    if let Some(ref bl) = p.blacklist {
                        for n in bl.iter() {
                            if matches.args.contains_key(n) {
                                return false
                            } else if self.groups.contains_key(n) {
                                let grp = self.groups.get(n).unwrap();
                                for an in grp.args.iter() {
                                    if matches.args.contains_key(an) {
                                        return false
                                    }
                                }
                            }
                        }
                    }
                },
                None    =>(),
            }
        }
        true
    }

    /// Returns a suffix that can be empty, or is the standard 'did you mean phrase
    fn did_you_mean_suffix<'z, T, I>(arg: &str, values: I, style: DidYouMeanMessageStyle)
                                                     -> (String, Option<&'z str>)
                                                        where       T: AsRef<str> + 'z,
                                                                    I: IntoIterator<Item=&'z T> {
        match did_you_mean(arg, values) {
                Some(candidate) => {
                    let mut suffix = "\n\tDid you mean ".to_string();
                    match style {
                        DidYouMeanMessageStyle::LongFlag => suffix.push_str(&Format::Good("--").to_string()[..]),
                        DidYouMeanMessageStyle::EnumValue => suffix.push('\''),
                    }
                    suffix.push_str(&Format::Good(candidate).to_string()[..]);
                    if let DidYouMeanMessageStyle::EnumValue = style {
                        suffix.push('\'');
                    }
                    suffix.push_str(" ?");
                    (suffix, Some(candidate))
                },
                None => (String::new(), None),
        }
    }
}