rsclp 0.1.6

A lib crate to parse proccess arguments
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
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
use std::any::{Any, TypeId};
use std::env::{Args, ArgsOs};
use std::fmt::{Debug, Display, Result};
use std::io::Write;
use std::io::Result as IOResult;
use std::result::Result as StdResult;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::str::FromStr;
use std::string::ToString;

/// Error message indexes
/// if you want to set error message in your language follow the indexes order
/// see CommandLineParser::new associated function example
pub const OPTION_ALREADY_EXISTS_ERROR_IDX: usize = 0usize;
pub const SHORT_OPTION_ALREADY_EXISTS_ERROR_IDX: usize = OPTION_ALREADY_EXISTS_ERROR_IDX + 1usize;
pub const LONG_OPTION_ALREADY_EXISTS_ERROR_IDX: usize = SHORT_OPTION_ALREADY_EXISTS_ERROR_IDX + 1usize;
pub const OPTION_IS_NOT_SET_ERROR_IDX: usize = LONG_OPTION_ALREADY_EXISTS_ERROR_IDX + 1usize;
pub const OPTION_IS_NOT_OF_REQUIRED_TYPE_ERROR_IDX: usize = OPTION_IS_NOT_SET_ERROR_IDX + 1usize;
pub const OPTION_NOT_FOUND_ERROR_IDX: usize = OPTION_IS_NOT_OF_REQUIRED_TYPE_ERROR_IDX + 1usize;
pub const OPTION_TYPE_UNDEFINED_ERROR_IDX: usize = OPTION_NOT_FOUND_ERROR_IDX + 1usize;
pub const BOOLEAN_OPTION_MISMATCH_VALUE_SET_ERROR_IDX: usize = OPTION_TYPE_UNDEFINED_ERROR_IDX + 1usize;
pub const INTEGER_OPTION_MISMATCH_VALUE_SET_ERROR_IDX: usize = BOOLEAN_OPTION_MISMATCH_VALUE_SET_ERROR_IDX + 1usize;
pub const FPOINT_OPTION_MISMATCH_VALUE_SET_ERROR_IDX: usize = INTEGER_OPTION_MISMATCH_VALUE_SET_ERROR_IDX + 1usize;
pub const MISSING_OPTION_ARGUMENT_ERROR_IDX: usize = FPOINT_OPTION_MISMATCH_VALUE_SET_ERROR_IDX + 1usize;
pub const MANDATORY_OPTION_HAS_NOT_SET_ERROR_IDX: usize = MISSING_OPTION_ARGUMENT_ERROR_IDX + 1usize;
pub const OPTION_IDENTIFIER_NOT_FOUND_ERROR_IDX: usize = MANDATORY_OPTION_HAS_NOT_SET_ERROR_IDX + 1usize;
pub const OPTION_ARGUMENT_ALREADY_ASSIGNED_ERROR_IDX: usize = OPTION_IDENTIFIER_NOT_FOUND_ERROR_IDX + 1usize;
pub const CMD_LINE_OPTION_ERROR_NUM: usize = OPTION_ARGUMENT_ALREADY_ASSIGNED_ERROR_IDX + 1usize;

// Error message in the Englih default language
const OPTION_ALREADY_EXISTS_ERROR: &str = "option already exists";
const SHORT_OPTION_ALREADY_EXISTS_ERROR: &str = "single character option already exists";
const LONG_OPTION_ALREADY_EXISTS_ERROR: &str = "long form option already exists";
const OPTION_IS_NOT_SET_ERROR: &str = "is not set";
const OPTION_IS_NOT_OF_REQUIRED_TYPE_ERROR: &str ="is not of the required type";
const OPTION_NOT_FOUND_ERROR: &str = "option not found";
const OPTION_TYPE_UNDEFINED_ERROR: &str = "Undefined command line option";
const BOOLEAN_OPTION_MISMATCH_VALUE_SET_ERROR: &str = "cannot set a boolean value to this option type";
const INTEGER_OPTION_MISMATCH_VALUE_SET_ERROR: &str = "cannot set an integer value to this option type";
const FPOINT_OPTION_MISMATCH_VALUE_SET_ERROR: &str = "cannot set a floating point value to this option type";
const MISSING_OPTION_ARGUMENT_ERROR: & str = "missing option argument"; 
const MANDATORY_OPTION_HAS_NOT_SET_ERROR: &str = "mandatory option has not been set";
const OPTION_IDENTIFIER_NOT_FOUND_ERROR: &str = "option identifier not found";
const OPTION_ARGUMENT_ALREADY_ASSIGNED_ERROR: &str =  "option argument already assigned";


// Short form not set value
const EMPTY_SHORT_FORM: char = ' ';

// Command line option type names
const COMMAND_LINE_OPTION_TYPE_UNDEFINED: &str = "UndefinedCommandLineOptionType";
const COMMAND_LINE_OPTION_TYPE_BOOLEAN: &str = "BooleanCommandLineOptionType";
const COMMAND_LINE_OPTION_TYPE_INTEGER: &str = "IntegerCommandLineOptionType";
const COMMAND_LINE_OPTION_TYPE_FPOINT: &str = "FPointCommandLineOptionType";
const COMMAND_LINE_OPTION_TYPE_STRING: &str = "StringCommandLineOptionType";

// Command line option assign tas
const OPTION_ASSIGN_TAG: &str = "=";

// Comman line option type enumeration
enum CommandLineOptionType {
    UNDEFINED(&'static str),
    BOOLEAN(&'static str),
    INTEGER(&'static str),
    FPOINT(&'static str),
    STRING(&'static str)
}

// Comman line option type enumeration implementation
impl CommandLineOptionType {
    // Associated function to create  default undefined
    // command line option
    fn new() -> Self {
        Self::UNDEFINED(COMMAND_LINE_OPTION_TYPE_UNDEFINED)
    }

    // method to return the string representation
    // of command line option type
    fn unwrap(&self) -> &'static str {
        match self {
            Self::UNDEFINED(value) => { *value }
            Self::BOOLEAN(value) => { *value }
            Self::INTEGER(value) => { *value }
            Self::FPOINT(value) => { *value }
            Self::STRING(value) => { *value }
        }
    }
}

// Display trait implementation 
impl Display for CommandLineOptionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
        write!(f, "{}", self.unwrap())
    }
}

// Fram<T> trait implementation to retrive a
// command line option type from a TypeId
impl From<TypeId> for CommandLineOptionType {
    fn from(type_id: TypeId) -> Self {
        let bool_type: bool = false;
        if bool_type.type_id() == type_id {
            return Self::BOOLEAN(COMMAND_LINE_OPTION_TYPE_BOOLEAN);
        }
        let i8_type = 0i8;
        if i8_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let u8_type = 0u8;
        if u8_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let i16_type = 0i16;
        if i16_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let u16_type = 0u16;
        if u16_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let i32_type = 0i32;
        if i32_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let u32_type = 0u32;
        if u32_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let i64_type = 0i64;
        if i64_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let u64_type = 0u64;
        if u64_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let i128_type = 0i128;
        if i128_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let u128_type = 0u128;
        if u128_type.type_id() == type_id {
            return Self::INTEGER(COMMAND_LINE_OPTION_TYPE_INTEGER);
        }
        let f32_type = 0.0f32;
        if f32_type.type_id() == type_id {
            return Self::FPOINT(COMMAND_LINE_OPTION_TYPE_FPOINT);
        }
        let f64_type = 0.0f64;
        if f64_type.type_id() == type_id {
            return Self::FPOINT(COMMAND_LINE_OPTION_TYPE_FPOINT);
        }
        let str_type: &str = "";
        if str_type.type_id() == type_id {
            return Self::STRING(COMMAND_LINE_OPTION_TYPE_STRING);
        }
        let string_type: String = String::new();
        if string_type.type_id() == type_id {
            return Self::STRING(COMMAND_LINE_OPTION_TYPE_STRING);
        }
        Self::UNDEFINED(COMMAND_LINE_OPTION_TYPE_UNDEFINED)
    }
}

// PartialEq traint implementation to compare
// 2 command line option types
impl PartialEq for CommandLineOptionType {
    fn eq(&self, other: &Self) -> bool {
        self.unwrap() == other.unwrap()
    }
}

// Command line option struct
// used by CommandLineParser to store
// command line options
struct CommandLineOption {
    short_form_option: char,
    long_form_option: String,
    mandatory: bool,
    arg_text: String,
    help_text: String,
    values: Vec<String>,
    typ: CommandLineOptionType,
}

// CommandLineOption implemenetation
impl CommandLineOption {
    // Associated function to create
    // a command line option identified by
    // a single charactrer only (i.g -h)
    fn new_short_only(short_form_option: char,
                        mandatory: bool,
                        arg_text: &str,
                        help_text: &str) -> Self {
        Self {
            short_form_option,
            long_form_option: String::from(""),
            mandatory,
            arg_text: String::from(arg_text),
            help_text: help_text.to_string(),
            values: vec![],
            typ: CommandLineOptionType::new()
        }
    }

    // Associated function to create
    // a command line option identified by
    // a string only (i.g --help)
    fn new_long_only(long_form_option: &str,
                        mandatory: bool,
                        arg_text: &str,
                        help_text: &str) -> Self {
        Self {
            short_form_option:  EMPTY_SHORT_FORM,
            long_form_option:  String::from(long_form_option),
            mandatory,
            arg_text: String::from(arg_text),
            help_text: help_text.to_string(),
            values: vec![],
            typ: CommandLineOptionType::new()
        }
    }

    // Associated function to create
    // a command line option identified by
    // a single character or a string (i.g -h/--help)
    fn new(short_form_option: char,
            long_form_option: &str,
            mandatory: bool,
            arg_text: &str,
            help_text: &str) -> Self {
        Self {
            short_form_option,
            long_form_option:  String::from(long_form_option),
            mandatory,
            arg_text: String::from(arg_text),
            help_text: help_text.to_string(),
            values: vec![],
            typ: CommandLineOptionType::new()
        }
    }

    // Method to calculate a unique hash value
    // that identified the stored command line option
    fn calculate_hash(&self) -> u64 {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        hasher.finish()
    }

    // Method to retrive the string representation of the 
    // command line option flags
    // i.g -h/--help or --config or -v
    fn get_flags(&self) -> String {
        let mut flags_repr =  "".to_string();
        if EMPTY_SHORT_FORM != self.short_form_option {
            flags_repr = format!("-{}", self.short_form_option);
        }
        if !self.long_form_option.is_empty() {
            if !flags_repr.is_empty()  {
                flags_repr.push('/');
            }
            flags_repr.push_str(&format!("--{}", self.long_form_option));
        }
        flags_repr
    }

    // Method to retrive command line option type
    // as a text
    fn get_type_name(&self) -> String {
        self.typ.unwrap().to_string()
    }

    // Method to retrive command line option help text
    // argument passed to this method are used
    // to allign the help text
    // * `max_flags_len` - longest flags text calculated by commnad line parser
    // * `max_arg_text_len` - longest agument text calculate by command line parser
    fn help_text(&self, max_flags_len: usize, max_arg_text_len: usize) -> String {
        let mut arg_text_len = self.arg_text.len();
        
        if 0 != arg_text_len {
            let fill_len = max_arg_text_len - arg_text_len;
            let fill = std::iter::repeat(" ").take(fill_len).collect::<String>();
            format!("{:>max_flags_len$} <{:arg_text_len$}>{} {}.\n", 
                self.get_flags(), self.arg_text, fill, self.help_text)
        } else {
            arg_text_len = max_arg_text_len + 2;
            let arg_text = std::iter::repeat(" ").take(arg_text_len).collect::<String>();
            format!("{:>max_flags_len$} {:arg_text_len$} {}.\n", 
                self.get_flags(), arg_text, self.help_text)
        }
    }

    // Method that returns if a command line option
    // has be set during the parsing phase
    fn is_set(&self) -> bool {
        0 != self.values.len()
    }

    // Method taht return the first of all set values
    // ore None if no values has been set 
    fn get_value(&self) -> Option<&String> {
        if self.values.len() > 0 {
            return self.values.get(0)
        }

        None
    }

    // Method to add a value to a command line option
    // It returns Ok(()) in case passed value respect option type
    // otherwhise it returns the command line option error index
    // * `value` - Value to be added
    fn add_value(&mut self, value: &str) -> StdResult<(), usize> {
        let type_name = self.typ.unwrap();
        if type_name != COMMAND_LINE_OPTION_TYPE_UNDEFINED {
            if type_name != COMMAND_LINE_OPTION_TYPE_STRING {
                if let Ok(_) = value.parse::<bool>() {
                    if type_name != COMMAND_LINE_OPTION_TYPE_BOOLEAN {
                        return Err(BOOLEAN_OPTION_MISMATCH_VALUE_SET_ERROR_IDX);    
                    }
                }  else if let Ok(_) = value.parse::<i128>() {
                    if type_name != COMMAND_LINE_OPTION_TYPE_INTEGER {
                        return Err(INTEGER_OPTION_MISMATCH_VALUE_SET_ERROR_IDX);
                    }
                } else if let Ok(_) = value.parse::<f64>() {
                    if type_name != COMMAND_LINE_OPTION_TYPE_FPOINT {
                        return Err(FPOINT_OPTION_MISMATCH_VALUE_SET_ERROR_IDX);
                    }
                }
            }
        } else {
            return Err(OPTION_TYPE_UNDEFINED_ERROR_IDX);
        }


        self.values.push(value.to_string());
        Ok(())
    }
    fn get_values(&self) -> &Vec<String> {
       &self.values
    }
}


// Hash trait implementation of command line option
// hash value is based on short & long form flags  
impl Hash for CommandLineOption {
    fn hash<H: Hasher>(&self, state: &mut H) {

        if EMPTY_SHORT_FORM != self.short_form_option {
            self.short_form_option.hash(state)
        }
        if !self.long_form_option.is_empty() {
            self.long_form_option.hash(state);
        }
    }
}

// PartialEq trait implementation of command line option
// used to compare 2 command line options
impl PartialEq for CommandLineOption {
    fn eq(&self, other: &Self) -> bool {
        (self.typ == other.typ) &&
        (self.short_form_option == other.short_form_option) &&
        (self.long_form_option == other.long_form_option) 
    }
}



/// Command line option error representation.
/// This struct is returned by all command line parser methods
/// in case of error you can print its representation simply
/// using {} placeholder in the rust macros
///
/// # Examples
/// ```
/// use rsclp::CommandLineParser;
/// 
/// fn main() {
///     let mut clp = CommandLineParser::new(None);
///     let config_option = clp.add_string_option('c', "config", true, "file path", "configuration file path").unwrap();
///     match clp.parse_args(std::env::args()) {
///         Ok(()) => {
///             let config_file: String = clp.get_value(&config_option).unwrap_or_else(|e| {
///                 eprintln!("{}", e);
///                 String::from("") });
///             println!("config_file: {}", config_file);
///         },
///         Err(parse_error) => {
///             eprintln!("{}", parse_error);
///             clp.show_help();
///         }
///     }     
/// }
/// ```
#[derive(PartialEq)]
pub struct CommandLineParserError {
    flags: String,
    typ: String,
    error: String
}

impl Display for CommandLineParserError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
        write!(f, "{} {}: {}", self.typ, self.flags, self.error)
    }
}

impl Debug for CommandLineParserError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
        write!(f, "{} {}: {}", self.typ, self.flags, self.error)
    }
}


/// Command line parser parsing mode
/// DefaultParsingMode mode means that - - option is treated
/// like all other option
/// PositionalArgumentsMode menas that all the options
/// after - - are positional argument that user can pass
/// to a process launched by your application
#[derive(PartialEq)]
pub enum ParsingMode {
    DefaultParsingMode,
    PositionalArgumentsMode,
}

impl ParsingMode {

    /// This method returns a textual representation
    /// of parsing mode
    fn unwrap(&self) -> &str {
        match self {
            ParsingMode::DefaultParsingMode => {
                "DefaultParsingMode"
            },
            ParsingMode::PositionalArgumentsMode => {
                "PositionalArgumentsMode"
            }
        }
    }
}

/// Display trait of ParsingMode
impl Display for ParsingMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
        write!(f, "{}", self.unwrap())
    }
}

/// Command line parser is able to parse process arguments.
/// Arguments could be of two types, single character argument (i.g -c)
/// or long text argument (i.g. --config-file)
/// Process argument could be of the following type:
/// Boolean - classic is -h to show process help, 
/// Integer - an integer value for example verbosity level --verbose 5, 
/// Floating point - a floating point numer --ratio=123.25, 
/// String - a text argument, classic configuration file path --config-file app.properties.
/// Integer, Floating point and String option has a mandatory angument
/// while Boolean option does not require an argument.
/// Argument can be passed in two ways: --config-file=config/app.properties for example
/// or --config-file config/app.properties the same example is valid for single character options
/// for example -c=onfig/app.properties or -c onfig/app.properties.
/// Single character option can be pass grouped together (i.g -xvz).
/// Be carful if a single character argument needs an argument you have to pass it or 
/// adding = and the value (i.g. -xvzf=file_to_compress.tar.gz) or as next process argument
/// (i.g -xvzf file_to_compress.tar.gz).
/// No more than a single character option with a mandatory argument can be grouped.
pub struct CommandLineParser {
    program_name: String,
    options: Vec<CommandLineOption>,
    errors_list: Vec<String>,
    remaining_args: Vec<String>,
    positional_args: Vec<String>,
    parsing_mode: ParsingMode
}


impl CommandLineParser {
    /// Associated funtion to create a CommandLineParser
    /// * `errors_list` - Optional list of errors (None means default Elnglish language)
    /// # Examples
    /// ```
    /// use rsclp::{CommandLineParser, CommandLineParserError, CMD_LINE_OPTION_ERROR_NUM};
    /// 
    /// fn main()  {
    ///     let it_error_list: [&str; CMD_LINE_OPTION_ERROR_NUM] = [
    ///        "l'opzione già esiste",
    ///         "l'opzione a singolo charattere già esiste",
    ///         "l'opzione a formato lungo già esiste",
    ///         "non è valorizzata",
    ///         "non è del tipo richiesto",
    ///         "opzione non trovata",
    ///         "opzione di linea di comando non definita",
    ///         "valore booleano non valido per questo tipo di opzione",
    ///         "valore intero non valido per questo tipo di opzione",
    ///         "valore a virgola mobile non valido per questo tipo di opzione",
    ///         "manca l'argomento per questa opzione", 
    ///         "opzione obbligatoria non valorizzata",
    ///         "identificativo dell'opzione non trovato",
    ///         "argomento dell'opzione fia assegnato"
    ///     ];
    ///     let mut clp = CommandLineParser::new(Some(it_error_list));
    ///     
    ///     
    /// }
    /// ```
    /// 
    pub fn new(errors_list: Option<[&str; CMD_LINE_OPTION_ERROR_NUM]>) -> Self {
        let mut result = Self {
            program_name: String::new(),
            options: vec![],
            errors_list: vec![],
            remaining_args: vec![],
            positional_args: vec![],
            parsing_mode: ParsingMode::DefaultParsingMode
        };
        match errors_list {
            Some(list) => {
                for element in list {
                    result.errors_list.push(element.to_string());
                }
            },
            None => {
                result.errors_list.push(OPTION_ALREADY_EXISTS_ERROR.to_string());
                result.errors_list.push(SHORT_OPTION_ALREADY_EXISTS_ERROR.to_string());
                result.errors_list.push(LONG_OPTION_ALREADY_EXISTS_ERROR.to_string());
                result.errors_list.push(OPTION_IS_NOT_SET_ERROR.to_string());
                result.errors_list.push(OPTION_IS_NOT_OF_REQUIRED_TYPE_ERROR.to_string());
                result.errors_list.push(OPTION_NOT_FOUND_ERROR.to_string());
                result.errors_list.push(OPTION_TYPE_UNDEFINED_ERROR.to_string());
                result.errors_list.push(BOOLEAN_OPTION_MISMATCH_VALUE_SET_ERROR.to_string());
                result.errors_list.push(INTEGER_OPTION_MISMATCH_VALUE_SET_ERROR.to_string());
                result.errors_list.push(FPOINT_OPTION_MISMATCH_VALUE_SET_ERROR.to_string());
                result.errors_list.push(MISSING_OPTION_ARGUMENT_ERROR.to_string());
                result.errors_list.push(MANDATORY_OPTION_HAS_NOT_SET_ERROR.to_string());
                result.errors_list.push(OPTION_IDENTIFIER_NOT_FOUND_ERROR.to_string());
                result.errors_list.push(OPTION_ARGUMENT_ALREADY_ASSIGNED_ERROR.to_string());
            }
        }
        result
    }

    /// Method to add a boolean command line option identified
    /// by a single character only
    /// * `short_form_option` - single character option flag
    /// * `mandatory` - boolean value to set if command line option is mandatory or not
    /// * `help_text` - command line option halt text 
    pub fn add_short_boolean_option(&mut self,
        short_form_option: char,
        mandatory: bool,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {

        let mut option = CommandLineOption::new_short_only(short_form_option, 
                                                                                mandatory, 
                                                                                "", 
                                                                                help_text);
        let bool_type = false;
        option.typ = CommandLineOptionType::from(bool_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a boolean command line option identified
    /// by a long text only
    /// * `long_form_option` - long text option flag
    /// * `mandatory` - boolean value to set if command line option is mandatory or not
    /// * `help_text` - command line option halt text 
    pub fn add_long_boolean_option(&mut self,
        long_form_option: &str,
        mandatory: bool,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new_long_only(long_form_option, 
                                                                            mandatory, 
                                                                            "", 
                                                                            help_text);
        let bool_type = false;
        option.typ = CommandLineOptionType::from(bool_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a boolean command line option identified
    /// by a single character and long form text
    /// * `short_form_option` - single character option flag
    /// * `long_form_option` - long text option flag
    /// * `mandatory` - boolean value to set if command line option is mandatory or not
    /// * `help_text` - command line option halt text 
    pub fn add_boolean_option(&mut self,
        short_form_option: char,
        long_form_option: &str,
        mandatory: bool,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new(short_form_option,
                                                                    long_form_option, 
                                                                    mandatory, 
                                                                    "", 
                                                                    help_text);
        let bool_type = false;
        option.typ = CommandLineOptionType::from(bool_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add an integer command line option identified
    /// by a single character only
    /// * `short_form_option` - single character option flag
    /// * `mandatory` - boolean value to set if command line option is mandatory or not
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_short_integer_option(&mut self,
        short_form_option: char,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {

        let mut option = CommandLineOption::new_short_only(short_form_option, 
                                                                                mandatory, 
                                                                                arg_text, 
                                                                                help_text);
        let integer_type = 0i128;
        option.typ = CommandLineOptionType::from(integer_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }
   
    /// Method to add an integer command line option identified
    /// by a long text only
    /// * `long_form_option` - long text option flag
    /// * `mandatory` - boolean value to set if command line option is mandatory or not
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_long_integer_option(&mut self,
        long_form_option: &str,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new_long_only(long_form_option, 
                                                                                mandatory, 
                                                                                arg_text, 
                                                                                help_text);
        let integer_type = 0i128;
        option.typ = CommandLineOptionType::from(integer_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add an integer command line option identified
    /// by a single character and long form text
    /// * `short_form_option` - single character option flag
    /// * `long_form_option` - long text option flag
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_integer_option(&mut self,
        short_form_option: char,
        long_form_option: &str,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new(short_form_option,
                                                                    long_form_option, 
                                                                    mandatory, 
                                                                    arg_text, 
                                                                    help_text);
        let integer_type = 0i128;
        option.typ = CommandLineOptionType::from(integer_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a floating point command line option identified
    /// by a single character only
    /// * `short_form_option` - single character option flag
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_short_fpoint_option(&mut self,
        short_form_option: char,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {

        let mut option = CommandLineOption::new_short_only(short_form_option,
                                                                                mandatory, 
                                                                                arg_text, 
                                                                                help_text);
        let fpoint_type = 0.0f64;
        option.typ = CommandLineOptionType::from(fpoint_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a floating point command line option identified
    /// by a long text only
    /// * `long_form_option` - long text option flag
    /// * `mandatory` - boolean value to set if command line option is mandatory or not
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_long_fpoint_option(&mut self,
        long_form_option: &str,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new_long_only(long_form_option, 
                                                                                mandatory, 
                                                                                arg_text, 
                                                                                help_text);
        let fpoint_type = 0.0f64;
        option.typ = CommandLineOptionType::from(fpoint_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a floating point command line option identified
    /// by a single character and long form text
    /// * `short_form_option` - single character option flag
    /// * `long_form_option` - long text option flag
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_fpoint_option(&mut self,
        short_form_option: char,
        long_form_option: &str,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new(short_form_option,
                                                                    long_form_option, 
                                                                    mandatory, 
                                                                    arg_text, 
                                                                    help_text);
        let fpoint_type = 0.0f64;
        option.typ = CommandLineOptionType::from(fpoint_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a text command line option identified
    /// by a single character only
    /// * `short_form_option` - single character option flag
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_short_string_option(&mut self,
        short_form_option: char,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {

        let mut option = CommandLineOption::new_short_only(short_form_option,
                                                                                mandatory, 
                                                                                arg_text, 
                                                                                help_text);
        let str_type = "";
        option.typ = CommandLineOptionType::from(str_type.type_id());

        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a text command line option identified
    /// by a long text only
    /// * `long_form_option` - long text option flag
    /// * `mandatory` - boolean value to set if command line option is mandatory or not
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_long_string_option(&mut self,
        long_form_option: &str,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new_long_only(long_form_option, 
                                                                                mandatory, 
                                                                                arg_text, 
                                                                                help_text);
        let str_type = "";
        option.typ = CommandLineOptionType::from(str_type.type_id());
    
        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }

    /// Method to add a text command line option identified
    /// by a single character and long form text
    /// * `short_form_option` - single character option flag
    /// * `long_form_option` - long text option flag
    /// * `arg_text` - command line option argument text to explain argument in help text 
    /// * `help_text` - command line option halt text 
    pub fn add_string_option(&mut self,
        short_form_option: char,
        long_form_option: &str,
        mandatory: bool,
        arg_text: &str,
        help_text: &str) -> StdResult<u64, CommandLineParserError> {
        let mut option = CommandLineOption::new(short_form_option,
                                                                    long_form_option, 
                                                                    mandatory, 
                                                                    arg_text, 
                                                                    help_text);
        let str_type = "";
        option.typ = CommandLineOptionType::from(str_type.type_id());
    
        if let StdResult::Err(error) = self.check_option_already_exists(&option) {
            return StdResult::Err(error);
        }

        let option_hash: u64 = option.calculate_hash();
        self.options.push(option);

        Ok(option_hash)
    }


    /// Method to add the classic help command line option
    /// -h & --help
    /// * `help_text` - command line option halt text 
    pub fn add_help_option(&mut self, help_text: &str) -> StdResult<u64, CommandLineParserError> {
        self.add_boolean_option('h', 
                                "help", 
                                false, help_text)
    }

    /// Method to add the classic version command line option
    /// -v & --version
    /// * `help_text` - command line option halt text 
    pub fn add_version_option(&mut self, help_text: &str) -> StdResult<u64, CommandLineParserError> {
        self.add_boolean_option('v', 
                                "version", 
                                false, help_text)
    }

    /// Method to check if the parser found and set a command line option
    /// during command line parsing phase
    /// * `option_hash` - command line option identifier returned by
    /// an add_* method 
    pub fn is_set(&self, option_hash: &u64) ->  bool {
        let mut iter = self.options.iter();
        while let Some(option) = iter.next() {
            if *option_hash == option.calculate_hash() {
                return option.is_set();
            }
        }
        false
    }

    /// Generic method to get the value of a command line option
    /// (the first encountered) during command line parsing phase
    /// if command line option is not of the required type &ltT&gt or
    /// is not set error is returned
    /// * `option_hash` - command line option identifier returned by
    /// an add_* method 
    pub fn get_value<T: FromStr + 'static>(&self, option_hash: &u64) -> StdResult<T, CommandLineParserError> {
        let mut iter = self.options.iter();
        while let Some(option) = iter.next() {
            if *option_hash == option.calculate_hash() {
                let option_value = option.get_value();
                if let Some(value) = option_value {
                    if let Ok(parsed_value) = value.parse::<T>() {
                        let type_id = parsed_value.type_id();
                        let parsed_value_type = CommandLineOptionType::from(type_id);
                        if parsed_value_type == option.typ {
                            return Ok(parsed_value);
                        }
                    }
                    return Err(CommandLineParserError {
                        flags: option.get_flags(),
                        typ: option.get_type_name(),
                        error: self.errors_list[OPTION_IS_NOT_OF_REQUIRED_TYPE_ERROR_IDX].clone()
                    });
                }
                return Err(CommandLineParserError {
                    flags: option.get_flags(),
                    typ: option.get_type_name(),
                    error: self.errors_list[OPTION_IS_NOT_SET_ERROR_IDX].clone()
                });
            }
        }

        Err(CommandLineParserError {
            flags: String::new(),
            typ: COMMAND_LINE_OPTION_TYPE_UNDEFINED.to_string(),
            error: self.errors_list[OPTION_IDENTIFIER_NOT_FOUND_ERROR_IDX].clone()
        })
    } 

    /// Generic method to get all values of a command line option
    /// during command line parsing phase
    /// i.g if the option is -f/--input-file &ltfile name&gt input file to be merged 
    /// and the command line is -f file1.txt -o output.txt --input-file=file2.txt
    /// get_value returns a vector containing file1.txt & file2.txt 
    /// if command line option is not of the required type &ltT&gt or
    /// is not set error is returned
    /// * `option_hash` - command line option identifier returned by
    /// an add_* method 
    pub fn get_values<T: FromStr>(&self, option_hash: &u64) -> Option<Vec<T>> {
        let mut values: Vec<T> = vec![];
        for option in &self.options {
            if *option_hash == option.calculate_hash() {
                let option_values = option.get_values();
                for option_value in option_values {
                    if let Ok(parsed_value) = option_value.parse::<T>() {
                        values.push(parsed_value);
                    } else {
                        eprintln!("{} {} {}", option.get_type_name(), option.get_flags(), self.errors_list[OPTION_IS_NOT_OF_REQUIRED_TYPE_ERROR_IDX]); 
                        return None;
                    }
                }
            }
        }
        if values.len() > 0 {
            Some(values)
        } else {
            None
        }
    }

    /// Method to retrive the global command line help text
    pub fn get_help_text(&self) -> String {
        let mut result = String::new();
        if !self.program_name.is_empty() {
            result.push_str(&format!("{} [OPTIONS]:\n", self.program_name));
        }
        for option in &self.options {
            result.push_str(&format!("\t{}", option.help_text(self.get_max_flags_len(), 
                                                                    self.get_max_arg_text_len())));
        }

        result
    }

    /// Method to show on standard output 
    /// the global command line help text
    pub fn show_help(&self) {
        println!("{}", self.get_help_text());
    }

    /// Method to show on object that implements
    /// the std::io::Write trait 
    /// the global command line help text
    /// * `writer` - Write trait to show help text
    /// Writer::write_all is used
    pub fn show_help_on(&self, writer: &mut dyn Write) -> IOResult<()>{
        writer.write_all(self.get_help_text().as_bytes())
    }

    /// Method to retrieve all arguments not related to an option
    pub fn get_remaining_args(&self) -> &Vec<String> {
        &self.remaining_args
    }

    /// Method to retrieve all positionale arguments 
    /// if ParsingMode is DefaultParsingMode the returned
    /// vector is empty
    pub fn get_positional_args(&self) ->&Vec<String> {
        &self.positional_args
    }

    /// Method to set parsing mode
    /// * `parsing_mode` - command line parser parsing mode 
    pub fn set_parsing_mode(&mut self, parsing_mode: ParsingMode) {
        self.parsing_mode = parsing_mode;
    }

    /// Method to parse arguments of a process, 
    /// * `args_os` - an iterator over the arguments of a process, yielding an OsString value for each argument.
    pub fn parse_args_os(&mut self, args_os: ArgsOs) -> StdResult<(), CommandLineParserError> {
        let process_args: Vec<String> = args_os.map(|elem| { 
            elem.into_string().unwrap_or(String::new()) }).collect();
            self.parse(&process_args)
    }

    /// Method to parse arguments of a process, 
    /// * `args` - an iterator over the arguments of a process, yielding a String value for each argument.
    pub fn parse_args(&mut self, args: Args) -> StdResult<(), CommandLineParserError> {
        let process_args: Vec<String> = args.collect();
        self.parse(&process_args)
    }

    /// Method to process arguments of a process.
    /// As opposed to parse_args method, process controls the returned 
    /// value of the parse_args method and in case of error it shows
    /// the error and the help text on standar error 
    pub fn process(&mut self) {
        if let Err(parse_error) = self.parse_args(std::env::args()) {
            eprintln!("{}", parse_error);            
            let _ = self.show_help_on(&mut std::io::stderr().lock());
            std::process::exit(-1);
        }
    }

    /// Method to process arguments of a process.
    /// Performs the same functionality as the process method but using ArgOs
    pub fn process_os(&mut self) {
        if let Err(parse_error) = self.parse_args_os(std::env::args_os()) {
            eprintln!("{}", parse_error);            
            let _ = self.show_help_on(&mut std::io::stderr().lock());
            std::process::exit(-1);
        }
    }

    // Method that realizes the process arguments parsing
    // * `process_args` - process arguments as a strings vector reference
    fn parse(&mut self, process_args: &Vec<String>) -> StdResult<(), CommandLineParserError> {
        let mut args = process_args.clone();
        self.program_name = args[0].clone();
        args.remove(0);
        let mut dashdash: bool = false;
        let mut idx: usize = 0;
        let args_len: usize = args.len();
        while idx < args_len {
            let arg: &String = &args[idx];
            if !dashdash {
                if arg.starts_with("-") {
                    if arg.starts_with("--") {
                        self.parse_long_form_option(&args, arg, &mut idx, &mut dashdash)?;
                    } else {
                        self.parse_short_form_option(&args, arg, &mut idx)?;
                    }
                } else {
                    self.remaining_args.push(arg.clone());
                }
            } else {
                self.positional_args.push(arg.clone());
            }

            idx = idx + 1;
        }

        Ok(())
    }

    // Method to check if mandatory options
    // has at least a value set
    pub fn check_mandatory_options(&self) -> StdResult<(), CommandLineParserError> {
        let mut iter = self.options.iter();
        while let Some(option) = iter.next() {
            if option.mandatory &&  !option.is_set() {
                return Err(CommandLineParserError {
                    flags: option.get_flags(),
                    typ: option.get_type_name(),
                    error: self.errors_list[MANDATORY_OPTION_HAS_NOT_SET_ERROR_IDX].clone()
                });
            }
        }
        Ok(())
    }


    // Method to parse single character option
    // has at least a value set
    // * `args` - remaining process arguments as a strings vector reference
    // * `arg` - current process argument to be parsed
    // * `idx` - mutable  process argument index
    fn parse_short_form_option(&mut self, args: &Vec<String>, arg: &String, idx: &mut usize) -> StdResult<(), CommandLineParserError> {
        let args_len = args.len();
        let mut opt_arg = String::new();
        let pos = arg.find(OPTION_ASSIGN_TAG).unwrap_or_else(|| {usize::MAX});
        let mut opt = &arg[1..];
        if pos != usize::MAX {
            opt = &arg[1..pos];
            opt_arg.push_str(&arg[pos + 1..]);
        }

        let options: Vec<char> = opt.chars().collect();
  
        let mut opt_arg_assigned = false;
        let mut char_idx = 0usize;
        while char_idx < options.len() {

            let opt = format!("-{}", options[char_idx]);
            if let Some(option) = self.get_option_mut(&opt) {
                if option.typ.unwrap() == COMMAND_LINE_OPTION_TYPE_BOOLEAN {
                    let mut value = "true";
                    if !opt_arg.is_empty() {
                        if !opt_arg_assigned {
                            value = &opt_arg;
                            opt_arg_assigned = true;
                        } else {
                            return Err(CommandLineParserError {
                                flags: option.get_flags(),
                                typ: option.get_type_name(),
                                error: format!("{}", self.errors_list[OPTION_ARGUMENT_ALREADY_ASSIGNED_ERROR_IDX])
                            });    
                        }
                    }
                    if let Err(error_idx) = option.add_value(value) {
                        return Err(CommandLineParserError {
                            flags: option.get_flags(),
                            typ: option.get_type_name(),
                            error: format!("{}", self.errors_list[error_idx])
                        });
                    }
                } else {
                    if opt_arg.is_empty() {
                        if *idx < args_len - 1 {
                            *idx = *idx + 1;
                            opt_arg = args[*idx].clone();
                        }
                    }
                    if opt_arg.is_empty() {
                        return Err(CommandLineParserError {
                            flags: option.get_flags(),
                            typ: option.get_type_name(),
                            error: format!("{}", self.errors_list[MISSING_OPTION_ARGUMENT_ERROR_IDX])
                        });
                    }
                    if !opt_arg_assigned {
                        if let Err(error_idx) = option.add_value(&opt_arg) {
                            return Err(CommandLineParserError {
                                flags: option.get_flags(),
                                typ: option.get_type_name(),
                                error: format!("{}", self.errors_list[error_idx])
                            });
                        }
                        opt_arg_assigned = true;
                    } else {
                        return Err(CommandLineParserError {
                            flags: option.get_flags(),
                            typ: option.get_type_name(),
                            error: format!("{}", self.errors_list[OPTION_ARGUMENT_ALREADY_ASSIGNED_ERROR_IDX])
                        });
                    }
                }
            }    
            char_idx = char_idx + 1usize;
        }
        Ok(())
    }

    // Method to parse long text option
    // has at least a value set
    // * `args` - remaining process arguments as a strings vector reference
    // * `arg` - current process argument to be parsed
    // * `idx` - mutable  process argument index
    fn parse_long_form_option(&mut self, args: &Vec<String>, arg: &String, idx: &mut usize, dashdash: &mut bool) -> StdResult<(), CommandLineParserError> {
        let args_len = args.len();
        if arg == "--" {
            if self.parsing_mode == ParsingMode::PositionalArgumentsMode {
                *dashdash = true;
                return Ok(());
            }
        }
        
        let mut opt_and_arg: Vec<String> = arg.splitn(2, OPTION_ASSIGN_TAG)
                                            .map(|x| x.to_string()).collect();
        let opt = opt_and_arg.get_mut(0).unwrap();
        if opt == "--" {
            opt.push_str("--");
        }
        if let Some(option) = self.get_option_mut(opt) {
            let mut opt_arg;
            if opt_and_arg.len() < 2 {
                if option.get_type_name() == COMMAND_LINE_OPTION_TYPE_BOOLEAN {
                   opt_arg = "true";
                } else {
                    *idx = *idx + 1;
                    if *idx < args_len {
                       opt_arg = args[*idx].as_str();     
                    } else {
                        return Err(CommandLineParserError{
                            flags: option.get_flags(),
                            typ: option.get_type_name(),
                            error: format!("{}", self.errors_list[MISSING_OPTION_ARGUMENT_ERROR_IDX])
                        });                    
                    }
                }
                opt_and_arg.push(opt_arg.to_string());
            }
            opt_arg = opt_and_arg[1].as_str();
            if opt_arg.is_empty() {
                return Err(CommandLineParserError{
                    flags: option.get_flags(),
                    typ: option.get_type_name(),
                    error: format!("{}", self.errors_list[MISSING_OPTION_ARGUMENT_ERROR_IDX])
                });                    
            }
            if let Err(error_idx) = option.add_value(opt_arg) {
                return Err(CommandLineParserError {
                    flags: option.get_flags(),
                    typ: option.get_type_name(),
                    error: format!("{}", self.errors_list[error_idx])
                });
            }
        } else {
            return Err(CommandLineParserError{
                flags: arg.clone(),
                typ: COMMAND_LINE_OPTION_TYPE_UNDEFINED.to_string(),
                error: format!("{}", self.errors_list[OPTION_NOT_FOUND_ERROR_IDX])
            });                    
        }
        Ok(())
    }

    // Method to get a mutable reference to an existing option
    // It returns an option so in case of a not added option
    // the None value is returned
    // * `searched_option` - option flag text to be searched 
    fn get_option_mut(&mut self, searched_option: &String) -> Option<&mut CommandLineOption> {
        let is_long = searched_option.starts_with("--");
        let mut iter = self.options.iter_mut();
        while let Some(option) = iter.next() {
            if is_long {
                if !option.long_form_option.is_empty() {
                    if option.long_form_option == searched_option[2..] {
                        return Some(option)
                    }
                }
            } else {
                if EMPTY_SHORT_FORM != option.short_form_option {
                    if option.short_form_option == searched_option.chars().nth(1).unwrap_or_else(|| {' '}) {
                        return Some(option);
                    }
                }
            }
        }
        None
    }



    // Method to check if a command line option already exists
    // or there are other option with the same single character or long text
    // flags. It returns an Ok(()) in case option not exists already 
    // a CommandLineParserErr otherwise
    // * `option_to_check` - reference to an option to be checkd
    fn check_option_already_exists(&self, option_to_check: &CommandLineOption) -> StdResult<(), CommandLineParserError> {
        let mut iter  = self.options.iter();
        while let Some(option) = iter.next() {

            if option_to_check == option {
                return Err(CommandLineParserError{
                    flags: option.get_flags(),
                    typ: option.get_type_name(),
                    error: self.errors_list[OPTION_ALREADY_EXISTS_ERROR_IDX].to_string()
                });
            }

            if option_to_check.short_form_option == option.short_form_option {
                    return StdResult::Err(CommandLineParserError {
                        flags: option.get_flags(),
                        typ: option.get_type_name(),
                        error: self.errors_list[SHORT_OPTION_ALREADY_EXISTS_ERROR_IDX].to_string()
                    });
            }
            if option_to_check.long_form_option == option.long_form_option {
                return StdResult::Err(CommandLineParserError {
                    flags: option.get_flags(),
                    typ: option.get_type_name(),
                    error: self.errors_list[LONG_OPTION_ALREADY_EXISTS_ERROR_IDX].to_string()
                });
            }
        }
        Ok(())
    }

    // Method that returns the maximum length
    // of all options flags, used to allign the help text
    pub fn get_max_flags_len(&self) -> usize {
        let mut result = 0usize;
        for option in &self.options {
            let flags = option.get_flags();
            if result < flags.len() {
                result = flags.len();
            }
        }
        result
    }

    // Method that returns the maximum length
    // of all argument texts, used to allign the  help text
    fn get_max_arg_text_len(&self) -> usize {
        let mut result = 0usize;
        for option in &self.options {
            if result < option.arg_text.len() {
                result = option.arg_text.len();
            }
        }
        result
    }



}



#[cfg(test)]
mod tests {
    use super::*;


    #[test]
    fn short_form_option_already_exists() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let result = clp.add_short_boolean_option('v', false, "print-out application version");
        assert_eq!(String::from("BooleanCommandLineOptionType -v/--version: single character option already exists"), format!("{}", result.unwrap_err()));
    }

    #[test]
    fn long_form_option_already_exists() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let result = clp.add_long_boolean_option("help", false, "print-out help menu");
        assert_eq!(String::from("BooleanCommandLineOptionType -h/--help: long form option already exists"), format!("{}", result.unwrap_err()));
    }

    #[test]
    fn both_form_option_already_exists() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let mut result = clp.add_boolean_option('h', "help", false,  "show this help");
        assert_eq!(format!("{}", result.unwrap_err()), String::from("BooleanCommandLineOptionType -h/--help: option already exists"));
        result = clp.add_boolean_option('h', "help-all", false, "show this help");
        assert_eq!(format!("{}", result.unwrap_err()), String::from("BooleanCommandLineOptionType -h/--help: single character option already exists"));
        result = clp.add_boolean_option('H', "help", false, "show this help");
        assert_eq!(format!("{}", result.unwrap_err()), String::from("BooleanCommandLineOptionType -h/--help: long form option already exists"));
    }

    #[test]
    fn option_help_text() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let _ = clp.add_integer_option('V', "verbose", false, "log verbosity", "set verbosity text").unwrap();
        let help_text = "\
\t-v/--version                 print-out application version.
\t   -h/--help                 print-out help menu.
\t-V/--verbose <log verbosity> set verbosity text.\n";
        assert_eq!(clp.get_help_text(), help_text);
    }

    #[test]
    fn show_help() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let _ = clp.add_integer_option('V', "verbose", false, "level", "set log verbosity level").unwrap();
        let _ = clp.add_short_string_option('c', false, "file path", "configuration file path").unwrap();
        let args = vec!["test_show_help".to_string()];        
        assert_eq!(Ok(()), clp.parse(&args));
        clp.show_help();
        let _ = clp.show_help_on(&mut std::io::stderr().lock());
    }

    #[test]
    fn is_not_set() {
        let mut clp = CommandLineParser::new(None);
        let config_option = clp.add_string_option('c', "config", false, "file path", "application configuration file").unwrap();
        let args = vec!["is_not_set".to_string()];
        assert_eq!(Ok(()), clp.parse(&args));
        assert_eq!(false, clp.is_set(&config_option));
    }

    #[test]
    fn missing_madatory_option() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_string_option('c', "config", true, "file path", "application configuration file").unwrap();
        let args = vec!["is_not_set".to_string()];
        assert_eq!(Ok(()), clp.parse(&args));
        assert_eq!("StringCommandLineOptionType -c/--config: mandatory option has not been set".to_string(), 
            clp.check_mandatory_options().unwrap_err().to_string());
    }

    #[test]
    fn missing_option_argument1() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_string_option('c', "config", true, "file path", "application configuration file").unwrap();
        let args = vec!["missing_option_argument".to_string(),
            "-c".to_string()
        ];
        assert_eq!("StringCommandLineOptionType -c/--config: missing option argument".to_string(), 
                clp.parse(&args).unwrap_err().to_string());
    }

    #[test]
    fn missing_option_argument2() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_string_option('c', "config", true, "file path", "application configuration file").unwrap();
        let args = vec!["missing_option_argument".to_string(),
            "-c=".to_string()
        ];
        assert_eq!("StringCommandLineOptionType -c/--config: missing option argument".to_string(), 
                clp.parse(&args).unwrap_err().to_string());
    }

    #[test]
    fn missing_option_argument3() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_string_option('c', "config", true, "file path", "application configuration file").unwrap();
        let args = vec!["missing_option_argument".to_string(),
            "--config".to_string()
        ];
        assert_eq!("StringCommandLineOptionType -c/--config: missing option argument".to_string(), 
                clp.parse(&args).unwrap_err().to_string());
    }

    #[test]
    fn missing_option_argument4() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_string_option('c', "config", true, "file path", "application configuration file").unwrap();
        let args = vec!["missing_option_argument".to_string(),
            "--config=".to_string()
        ];
        assert_eq!("StringCommandLineOptionType -c/--config: missing option argument".to_string(), 
                clp.parse(&args).unwrap_err().to_string());
    }

    #[test]
    fn option_is_not_set() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let verbosity = clp.add_integer_option('V', "verbose", false, "level", "set log verbosity level").unwrap();
        let _ = clp.add_short_string_option('c', false, "file path", "configuration file path").unwrap();
        let args = vec!["test_sho_help".to_string()];        
        assert_eq!(Ok(()), clp.parse(&args));
        let value = clp.get_value::<i32>(&verbosity);
        assert_eq!("IntegerCommandLineOptionType -V/--verbose: is not set".to_string(), value.unwrap_err().to_string());
    }

    #[test]
    fn option_identifier_is_undefined() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let _ = clp.add_integer_option('V', "verbose", false, "level", "set log verbosity level").unwrap();
        let _ = clp.add_short_string_option('c', false, "file path", "configuration file path").unwrap();
        let args = vec!["test_sho_help".to_string()];        
        assert_eq!(Ok(()), clp.parse(&args));
        let value = clp.get_value::<i32>(&1u64);
        assert_eq!("UndefinedCommandLineOptionType : option identifier not found".to_string(), value.unwrap_err().to_string());
    }

    #[test]
    fn option_get_mismatch_value() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let verbosity = clp.add_integer_option('V', "verbose", false, "level", "set log verbosity level").unwrap();
        let _ = clp.add_short_string_option('c', false, "file path", "configuration file path").unwrap();
        let args = vec!["test_sho_help".to_string(), "-V".to_string(), "-12".to_string()];        
        assert_eq!(Ok(()), clp.parse(&args));
        let value = clp.get_value::<f32>(&verbosity);
        assert_eq!("IntegerCommandLineOptionType -V/--verbose: is not of the required type".to_string(), value.unwrap_err().to_string());
        let value = clp.get_value::<i8>(&verbosity);
        assert_eq!(-12, value.unwrap());         
    }

    #[test]
    fn mismatch_assignment() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let _ = clp.add_integer_option('V', "verbose", false, "level", "set log verbosity level").unwrap();
        let _ = clp.add_short_string_option('c', false, "file path", "configuration file path").unwrap();
        let args = vec!["mismatch_assignment".to_string(),
            "-V=123.3".to_string()
        ];
        assert_eq!("IntegerCommandLineOptionType -V/--verbose: cannot set a floating point value to this option type".to_string(), 
                clp.parse(&args).unwrap_err().to_string());
    }

    #[test]
    fn option_arg_already_assigned() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_version_option("print-out application version").unwrap();
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let _ = clp.add_integer_option('V', "verbose", false, "level", "set log verbosity level").unwrap();
        let _ = clp.add_short_string_option('c', false, "file path", "configuration file path").unwrap();
        let mut args = vec!["test_sho_help".to_string(), "-cV=goofy.txt".to_string()];        
        assert_eq!("IntegerCommandLineOptionType -V/--verbose: option argument already assigned".to_string(), 
                    clp.parse(&args).unwrap_err().to_string());
        args = vec!["test_sho_help".to_string(), "-cV".to_string(), "goofy.txt".to_string()];        
        assert_eq!("IntegerCommandLineOptionType -V/--verbose: option argument already assigned".to_string(), 
                clp.parse(&args).unwrap_err().to_string());
        }

    #[test]
    fn test_boolean_option_eq() {
        let mut clp = CommandLineParser::new(None);
        let version_option = clp.add_version_option("print-out application version").unwrap();
        let args = vec!["test_boolean_option_eq".to_string(), "-v=false".to_string()];        
        assert_eq!(Ok(()), clp.parse(&args));
        assert_eq!(true, clp.is_set(&version_option));
        assert_eq!(Ok(false), clp.get_value::<bool>(&version_option));
    }
    #[test]
    fn test_boolean_option_arg_already_assigned() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_help_option("print-out help menu").unwrap();
        let _ = clp.add_version_option("print-out application version").unwrap();
        let args = vec!["test_boolean_option_eq".to_string(), "-hv=false".to_string()];        
        assert_eq!("BooleanCommandLineOptionType -v/--version: option argument already assigned".to_string(), 
                clp.parse(&args).unwrap_err().to_string());
    }

    #[test]
    fn test_long_form_option() {
        let mut clp = CommandLineParser::new(None);
        let config_option = clp.add_long_string_option("config", false, "file path", "application configuration file").unwrap();
        let args = vec!["test_long_form_option".to_string(), "--config=goofy.properties".to_string()];        
        assert_eq!(Ok(()), clp.parse(&args));
        assert_eq!(String::from("goofy.properties"), clp.get_value::<String>(&config_option).unwrap());
    }

    #[test]
    fn test_boolean_long_form_option() {
        let mut clp = CommandLineParser::new(None);
        let help_option = clp.add_help_option("show this help").unwrap();
        let args = vec!["test_boolean_long_form_option".to_string(), "--help=false".to_string()];        
        assert_eq!(Ok(()), clp.parse(&args));
        assert_eq!(Ok(false), clp.get_value::<bool>(&help_option));
    }

    #[test]
    fn test_get_values() {
        let mut clp = CommandLineParser::new(None);
        let config_option = clp.add_string_option('c', "config", false, "file path", "application configuration file").unwrap();
        let _ = clp.add_integer_option('V', "verbosity", false, "level", "log verbosity level").unwrap();
        let args = vec!["test_get_values".to_string(), 
                                    "--config=config1.properties".to_string(),
                                    "--verbosity=2".to_string(), 
                                    "-c".to_string(), 
                                    "config2.properties".to_string(),
                                    "remaining args1".to_string(),
                                    "--config".to_string(),
                                    "config3.properties".to_string(),
                                    ];   
        assert_eq!(Ok(()), clp.parse(&args));
        assert_eq!(true, clp.is_set(&config_option));
        let values: Vec<String> = clp.get_values(&config_option).unwrap();
        assert_eq!(vec!["config1.properties".to_string(), 
                        "config2.properties".to_string(),
                        "config3.properties".to_string()],
                    values);     
    }

    #[test]
    fn test_remaining_args() {
        let mut clp = CommandLineParser::new(None);
        let _ = clp.add_string_option('c', "config", false, "file path", "application configuration file").unwrap();
        let _ = clp.add_integer_option('V', "verbosity", false, "level", "log verbosity level").unwrap();
        let args = vec!["test_get_values".to_string(), 
                                    "--config=config1.properties".to_string(),
                                    "remaining_args1".to_string(),
                                    "--config".to_string(),
                                    "config2.properties".to_string(),
                                    "remaining_args2".to_string(),
                                    "--verbosity=2".to_string(), 
                                    "-c".to_string(), 
                                    "config3.properties".to_string(),
                                    "remaining_args3".to_string(),
                                    ];   
        assert_eq!(Ok(()), clp.parse(&args));
        let remaining_args = vec![
            "remaining_args1".to_string(),
            "remaining_args2".to_string(),
            "remaining_args3".to_string()
        ];
        assert_eq!(clp.get_remaining_args(), &remaining_args);
        println!("get_remaining_args(): {:#?}", clp.get_remaining_args());
        println!("remaining_args: {:#?}", remaining_args);
    }

    #[test]
    fn test_positional_args() {
        let mut clp = CommandLineParser::new(None);
        clp.set_parsing_mode(ParsingMode::PositionalArgumentsMode);
        let _ = clp.add_string_option('c', "config", false, "file path", "application configuration file").unwrap();
        let _ = clp.add_integer_option('V', "verbosity", false, "level", "log verbosity level").unwrap();

        let args = vec!["test_get_values".to_string(), 
                                    "--config=config1.properties".to_string(),
                                    "remaining_args1".to_string(),
                                    "--config".to_string(),
                                    "config2.properties".to_string(),
                                    "remaining_args2".to_string(),
                                    "--".to_string(),
                                    "--nocapture".to_string(), 
                                    "--test-threads=1".to_string()
                                    ];   
        assert_eq!(Ok(()), clp.parse(&args));
        let remaining_args = vec![
            "remaining_args1".to_string(),
            "remaining_args2".to_string(),
        ];
        assert_eq!(clp.get_remaining_args(), &remaining_args);
        println!("get_remaining_args(): {:#?}", clp.get_remaining_args());
        println!("remaining_args: {:#?}", remaining_args);

        let positional_args = vec![
            "--nocapture".to_string(), 
            "--test-threads=1".to_string()
        ];
        assert_eq!(clp.get_positional_args(), &positional_args);
        println!("get_positional_args(): {:#?}", clp.get_positional_args());
        println!("positional_args: {:#?}", positional_args);
    }

    #[test]
    fn dashdash_default_parse_mode() {
        let mut clp = CommandLineParser::new(None);
        let dashdash_option = clp.add_string_option('-', "--", false, "file path", "remove file starting with -").unwrap();
        let args = vec!["test_get_values".to_string(), 
                                    "--".to_string(),
                                    "-".to_string()
        ];
        assert_eq!(Ok(()), clp.parse(&args));
        assert_eq!(true, clp.is_set(&dashdash_option));
        assert_eq!("-".to_string(), clp.get_value::<String>(&dashdash_option).unwrap())
    }
}