-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.c
1212 lines (1129 loc) · 45.4 KB
/
args.c
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
/*
* MIT License
*
* Copyright (c) 2024 Michel Kakulphimp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "args.h"
#include "console.h"
int current_arg_index = 1;
CliOptionGroup_t *options_registry[MAX_OPTION_GROUPS];
bool arg_ledger[MAX_CLI_ARGS] = {false};
int num_registered_options = 0;
CliOptions_t *last_options_parsed = NULL;
/* String representations of the option types */
const char *opt_type_strings[] = {
"NONE", // OPTION_TYPE_NONE
"FLAG", // OPTION_TYPE_FLAG
"STRING", // OPTION_TYPE_STRING
"ENUM", // OPTION_TYPE_ENUM
"FLOAT", // OPTION_TYPE_FLOAT
"INT", // OPTION_TYPE_INT
"UINT", // OPTION_TYPE_UINT
"UINT32", // OPTION_TYPE_UINT32
"UINT64", // OPTION_TYPE_UINT64
"HEXUINT8", // OPTION_TYPE_HEXUINT8
"HEXUINT16", // OPTION_TYPE_HEXUINT16
"HEXUINT32", // OPTION_TYPE_HEXUINT32
"HEXUINT64", // OPTION_TYPE_HEXUINT64
"FUNC_PTR", // OPTION_TYPE_FUNC_PTR
};
/**
* @brief Set an OPTION_TYPE_FLAG option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the flag to
*/
void args_set_flag_value(const char *name, CliOptions_t *options, int value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
(*((bool *)(options[i].destination))) = value;
break;
}
}
}
/**
* @brief Set an OPTION_TYPE_STRING option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the string to
*/
void args_set_string_value(const char *name, CliOptions_t *options, const char *value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
strcpy((char *)(options[i].destination), value);
break;
}
}
}
/**
* @brief Set an OPTION_TYPE_ENUM option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the enum to
*/
void args_set_enum_value(const char *name, CliOptions_t *options, int value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
(*((int *)(options[i].destination))) = value;
break;
}
}
}
/**
* @brief Set an OPTION_TYPE_INT option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the option to
*/
void args_set_int_value(const char *name, CliOptions_t *options, int value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
(*((int *)(options[i].destination))) = value;
break;
}
}
}
/**
* @brief Set an OPTION_TYPE_UINT8 option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the option to
*/
void args_set_u_int8_value(const char *name, CliOptions_t *options, uint8_t value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
(*((uint8_t *)(options[i].destination))) = value;
break;
}
}
}
/**
* @brief Set an OPTION_TYPE_UINT16 option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the option to
*/
void args_set_u_int16_value(const char *name, CliOptions_t *options, uint16_t value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
(*((uint16_t *)(options[i].destination))) = value;
break;
}
}
}
/**
* @brief Set an OPTION_TYPE_UINT32 option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the option to
*/
void args_set_u_int32_value(const char *name, CliOptions_t *options, uint32_t value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
(*((uint32_t *)(options[i].destination))) = value;
break;
}
}
}
/**
* @brief Set an OPTION_TYPE_UINT64 option to a value.
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @param value The value to set the option to
*/
void args_set_u_int64_value(const char *name, CliOptions_t *options, uint64_t value)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
(*((uint64_t *)(options[i].destination))) = value;
break;
}
}
}
/**
* @brief Get an option's destination pointer
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return void* The destination pointer of the option
*/
void *args_get_option_destination_pointer(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return options[i].destination;
}
}
return 0;
}
/**
* @brief Get an OPTION_TYPE_FLAG option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return int The value of the option
*/
int args_get_flag_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (*((bool *)(options[i].destination)));
}
}
return 0;
}
/**
* @brief Get an OPTION_TYPE_STRING option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return const char* The value of the option
*/
const char *args_get_string_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (char *)(options[i].destination);
}
}
return NULL;
}
/**
* @brief Get an OPTION_TYPE_ENUM option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return int The value of the option
*/
int args_get_enum_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (*((int *)(options[i].destination)));
}
}
return 0;
}
/**
* @brief Get an OPTION_TYPE_INT option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return int The value of the option
*/
int args_get_int_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (*((int *)(options[i].destination)));
}
}
return 0;
}
/**
* @brief Get an OPTION_TYPE_UINT8 option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return uint8_t The value of the option
*/
uint8_t args_get_u_int8_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (*((uint8_t *)(options[i].destination)));
}
}
return 0;
}
/**
* @brief Get an OPTION_TYPE_UINT16 option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return uint16_t The value of the option
*/
uint16_t args_get_u_int16_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (*((uint16_t *)(options[i].destination)));
}
}
return 0;
}
/**
* @brief Get an OPTION_TYPE_UINT32 option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return uint32_t The value of the option
*/
uint32_t args_get_u_int32_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (*((uint32_t *)(options[i].destination)));
}
}
return 0;
}
/**
* @brief Get an OPTION_TYPE_UINT64 option's value
*
* @param name The name of the option (for lookup)
* @param options The list of options to look through
* @return uint64_t The value of the option
*/
uint64_t args_get_u_int64_value(const char *name, CliOptions_t *options)
{
for (int i = 0; options[i].name != 0; i++)
{
if (strcmp(options[i].name, name) == 0)
{
return (*((uint64_t *)(options[i].destination)));
;
}
}
return 0;
}
/**
* @brief Set the last options as all parsed or not all parsed
*
* @param state The parsed state to set the last options to
*/
void args_set_last_option_parsed(bool state)
{
if (last_options_parsed != NULL)
{
args_set_all_parsed(last_options_parsed, state);
}
}
/**
* @brief Set all provided options as all parsed or not all parsed
*
* @param options The options whose parsed state is to be all set
* @param state The parsed state to set the options to
*/
void args_set_all_parsed(CliOptions_t *options, bool state)
{
unsigned int index = 0;
while (options[index].name != 0)
{
options[index].is_parsed = state;
index++;
}
}
/**
* @brief Set the parsed state of an option using its name as the key
*
* @param options The options to look through
* @param name The name of the option to set the parsed state of
* @param state The parsed state to set the option to
*/
void args_set_parsed(CliOptions_t *options, const char *name, bool state)
{
unsigned int index = 0;
while (options[index].name != 0)
{
if (strcmp(options[index].name, name) == 0)
{
options[index].is_parsed = state;
break;
}
index++;
}
}
/**
* @brief Sets the is_defined_ptr flag for the option with the given name
*
* @param options The options that the option is in
* @param name The name of the option
* @param state The state to set the is_defined_ptr flag to
*/
void args_set_defined(CliOptions_t *options, const char *name, bool state)
{
unsigned int index = 0;
while (options[index].name != 0)
{
if (strcmp(options[index].name, name) == 0)
{
if (options[index].is_defined_ptr != NULL)
{
*options[index].is_defined_ptr = state;
console_print_debug(LOGGING_LEVEL_1, "%s: Option \"%s\" set as defined through its pointer @ 0x%p.", __func__, name, options[index].is_defined_ptr);
}
else
{
console_print_warn(LOGGING_LEVEL_1, "%s: Option \"%s\" does not have a defined flag variable associated! Cannot set to defined state.", __func__, name);
}
break;
}
index++;
}
}
/**
* @brief Check if all options have been parsed
*
* @param options The options to check
* @return true All options have been parsed
* @return false Not all options have been parsed
*/
bool args_check_all_parsed(CliOptions_t *options)
{
unsigned int index = 0;
bool options_parsed = true;
while (options[index].name != 0)
{
options_parsed &= options[index].is_parsed;
index++;
}
return options_parsed;
}
/**
* @brief Check if an option has been parsed
*
* @param options The options to check
* @param name The name of the option to check
* @return true The option has been parsed
* @return false The option has not been parsed or the option name was not found
*/
bool args_check_parsed(CliOptions_t *options, const char *name)
{
unsigned int index = 0;
bool option_parsed = false;
while (options[index].name != 0)
{
if (strcmp(options[index].name, name) == 0)
{
option_parsed = options[index].is_parsed;
break;
}
index++;
}
return option_parsed;
}
/**
* @brief Check if all option pointers are valid (are not NULL)
*
* @param options The options to check
* @return true All option pointers are valid
* @return false One or more option pointers are invalid
*/
bool args_check_pointers(CliOptions_t *options)
{
bool fatal_error = false;
unsigned int index = 0;
while (options[index].name != 0)
{
if (!options[index].destination)
{
console_print_error(LOGGING_LEVEL_0, "%s: Null pointer for option %d destination!", __FUNCTION__, index);
return false;
}
index++;
}
return true;
}
/**
* @brief Check if an option is set or not (i.e. if it was specified on the command line)
*
* @param options The options to check
* @param name The name of the option to check
* @return true The option is set
* @return false The option is not set or the option name was not found
*/
bool args_check_defined(CliOptions_t *options, const char *name)
{
unsigned int index = 0;
while (options[index].name != 0)
{
if (strcmp(options[index].name, name) == 0)
{
if (options[index].is_defined_ptr != NULL)
{
return *options[index].is_defined_ptr;
}
else
{
return false;
}
}
index++;
}
return false;
}
/**
* @brief Print an option to the console
*
* @param logging_level The logging level to print the option at
* @param option The option to print
*/
void args_print_option(LoggingLevel_e logging_level, CliOptions_t *option)
{
char option_name_buff[MAX_OPT_NAME_LENGTH + 1];
const char tag_arg[] = ANSI_COLOR_CYAN "[arg]" ANSI_COLOR_RESET;
const char tag_func[] = ANSI_COLOR_GREEN "[fnc]" ANSI_COLOR_RESET;
const char tag_none[] = ".....";
const char *tag = NULL;
if (option->arg_type)
{
tag = tag_arg;
}
else if (option->option_type == OPTION_TYPE_FUNC_PTR)
{
tag = tag_func;
}
else
{
tag = tag_none;
}
snprintf(option_name_buff, sizeof(option_name_buff), "--%s", option->name);
console_print(logging_level, " %-0*s %-0*s %-0*s", MAX_OPT_NAME_LENGTH, option_name_buff, MAX_OPT_ARGS_LENGTH, tag, MAX_OPT_DESC_LENGTH, option->description);
}
/**
* @brief Print the help for a function by looking up the function pointer in the options registry
*
* @param function The function pointer to print the help for
*/
void args_print_help(ConsoleFunctionPointer_t function)
{
console_print_header(LOGGING_LEVEL_0, "Help");
if (function)
{
CliOptionGroup_t *function_options = NULL;
CliOptions_t *parent_function_option = NULL;
/* Function pointer specified, print that function's options and any other option that is not a function pointer */
for (int i = 0; i < num_registered_options; i++)
{
/* Look for the specified function pointer in the options registry */
CliOptions_t *options = options_registry[i]->options;
unsigned int index = 0;
while (options[index].name != 0)
{
if (options[index].destination == (void *)function)
{
parent_function_option = &options[index];
function_options = options[index].function_options;
break;
}
index++;
}
}
if (!function_options)
{
console_print(LOGGING_LEVEL_0, "The following function does not have specific arguments:");
}
else
{
console_print(LOGGING_LEVEL_0, "The following are options for the following function:");
}
console_print_new_line(LOGGING_LEVEL_0);
args_print_option(LOGGING_LEVEL_0, parent_function_option);
if (function_options)
{
/* Print that function's extended help if it exists */
if (function_options->extended_help)
{
console_print_sub_header(LOGGING_LEVEL_0, "Extended Help");
console_print_block(LOGGING_LEVEL_0, function_options->extended_help);
}
/* Print that function's options */
console_print_sub_header(LOGGING_LEVEL_0, "%s", function_options->name);
CliOptions_t *options = function_options->options;
unsigned int index = 0;
while (options[index].name != 0)
{
args_print_option(LOGGING_LEVEL_0, &options[index]);
index++;
}
}
}
else
{
console_print_block(LOGGING_LEVEL_0,
"The following are the options for this program. If the option represents a function pointer that directly executes an internal function, it will be proceeded by a " ANSI_COLOR_GREEN "[fnc]" ANSI_COLOR_RESET " tag. If the option expects an argument, it will be proceeded by an " ANSI_COLOR_CYAN "[arg]" ANSI_COLOR_RESET " tag. For further help on a function, --help can be appended after a function for specific help on that function.");
}
for (int i = 0; i < num_registered_options; i++)
{
if (function)
{
/* If we're printing a function pointer's option, ignore all other function pointer options. Do this by
going through the current registered option group and skip it entirely if all options are function pointers
*/
bool dont_print_group = true;
CliOptions_t *options = options_registry[i]->options;
unsigned int index = 0;
while (options[index].name != 0)
{
/* Go through all options and see if any aren't function pointers */
if (options[index].option_type != OPTION_TYPE_FUNC_PTR)
{
dont_print_group = false;
break;
}
index++;
}
if (dont_print_group)
{
/* Skip printing this group entirely */
continue;
}
}
console_print_sub_header(LOGGING_LEVEL_0, "%s", options_registry[i]->name);
CliOptions_t *options = options_registry[i]->options;
unsigned int index = 0;
while (options[index].name != 0)
{
args_print_option(LOGGING_LEVEL_0, &options[index]);
index++;
}
}
console_print_new_line(LOGGING_LEVEL_0);
}
/**
* @brief This function registers an options group with the central registry. If a function pointer is passed it, the
* group is instead associated with the option that matches the same function pointer in the options registry.
*
* @param option_group A pointer to the options group to register
* @param function An optional function pointer to register the options group with the same function
* pointer option found in the main registry.
*/
void args_register_options(CliOptionGroup_t *option_group, ConsoleFunctionPointer_t function)
{
bool fatal_error = false;
CliOptions_t *parent_function_option = NULL;
CliOptions_t *options = NULL;
bool function_option_registered = false;
int index = 0;
console_print_debug(LOGGING_LEVEL_1, "%s: Attempting to register \"%s\" to options registry", __FUNCTION__, option_group->name);
if (num_registered_options >= MAX_OPTION_GROUPS)
{
console_print_error(LOGGING_LEVEL_0, "%s: Fatal error: can't register any more option groups!", __FUNCTION__);
fatal_error = true;
}
else
{
/* Perform a sanity check on the options */
options = option_group->options;
index = 0;
while (options[index].name)
{
if (options[index].arg_type == ARG_TYPE_NO_ARGUMENT)
{
/* Sanity check that the option type is a flag or function pointer */
if (options[index].option_type != OPTION_TYPE_FLAG && options[index].option_type != OPTION_TYPE_FUNC_PTR)
{
console_print_error(LOGGING_LEVEL_0, "%s: Fatal error! Option \"%s\" is not a OPTION_TYPE_FLAG or OPTION_TYPE_FUNC_PTR but was specified with ARG_TYPE_NO_ARGUMENT!", __FUNCTION__, options[index].name);
exit(FR_ASSERT_FAIL);
}
}
else if (options[index].arg_type == ARG_TYPE_NO_ARGUMENT)
{
/* Sanity check that the option type is not a flag or function pointer */
if (options[index].option_type == OPTION_TYPE_FLAG || options[index].option_type == OPTION_TYPE_FUNC_PTR)
{
console_print_error(LOGGING_LEVEL_0, "%s: Fatal error! Option \"%s\" is a OPTION_TYPE_FLAG or OPTION_TYPE_FUNC_PTR but was specified with ARG_TYPE_REQUIRED_ARGUMENT!", __FUNCTION__, options[index].name);
exit(FR_ASSERT_FAIL);
}
}
index++;
}
/* If a function pointer was passed in, the user intends to link the options to an existing option that is of
the function pointer type. This allows one to link both option groups to display from a single help print option.*/
if (function)
{
console_print_debug(LOGGING_LEVEL_1, "%s: Looking for option with destination 0x%x", __FUNCTION__, function);
/* Iterate through existing registered option groups */
for (int i = 0; i < num_registered_options; i++)
{
options = options_registry[i]->options;
index = 0;
while (options[index].name)
{
/* Iterate through function group options */
if (options[index].option_type == OPTION_TYPE_FUNC_PTR &&
options[index].destination == (void *)function)
{
/* Found a match, update the function_options member */
options[index].function_options = option_group;
function_option_registered = true;
parent_function_option = &options[index];
break;
}
index++;
}
}
if (!function_option_registered)
{
console_print_error(LOGGING_LEVEL_0, "%s: Fatal error: couldn't find a matching function pointer for option registration!", __FUNCTION__);
fatal_error = true;
}
else
{
console_print_debug(LOGGING_LEVEL_1, "%s: Successfully registered options \"%s\" to function \"%s\"", __FUNCTION__, option_group->name, parent_function_option->name);
}
}
/* Options not tied to a function, add it to the main registry as another entry */
else
{
/* Don't register more than once */
bool options_registered = false;
for (int i = 0; i < num_registered_options; i++)
{
if (options_registry[i] == option_group)
{
options_registered = true;
}
}
if (!options_registered)
{
options_registry[num_registered_options++] = option_group;
console_print_debug(LOGGING_LEVEL_1, "%s: Successfully registered options \"%s\" to options registry! Registry now has %d options registered.", __FUNCTION__, option_group->name, num_registered_options);
}
else
{
console_print_debug(LOGGING_LEVEL_1, "%s: Options \"%s\" was already in the registry. Nothing happened.", __FUNCTION__, option_group->name, num_registered_options);
}
}
}
/* Exit after fatal errors */
if (fatal_error)
{
exit(1);
}
}
/**
* @brief This function performs the equivalent of the standard getopt_long_only but for enhanced
* options defined by CliOptions_t. This function will update the argument ledger to keep track
* of which arguments have been parsed.
*
* @param argc The count of arguments
* @param argv The arguments array
* @param options The options to parse
* @param option_index The index of the option that was parsed
* @param option_arg The argument of the option that was parsed
*
* @return GetOptResult_e The result of the parsing. GETOPT_OK if the option was parsed successfully,
* GETOPT_MISSING_ARG if an argument was missing, GETOPT_STRAY_ARG if a stray argument was
* found, GETOPT_END if the end of the options was reached, GETOPT_HELP if the help option was
* requested.
*/
GetOptResult_e args_getopt_index(int argc, char *argv[], CliOptions_t *options, int *option_index, char **option_arg)
{
/* Go through arguments from where we left off */
int dash_count = 0;
for (int arg_index = current_arg_index; arg_index < argc; arg_index++)
{
/* Skip if we've already successfully parsed this argument */
if (arg_ledger[arg_index])
{
continue;
}
console_print_debug(LOGGING_LEVEL_1, "%s: Parsing argument \"%s\"...", __FUNCTION__, argv[arg_index]);
/* Check if the argument is help */
if ((strcmp(argv[arg_index], "--help") == 0) || (strcmp(argv[arg_index], "-help") == 0))
{
/* Help was requested */
console_print_debug(LOGGING_LEVEL_1, "%s: Help requested!", __FUNCTION__);
arg_ledger[arg_index] = true; /* Mark the help argument as parsed */
return GETOPT_HELP;
}
/* Get dash count */
if (argv[arg_index][0] == '-')
{
dash_count = 1;
}
if (argv[arg_index][0] == '-' && argv[arg_index][1] == '-')
{
dash_count = 2;
}
/* Check if we've run into a stray argument (we should always be pointing at an option) */
if (dash_count == 0)
{
console_print_error(LOGGING_LEVEL_0, "%s: Fatal error! Stray argument \"%s\" found!", __FUNCTION__, argv[arg_index]);
return GETOPT_STRAY_ARG;
}
/* Iterate through the options that were passed in */
for (int options_index = 0; options[options_index].name != 0; options_index++)
{
/* Skip if we've already parsed this option */
if (options[options_index].is_parsed)
{
continue;
}
/* Check if argument matches */
if (strcmp(options[options_index].name, argv[arg_index] + dash_count) == 0)
{
/* Check if the option is a flag or a function pointer */
if (options[options_index].arg_type == ARG_TYPE_NO_ARGUMENT)
{
/* We found the option, set the index and return */
*option_index = options_index; /* Set the index */
*option_arg = NULL; /* No argument for this option */
options[options_index].is_parsed = true; /* Mark the option as parsed */
arg_ledger[arg_index] = true; /* Mark the argument as parsed */
current_arg_index = arg_index + 1; /* Move the current argument index so that we start parsing on the next one */
console_print_debug(LOGGING_LEVEL_1, "%s: Found option \"%s\".", __FUNCTION__, options[options_index].name);
return GETOPT_OK;
}
/* Check if the option is a required argument */
else if (options[options_index].arg_type == ARG_TYPE_REQUIRED_ARGUMENT)
{
/* Make sure we have an argument */
if (arg_index + 1 < argc)
{
/* Make sure the next argument is not an option */
if (argv[arg_index + 1][0] != '-')
{
/* We found the option's argument, set the index and argument pointer and return */
*option_index = options_index; /* Set the index */
*option_arg = argv[arg_index + 1]; /* Set the argument */
options[options_index].is_parsed = true; /* Mark the option as parsed */
arg_ledger[arg_index] = true; /* Mark the argument as parsed (recognized option) */
arg_ledger[arg_index + 1] = true; /* Mark the argument as parsed (recognized argument) */
current_arg_index = arg_index + 2; /* Move the current argument index so that we start parsing on the next one */
console_print_debug(LOGGING_LEVEL_1, "%s: Found option \"%s\" with required argument \"%s\"", __FUNCTION__, options[options_index].name, argv[arg_index + 1]);
return GETOPT_OK;
}
}
/* If we got here, we're missing an argument */
console_print_error(LOGGING_LEVEL_0, "%s: Error! Option \"%s\" requires an argument!", __FUNCTION__, options[options_index].name);
return GETOPT_MISSING_ARG;
}
}
else
{
/* Option didn't match, go to the next one */
continue;
}
}
}
/* If we're escaped the for loop, we've reached the end of the options */
current_arg_index = argc;
return GETOPT_END;
}
/**
* @brief Parse the command line arguments for function arguments. Options can be specified through the command
* line with either a double dash or single dash. An argument is specified by appending the argument
* proceeding an option, but without dashes. For example, to specify an argument for the option "--option",
* the command line argument would be "--option argument".
*
* @param[in] argc The count of arguments
* @param argv The arguments array
* @param function If set, this function will parse options pointed to by options with
* arg_type==OPTION_TYPE_FUNC_PTR
* @param enable_help If set, this function will display help if the --help option is passed
*/
ConsoleFunctionPointer_t args_parse(int argc, char *argv[], ConsoleFunctionPointer_t function, bool enable_help)
{
int option_index;
char *option_argument;
int option_offset;
GetOptResult_e get_opt_result;
ConsoleFunctionPointer_t function_pointer_argument = NULL;
CliOptionGroup_t *function_options_group = NULL;
CliOptionGroup_t *current_options_group = NULL;
CliOptions_t *current_options = NULL;
bool fatal_error = false;
bool help_wanted = false;
/* Check if we have arguments to parse */
if (!(argc > 1))
{
console_print_warn(LOGGING_LEVEL_1, "%s: No arguments to parse!", __FUNCTION__);
return NULL;
}
/* Check if we support the number of arguments */
if (argc > MAX_CLI_ARGS)
{
console_print_error(LOGGING_LEVEL_0, "%s: Fatal error! Too many arguments to parse! (%d > %d)", __FUNCTION__, argc, MAX_CLI_ARGS);
return NULL;
}
console_print_debug(LOGGING_LEVEL_1, "%s: Command line arguments detected, will try to parse them", __FUNCTION__);
/* If we were provided a function pointer, we must also parse those arguments. We parse it at the end of our list,
after the option registry option groups. */
int option_groups_to_parse = (function ? num_registered_options + 1 : num_registered_options);
for (int i = 0; i < option_groups_to_parse; i++)
{
/* If we're also parsing a function's arguments, parse the function arguments if we're at the last item */
if (function && (i == num_registered_options))
{
if (!function_options_group)
{
console_print_warn(LOGGING_LEVEL_1, "%s: Function does not have options to parse.", __FUNCTION__);
break;
}
else
{
current_options_group = function_options_group;
current_options = function_options_group->options;
}
}
else
{
current_options_group = options_registry[i];