-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptly.h
More file actions
1347 lines (1054 loc) · 40.9 KB
/
optly.h
File metadata and controls
1347 lines (1054 loc) · 40.9 KB
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
/*
optly.h — v2.3.1
Single-header command line argument parser for C.
Features
--------
* STB-style single-header library
* Commands and command-specific flags
* Long flags (--verbose)
* Short flags (-v)
* Batched short flags (-abc)
* Inline flag values (--threads=4)
* Separate flag values (--threads 4)
* Typed flag values
* Positional arguments
* Optional commands
* Optional flags
* No dynamic memory allocation
* Portable C (C99)
Basic Usage
-----------
#define OPTLY_IMPLEMENTATION
#include "optly.h"
// You need to define "main" command, which is your application.
// Note: If you compile with `gcc` with flag `-pedantic` this will fire warning.
// To suppress it have static variables for flags, commands and enum flag values separately,
// or move cmd to runtime (to `main` function)
static OptlyCommand cmd = {
// Name will be used in usage. If you set is as NULL it will be argv[0]
.name = NULL,
// If not null, then it will be used in usage
.description = NULL,
// This is your "top-level global" flags. They should be specified before any command (./app --thread 2 -v)
.flags = (OptlyFlag[]){
// Long (full name) short name description (for usage) Default value Flag value type
{ "verbose", 'v', "Enable verbose output", .value.as_bool = false, .type = OPTLY_TYPE_BOOL },
// Values are unions, so you need to specify member of value with correct type some way
{ "threads", 't', "Worker threads", false, {.as_uint32 = 4}, OPTLY_TYPE_UINT32 },
// Flag arrays should alwasy ends with NULL_FLAG. Try to not forget about it :)
NULL_FLAG,
},
// This is your commands. (git `commit`, docker `compose` `up`)
.commands = (OptlyCommand[]){
// Instead of defining whole struct manually you can use helper functions
optly_command("run", "Runs server",
// optly_flags macro deals with type castings and closing array with NULL_FLAG
optly_flags(
// This is *command* flag. `./app -p 8080 run` will not work, but `./app run -p 8080` will
optly_flag_uint16("port", 'p', "Server port", .value.as_uint16 = 8080)
// NOTE: this flag and 'verbose' GLOBAL flag are not the same
// `./app -v run` - enables global -v flag
// `./app run -v` - enables command -v flag
optly_flag_bool("verbose", 'v', "Enable verbose output for worker", .value.as_bool = false)
),
// This is subcommands of command "run"
optly_commands(
// `./app run check` subcommand have no description, flags or subcommands.
// NULL is here to suppress warning about not providing argument in variadic macro
optly_command("check", NULL),
// `./app run dump_config` subcommands
optly_command("dump_config",
// We skipped description, so we need to specify fileds now
.flags = optly_flags(
optly_flag_bool("color", 'c', "Show colors", .value.as_bool = false)
)
)
),
// Positioanl arguments can be defined lilke this
optly_positionals(optly_positional("address", "Address to listen on", .min = 0, .max = 1)),
),
}
};
int main(int argc, char **argv) {
optly_parse_args(argc, argv, &cmd);
printf("Verbose: %s\n", optly_flag_value_bool(&cmd, "verbose") ? "true" : "false");
printf("Threads: %u\n", optly_flag_value_uint32(&cmd, "threads"));
if (!cmd.next_command) {
return 0;
}
printf("Command: %s\n", cmd.next_command->name);
printf("Verbose: %s\n", optly_flag_value_bool(cmd.next_command, "verbose") ? "true" : "false");
printf("Port: %u\n", optly_flag_value_uint16(cmd.next_command, "port"));
if (cmd.next_command->next_command) {
printf("Command: %s\n", cmd.next_command->next_command->name);
}
OptlyPositional *address = optly_get_positional(&cmd, "address");
if (address && address->count == 1) {
printf("Address: %s\n", address->values[1]);
} else {
printf("Address: 0.0.0.0\n");
}
}
Commands
--------
Commands are positional tokens that do not begin with '-'.
app build
app run
Each command may define its own flags.
Flags
-----
Flags may be long or short.
--verbose
-v
Flags may have values.
--threads=4
--threads 4
-t 4
Short flags can be batched.
-abc -> -a -b -c
Positional Arguments
--------------------
Any non-flag tokens after command selection are stored as positional arguments.
app build file1 file2
Access:
Named positional: (can have many valies inside)
Positional *pos = optly_get_positional(&cmd, "name");
Or directly through command:
for (OptlyPositional *p = cmd.positionals; p->name; p++) {
// ...
}
Help and version command/flag generation
----------------------------
You can define
#define OPTLY_GEN_HELP_FLAG
#define OPTLY_GEN_HELP_COMMAND
to generate help flag `--help | -h` and/or help command `help cmd`, or
#define OPTLY_GEN_VERSION_FLAG
#define OPTLY_GEN_VERSION_COMMAND
to generate version flag `--version | -v` and/or version command `version`.
If help/version command/flag would be found during parsing usage would be
automatically called and `exit(0)` is called.
Note that user defined flags with `-h`/`-v` would interfere with generated flags.
Error and logging handling
--------------
This library always logs errors internally using OPTLY_LOG.
Instead of failing on first error, Optly now accumulates all errors during parsing.
`optly_parse_args()` returns an `OptlyErrors` struct:
OptlyErrors errs = optly_parse_args(argc, argv, &cmd);
for (size_t i = 0; i < optly_errors_count(&errs); i++) {
OptlyError e = optly_errors_at(&errs, i);
printf("Error: %s", optly_error_message(e.kind));
if (e.arg) {
printf(" (%s)", e.arg);
}
printf("\n");
}
Each error may include optional context, which could be - flag, value, command or positional name
By default, Optly will call `exit()` if any errors occurred.
To disable this behavior:
#define OPTLY_NO_EXIT
This is useful for:
- testing
- custom error handling
The best way to control logging is to use Logcie library (https://github.com/strongleong/logcie).
Or have a look at OPTLY_LOG family of macros.
Licese
------
MIT/Public domain - choose whitchever you prefer
*/
#ifndef OPTLY_H
#define OPTLY_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifndef OPTLYDEF
#define OPTLYDEF
#endif // OPTLYDEF
// Versioning macros
#define OPTLY_VERSION_MAJOR 2
#define OPTLY_VERSION_MINOR 3
#define OPTLY_VERSION_RELEASE 1
#define OPTLY_VERSION_NUMBER (OPTLY_VERSION_MAJOR * 100 * 100 + OPTLY_VERSION_MINOR * 100 + OPTLY_VERSION_RELEASE)
#define OPTLY_VERSION_FULL OPTLY_VERSION_MAJOR.OPTLY_VERSION_MINOR.OPTLY_VERSION_RELEASE
#define OPTLY_QUOTE(str) #str
#define OPTLY_EXPAND_AND_QUOTE(str) OPTLY_QUOTE(str)
#define OPTLY_VERSION_STRING OPTLY_EXPAND_AND_QUOTE(OPTLY_VERSION_FULL)
// If you need more that 64 positional arguments per ONE command
// you should look in the mirror really deep
#ifndef OPTLY_MAX_POSITIONALS
#define OPTLY_MAX_POSITIONALS 64
#endif
#ifndef OPTLY_FLAG_BUFFER_LENGTH
#define OPTLY_FLAG_BUFFER_LENGTH 256
#endif
#ifndef OPTLY_MAX_ERRORS
#define OPTLY_MAX_ERRORS 32
#endif
#ifndef OPTLY_HELP_SHORT_FLAG
#define OPTLY_HELP_SHORT_FLAG "-h"
#endif
#ifndef OPTLY_VERSION_SHORT_FLAG
#define OPTLY_VERSION_SHORT_FLAG "-v"
#endif
typedef enum OptlyFlagType {
OPTLY_TYPE_BOOL,
OPTLY_TYPE_CHAR,
OPTLY_TYPE_STRING,
OPTLY_TYPE_INT8,
OPTLY_TYPE_INT16,
OPTLY_TYPE_INT32,
OPTLY_TYPE_INT64,
OPTLY_TYPE_UINT8,
OPTLY_TYPE_UINT16,
OPTLY_TYPE_UINT32,
OPTLY_TYPE_UINT64,
OPTLY_TYPE_FLOAT,
OPTLY_TYPE_DOUBLE,
OPTLY_TYPE_ENUM,
} OptlyFlagType;
typedef union OptlyFlagValue {
bool as_bool;
char as_char;
char *as_string;
char **as_enum;
int8_t as_int8;
int16_t as_int16;
int32_t as_int32;
int64_t as_int64;
uint8_t as_uint8;
uint16_t as_uint16;
uint32_t as_uint32;
uint64_t as_uint64;
float as_float;
double as_double;
} OptlyFlagValue;
typedef struct {
char *fullname;
char shortname;
char *description;
bool required;
bool present;
OptlyFlagValue value;
OptlyFlagType type;
} OptlyFlag;
typedef struct {
char *name;
char *description;
size_t min;
size_t max;
char *values[OPTLY_MAX_POSITIONALS];
size_t count;
} OptlyPositional;
typedef struct OptlyCommand OptlyCommand;
struct OptlyCommand {
char *name;
char *description;
OptlyFlag *flags;
OptlyCommand *commands;
OptlyPositional *positionals;
OptlyCommand *next_command;
};
typedef enum OptlyErrorKind {
OPTLY_OK = 0,
OPTLY_ERR_UNKNOWN_FLAG,
OPTLY_ERR_UNKNOWN_COMMAND,
OPTLY_ERR_MISSING_VALUE,
OPTLY_ERR_INVALID_VALUE,
OPTLY_ERR_MISSING_REQUIRED,
OPTLY_ERR_POSITIONAL_TOO_FEW,
OPTLY_ERR_POSITIONAL_TOO_MANY,
OPTLY_ERR_DUPLICATE_VARIADIC,
OPTLY_ERR_BATCH_NON_BOOL,
Count_OptlyError
} OptlyErrorKind;
typedef struct OptlyError {
OptlyErrorKind kind;
const char *arg;
} OptlyError;
typedef struct OptlyErrors {
OptlyError items[OPTLY_MAX_ERRORS];
size_t count;
} OptlyErrors;
OPTLYDEF size_t optly_errors_count(const OptlyErrors *errs);
OPTLYDEF OptlyError optly_errors_at(const OptlyErrors *errs, size_t i);
OPTLYDEF const char *optly_error_message(OptlyErrorKind err);
OPTLYDEF void optly_error_print(const OptlyErrors *errs);
#define NULL_FLAG {.fullname = NULL, .shortname = 0, .value = {.as_int64 = 0}, .type = 0}
#define NULL_COMMAND {.name = NULL, .flags = NULL}
#define NULL_POSITIONAL {.name = NULL}
// NOTE: Forcing designated initializer for automatically zero-initializing missing fields
#define optly_flag(name, ...) \
(OptlyFlag) { \
.fullname = (name), __VA_ARGS__, \
.present = false \
}
#define optly_command(namme, ...) \
(OptlyCommand) { \
.name = (namme), __VA_ARGS__ \
}
#define optly_flags(...) \
(OptlyFlag[]) { \
__VA_ARGS__, NULL_FLAG \
}
#define optly_commands(...) \
(OptlyCommand[]) { \
__VA_ARGS__, NULL_COMMAND \
}
#define optly_positional(namme, ...) \
(OptlyPositional) { \
.name = (namme), __VA_ARGS__ \
}
#define optly_positionals(...) \
(OptlyPositional[]) { \
__VA_ARGS__, NULL_POSITIONAL \
}
#define optly_flag_bool(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_BOOL)
#define optly_flag_char(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_CHAR)
#define optly_flag_string(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_STRING)
#define optly_flag_int8(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_INT8)
#define optly_flag_int16(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_INT16)
#define optly_flag_int32(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_INT32)
#define optly_flag_int64(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_INT64)
#define optly_flag_uint8(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_UINT8)
#define optly_flag_uint16(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_UINT16)
#define optly_flag_uint32(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_UINT32)
#define optly_flag_uint64(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_UINT64)
#define optly_flag_float(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_FLOAT)
#define optly_flag_double(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_DOUBLE)
#define optly_flag_enum(name, ...) optly_flag(name, __VA_ARGS__, .type = OPTLY_TYPE_ENUM)
#define optly_enum_values(default, ...) \
.value.as_enum = (char *[]) { \
default, __VA_ARGS__, NULL \
}
#if defined(OPTLY_GEN_VERSION_FLAG) || defined(OPTLY_GEN_VERSION_COMMAND)
OPTLYDEF OptlyErrors optly_parse_args(int argc, char *argv[], OptlyCommand *main_cmd, const char *version);
#else
OPTLYDEF OptlyErrors optly_parse_args(int argc, char *argv[], OptlyCommand *main_cmd);
#endif
OPTLYDEF bool optly_is_command(OptlyCommand *command, const char *name);
OPTLYDEF const OptlyFlag *optly_get_flag(const OptlyFlag *flags, const char *name);
OPTLYDEF OptlyPositional *optly_get_positional(OptlyCommand *command, const char *name);
OPTLYDEF void optly_usage(OptlyCommand *command);
static inline bool optly_is_flag_null(const OptlyFlag *flag) {
return flag == NULL || (flag->fullname == NULL && flag->shortname == 0);
}
static inline bool optly_is_command_null(const OptlyCommand *cmd) {
return cmd == NULL || cmd->name == NULL;
}
inline OPTLYDEF bool optly_flag_value_bool(const OptlyCommand *command, const char *name);
inline OPTLYDEF char optly_flag_value_char(const OptlyCommand *command, const char *name);
inline OPTLYDEF char *optly_flag_value_string(const OptlyCommand *command, const char *name);
inline OPTLYDEF int8_t optly_flag_value_int8(const OptlyCommand *command, const char *name);
inline OPTLYDEF int16_t optly_flag_value_int16(const OptlyCommand *command, const char *name);
inline OPTLYDEF int32_t optly_flag_value_int32(const OptlyCommand *command, const char *name);
inline OPTLYDEF int64_t optly_flag_value_int64(const OptlyCommand *command, const char *name);
inline OPTLYDEF uint8_t optly_flag_value_uint8(const OptlyCommand *command, const char *name);
inline OPTLYDEF uint16_t optly_flag_value_uint16(const OptlyCommand *command, const char *name);
inline OPTLYDEF uint32_t optly_flag_value_uint32(const OptlyCommand *command, const char *name);
inline OPTLYDEF uint64_t optly_flag_value_uint64(const OptlyCommand *command, const char *name);
inline OPTLYDEF float optly_flag_value_float(const OptlyCommand *command, const char *name);
inline OPTLYDEF double optly_flag_value_double(const OptlyCommand *command, const char *name);
inline OPTLYDEF OptlyPositional *optly_get_positional(OptlyCommand *command, const char *name);
#endif // OPTLY_H
// -----------------------------------
#ifdef OPTLY_IMPLEMENTATION
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
// Logcie integration
#ifndef OPTLY_LOG
#ifdef LOGCIE
#ifdef LOGCIE_VA_LOGS
#define OPTLY_LOG(level, ...) LOGCIE_##level##_VA(__VA_ARGS__)
#else
#define OPTLY_LOG(level, ...) LOGCIE_##level(__VA_ARGS__)
#endif
#else
#define OPTLY_LOG(level, ...) \
do { \
fprintf(stderr, #level ": "__VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
#endif
#endif
#define SHIFT_ARG(argv, argc) (++(argv), --(argc))
static const char *error_messages[] = {
[OPTLY_OK] = "No error",
[OPTLY_ERR_UNKNOWN_FLAG] = "Unknown flag",
[OPTLY_ERR_UNKNOWN_COMMAND] = "Unknown command",
[OPTLY_ERR_MISSING_VALUE] = "Flag requires a value",
[OPTLY_ERR_INVALID_VALUE] = "Invalid value for flag",
[OPTLY_ERR_MISSING_REQUIRED] = "Required flag is not present",
[OPTLY_ERR_POSITIONAL_TOO_FEW] = "Not enough positional arguments",
[OPTLY_ERR_POSITIONAL_TOO_MANY] = "Too many positional arguments",
[OPTLY_ERR_DUPLICATE_VARIADIC] = "Duplicate variadic positional",
[OPTLY_ERR_BATCH_NON_BOOL] = "Cannot batch non-boolean flags",
};
OPTLYDEF const char *optly_error_message(OptlyErrorKind err) {
#if __STDC_VERSION__ >= 201112L // Check for C11 support
static_assert(Count_OptlyError == 10, "Forgot to update optly_error_message");
#else
assert(Count_OptlyError == 10 && "Forgot to update optly_error_message");
#endif
assert(err >= OPTLY_OK && err < Count_OptlyError);
return error_messages[err];
}
OPTLYDEF void optly_error_print(const OptlyErrors *errs) {
for (size_t i = 0; i < errs->count; i++) {
fprintf(stdout, "ERROR: %s (%s)\n", optly_error_message(errs->items[i].kind), errs->items[i].arg);
}
}
static void optly__push_error(OptlyErrors *errs, OptlyErrorKind err, const char *arg) {
if (!errs) {
return;
}
if (errs->count < OPTLY_MAX_ERRORS) {
errs->items[errs->count++] = (OptlyError){err, arg};
}
}
#ifdef OPTLY_NO_EXIT
#define OPTLY_EXIT(errs, code)
#else
#define OPTLY_EXIT(errs, code) \
do { exit(code); } while (0)
#endif
OPTLYDEF size_t optly_errors_count(const OptlyErrors *errs) {
return errs ? errs->count : 0;
}
OPTLYDEF OptlyError optly_errors_at(const OptlyErrors *errs, size_t i) {
return errs->items[i];
}
static bool optly__has_flags(OptlyCommand *cmd) {
return cmd && cmd->flags && !optly_is_flag_null(cmd->flags);
}
static bool optly__has_commands(OptlyCommand *cmd) {
return cmd && cmd->commands && !optly_is_command_null(cmd->commands);
}
static bool optly__has_positionals(OptlyCommand *cmd) {
return cmd && cmd->positionals && cmd->positionals->name;
}
static void optly__usage_positionals_signature(OptlyPositional *pos) {
if (!pos) return;
for (; pos->name; pos++) {
bool required = pos->min > 0;
bool variadic = pos->max == 0 || pos->max > 1;
if (required)
fprintf(stderr, " <%s%s>", pos->name, variadic ? "..." : "");
else
fprintf(stderr, " [%s%s]", pos->name, variadic ? "..." : "");
}
}
static void optly__usage_signature(OptlyCommand *cmd) {
fprintf(stderr, "Usage: %s", cmd->name);
if (optly__has_flags(cmd))
fprintf(stderr, " [FLAGS]");
if (optly__has_positionals(cmd))
optly__usage_positionals_signature(cmd->positionals);
if (optly__has_commands(cmd))
fprintf(stderr, " <COMMAND>");
fprintf(stderr, "\n");
}
static uint8_t type_name_pad = 8;
static const char *optly__flag_type_name(OptlyFlagType type) {
switch (type) {
case OPTLY_TYPE_BOOL: return "";
case OPTLY_TYPE_CHAR: return "<char>";
case OPTLY_TYPE_STRING: return "<str>";
case OPTLY_TYPE_INT8: return "<i8>";
case OPTLY_TYPE_INT16: return "<i16>";
case OPTLY_TYPE_INT32: return "<i32>";
case OPTLY_TYPE_INT64: return "<i64>";
case OPTLY_TYPE_UINT8: return "<u8>";
case OPTLY_TYPE_UINT16: return "<u16>";
case OPTLY_TYPE_UINT32: return "<u32>";
case OPTLY_TYPE_UINT64: return "<u64>";
case OPTLY_TYPE_FLOAT: return "<float>";
case OPTLY_TYPE_DOUBLE: return "<double>";
case OPTLY_TYPE_ENUM: return "<enum>";
}
return "";
}
static size_t optly__flag_print_width(OptlyFlag *flags) {
size_t max = 0;
for (OptlyFlag *flag = flags; !optly_is_flag_null(flag); flag++) {
size_t len = 0;
if (flag->shortname) {
len += 2;
}
if (flag->fullname) {
len += strlen(flag->fullname) + 3;
}
if (len > max) {
max = len;
}
}
return max;
}
static void optly__print_default_value(OptlyFlag *flag) {
if (flag->type == OPTLY_TYPE_BOOL) return;
fprintf(stderr, " (default: ");
switch (flag->type) {
case OPTLY_TYPE_CHAR: fprintf(stderr, "%c", flag->value.as_char); break;
case OPTLY_TYPE_STRING: fprintf(stderr, "%s", flag->value.as_string); break;
case OPTLY_TYPE_INT8: fprintf(stderr, "%d", flag->value.as_int8); break;
case OPTLY_TYPE_INT16: fprintf(stderr, "%d", flag->value.as_int16); break;
case OPTLY_TYPE_INT32: fprintf(stderr, "%d", flag->value.as_int32); break;
case OPTLY_TYPE_INT64: fprintf(stderr, "%lld", (long long)flag->value.as_int64); break;
case OPTLY_TYPE_UINT8: fprintf(stderr, "%u", flag->value.as_uint8); break;
case OPTLY_TYPE_UINT16: fprintf(stderr, "%u", flag->value.as_uint16); break;
case OPTLY_TYPE_UINT32: fprintf(stderr, "%u", flag->value.as_uint32); break;
case OPTLY_TYPE_UINT64: fprintf(stderr, "%llu", (unsigned long long)flag->value.as_uint64); break;
case OPTLY_TYPE_FLOAT: fprintf(stderr, "%f", flag->value.as_float); break;
case OPTLY_TYPE_DOUBLE: fprintf(stderr, "%f", flag->value.as_double); break;
default: break;
}
fprintf(stderr, ")");
}
static size_t optly__command_print_width(OptlyCommand *commands) {
size_t max = 0;
for (OptlyCommand *cmd = commands; !optly_is_command_null(cmd); cmd++) {
size_t len = strlen(cmd->name);
if (len > max) {
max = len;
}
}
return max;
}
static void optly__usage_commands_list(OptlyCommand *commands) {
if (!commands) return;
fprintf(stderr, "\nCOMMANDS\n");
size_t pad = optly__command_print_width(commands);
for (OptlyCommand *cmd = commands; !optly_is_command_null(cmd); cmd++) {
fprintf(stderr, " %-*s %s\n", (int)pad, cmd->name, cmd->description ? cmd->description : "");
}
#ifdef OPTLY_GEN_HELP_COMMAND
fprintf(stderr, " %-*s %s\n", (int)pad, "help", "Show help for command");
#endif
#ifdef OPTLY_GEN_VERSION_COMMAND
fprintf(stderr, " %-*s %s\n", (int)pad, "version", "Show app version");
#endif
}
static void optly__usage_flags(OptlyFlag *flags) {
if (!flags) return;
fprintf(stderr, "\nFLAGS\n");
size_t pad = optly__flag_print_width(flags);
for (OptlyFlag *flag = flags; !optly_is_flag_null(flag); flag++) {
char buf[OPTLY_FLAG_BUFFER_LENGTH + 16];
// const char *type = optly__flag_type_name(flag->type);
char type_buf[OPTLY_FLAG_BUFFER_LENGTH];
if (flag->type == OPTLY_TYPE_ENUM && flag->value.as_enum) {
char **vals = flag->value.as_enum;
size_t offset = 0;
offset += snprintf(type_buf + offset, sizeof(type_buf) - offset, "[");
for (char **v = vals + 1; *v; v++) {
offset += snprintf(type_buf + offset, sizeof(type_buf) - offset, "%s", *v);
if (*(v + 1)) {
offset += snprintf(type_buf + offset, sizeof(type_buf) - offset, "|");
}
}
snprintf(type_buf + offset, sizeof(type_buf) - offset, "]");
} else {
snprintf(type_buf, sizeof(type_buf), "%s", optly__flag_type_name(flag->type));
}
const char *type = type_buf;
if (flag->shortname && flag->fullname) {
snprintf(buf, sizeof(buf), "-%c --%s %s", flag->shortname, flag->fullname, type);
} else if (flag->fullname) {
snprintf(buf, sizeof(buf), "--%s %s", flag->fullname, type);
} else {
snprintf(buf, sizeof(buf), "-%c %s", flag->shortname, type);
}
fprintf(stderr, " %-*s %s", (int)pad + type_name_pad, buf, flag->description ? flag->description : "");
if (flag->required) {
fprintf(stderr, " (required)");
} else if (flag->type == OPTLY_TYPE_ENUM && flag->value.as_enum) {
if (flag->value.as_enum[0]) {
fprintf(stderr, " (default: %s)", flag->value.as_enum[0]);
}
} else if (flag->value.as_string != NULL) {
optly__print_default_value(flag);
}
fprintf(stderr, "\n");
}
#ifdef OPTLY_GEN_HELP_FLAG
fprintf(stderr, "\n %-*s Show this message\n", (int)pad + type_name_pad, "-h --help");
#endif
#ifdef OPTLY_GEN_VERSION_FLAG
fprintf(stderr, " %-*s Show version\n", (int)pad + type_name_pad, "-v --version");
#endif
}
static void optly__usage_positionals(OptlyPositional *pos) {
if (!pos) return;
fprintf(stderr, "\nPOSITIONAL ARGUMENTS\n");
for (; pos->name; pos++) {
if (pos->max == 0) {
fprintf(stderr, " %s (%zu.. values)\n", pos->name, pos->min);
} else {
fprintf(stderr, " %s (%zu..%zu values)\n", pos->name, pos->min, pos->max);
}
}
}
OPTLYDEF void optly_usage(OptlyCommand *command) {
if (command->description) {
fprintf(stderr, "%s\n\n", command->description);
}
optly__usage_signature(command);
optly__usage_commands_list(command->commands);
optly__usage_positionals(command->positionals);
optly__usage_flags(command->flags);
#ifdef OPTLY_GET_HELP_COMMAND
fprintf(stderr, "\nRun '%s help <command>' for more information.\n", command->name);
#endif
}
/**
* Check if argument matches a flag definition.
*/
static bool optly__flag_matches(const char *arg, const OptlyFlag *flag) {
bool is_short = arg[1] != '-';
return (!is_short && strcmp(arg + 2, flag->fullname) == 0) ||
(is_short && (arg[1] == flag->shortname));
}
/**
* Find a flag by argument.
*/
static OptlyFlag *optly__find_flag(const char *arg, OptlyFlag *flags) {
for (OptlyFlag *f = flags; !optly_is_flag_null(f); f++) {
if (optly__flag_matches(arg, f)) {
return f;
}
}
return NULL;
}
static void optly__flag_set_value(OptlyFlag *flag, char *value, OptlyErrors *errs) {
assert(flag);
if (flag->type != OPTLY_TYPE_BOOL && !value) {
OPTLY_LOG(FATAL, "Flag --%s requires value", flag->fullname);
optly__push_error(errs, OPTLY_ERR_MISSING_VALUE, flag->fullname);
return;
}
if (flag->type != OPTLY_TYPE_ENUM) {
flag->value.as_int64 = 0;
}
char *end = "";
switch (flag->type) {
case OPTLY_TYPE_CHAR: flag->value.as_char = *value; break;
case OPTLY_TYPE_STRING: flag->value.as_string = value; break;
case OPTLY_TYPE_INT8: flag->value.as_int8 = strtoll(value, &end, 10); break;
case OPTLY_TYPE_INT16: flag->value.as_int16 = strtoll(value, &end, 10); break;
case OPTLY_TYPE_INT32: flag->value.as_int32 = strtoll(value, &end, 10); break;
case OPTLY_TYPE_INT64: flag->value.as_int64 = strtoll(value, &end, 10); break;
case OPTLY_TYPE_UINT8: flag->value.as_uint8 = strtoull(value, &end, 10); break;
case OPTLY_TYPE_UINT16: flag->value.as_uint16 = strtoull(value, &end, 10); break;
case OPTLY_TYPE_UINT32: flag->value.as_uint32 = strtoull(value, &end, 10); break;
case OPTLY_TYPE_UINT64: flag->value.as_uint64 = strtoull(value, &end, 10); break;
case OPTLY_TYPE_FLOAT: flag->value.as_float = strtof(value, &end); break;
case OPTLY_TYPE_DOUBLE: flag->value.as_double = strtod(value, &end); break;
case OPTLY_TYPE_BOOL: flag->value.as_bool = true; break;
case OPTLY_TYPE_ENUM: {
char **vals = flag->value.as_enum;
bool valid = false;
for (char **v = vals + 1; *v; v++) {
if (strcmp(*v, value) == 0) {
valid = true;
break;
}
}
if (!valid) {
OPTLY_LOG(ERROR, "Invalid enum value '%s' for --%s", value, flag->fullname);
optly__push_error(errs, OPTLY_ERR_INVALID_VALUE, value);
return;
}
flag->value.as_enum[0] = value;
break;
}
}
if (*end != '\0') {
OPTLY_LOG(ERROR, "Argument '%s' is not a number (%s)", flag->fullname, value);
optly__push_error(errs, OPTLY_ERR_INVALID_VALUE, value);
return;
}
flag->present = true;
}
inline static bool optly__is_help_flag(char *arg) {
return strcmp(arg, "--help") == 0 ||
strcmp(arg, OPTLY_HELP_SHORT_FLAG) == 0 ||
(strlen(arg) > 2 &&
arg[0] == '-' &&
arg[1] != '-' &&
strchr(arg, OPTLY_HELP_SHORT_FLAG[1]) != NULL);
}
inline static bool optly__is_version_flag(char *arg) {
return strcmp(arg, "--version") == 0 ||
strcmp(arg, OPTLY_VERSION_SHORT_FLAG) == 0 ||
(strlen(arg) > 2 &&
arg[0] == '-' &&
arg[1] != '-' &&
strchr(arg, OPTLY_VERSION_SHORT_FLAG[1]) != NULL);
}
static void optly__parse_batch_flags(char *arg, OptlyFlag *flags, OptlyErrors *errs) {
if (strchr(arg, '=') != NULL) {
return;
}
for (char *c = &arg[1]; *c; c++) {
char sarg[3];
snprintf(sarg, sizeof(sarg), "-%c", *c);
OptlyFlag *flag = optly__find_flag(sarg, flags);
if (!flag) {
OPTLY_LOG(WARN, "Unknown short flag: %s", sarg);
optly__push_error(errs, OPTLY_ERR_UNKNOWN_FLAG, sarg);
continue;
}
if (flag->type != OPTLY_TYPE_BOOL) {
OPTLY_LOG(WARN, "cannot batch non-boolean flags (invalid flag in %s)", sarg);
optly__push_error(errs, OPTLY_ERR_BATCH_NON_BOOL, &flag->shortname);
continue;
}
flag->value.as_bool = true;
flag->present = true;
}
return;
}
static void optly__parse_long_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag *flags, OptlyErrors *errs) {
char **argv = *argv_ptr;
int argc = *argc_ptr;
if (!argv || argc <= 0 || !*argv) {
return;
}
char *arg = *argv;
char *value = NULL;
char *eq = strchr(arg, '=');
char tmp[OPTLY_FLAG_BUFFER_LENGTH];
if (eq) {
size_t len = strlen(arg);
if (len >= sizeof(tmp)) {
len = sizeof(tmp) - 1;
}
memcpy(tmp, arg, len);
tmp[len] = '\0';
char *eq2 = strchr(tmp, '=');
*eq2 = '\0';
arg = tmp;
value = eq + 1;
}
OptlyFlag *flag = optly__find_flag(arg, flags);
if (!flag) {
OPTLY_LOG(WARN, "Unknown flag: %s", arg);
// NOTE: We can't save arg for later because it can point to local tmp (if arg was in form --flag=value)
optly__push_error(errs, OPTLY_ERR_UNKNOWN_FLAG, *argv);
return;
}
flag->present = true;
if (!value && flag->type != OPTLY_TYPE_BOOL && argv[1] && argv[1][0] != '-') {
value = argv[1];
SHIFT_ARG(argv, argc);
}
optly__flag_set_value(flag, value, errs);
*argv_ptr = argv;
*argc_ptr = argc;
}
/**
* Parse flags from argv.
*/
static void optly__parse_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag *flags, OptlyErrors *errs) {
char **argv = *argv_ptr;
int argc = *argc_ptr;
if (!argv || argc <= 0 || !*argv) {
return;
}
char *arg = *argv;
bool is_batch_short = (arg[0] == '-' && arg[1] != '-' && strlen(arg) > 2) && arg[2] != '=';
if (is_batch_short) {
optly__parse_batch_flags(arg, flags, errs);
} else {
optly__parse_long_flags(argv_ptr, argc_ptr, flags, errs);
}
}
/**
* Parse a command from argv.
*/
static OptlyCommand *optly__parse_command(const char *arg, OptlyCommand *commands) {