18 size_t len = strlen(prefix);
19 return strncmp(str, prefix, len) ? NULL : str + len;
25 for (;; str++, prefix++)
28 else if (*str != *prefix)
29 return (
unsigned char)*prefix - (
unsigned char)*str;
36 if (!strncmp(This->
argv[0],
"--", 2)) {
37 fprintf(stderr,
"error: option `%s` %s\n", opt->
long_name, reason);
40 fprintf(stderr,
"error: option `%c` %s\n", opt->
short_name, reason);
59 if (*(
int *)opt->
value < 0) {
60 *(
int *)opt->
value = 0;
64 if (flags & OPT_UNSET) {
65 *(
int *)opt->
value &= (
int)~(1 << opt->
data);
67 *(
int *)opt->
value |= (
int)(1 << opt->
data);
71 if (flags & OPT_UNSET) {
72 *(uint64_t *)opt->
value &= (uint64_t)~(1 << opt->
data);
74 *(uint64_t *)opt->
value |= (uint64_t)(1 << opt->
data);
81 }
else if (This->
argc > 1) {
83 *(
const char **)opt->
value = *++This->
argv;
90 *(
int *)opt->
value = (
int)strtol(This->
optvalue, (
char **)&s, 0);
92 }
else if (This->
argc > 1) {
94 *(
int *)opt->
value = (
int)strtol(*++This->
argv, (
char **)&s, 0);
105 *(uint64_t *)opt->
value = (uint64_t)_strtoui64(This->
optvalue, (
char **)&s, 0);
107 *(uint64_t *)opt->
value = (uint64_t)strtoull(This->
optvalue, (
char **)&s, 0);
110 }
else if (This->
argc > 1) {
113 *(uint64_t *)opt->
value = (uint64_t)_strtoui64(*++This->
argv, (
char **)&s, 0);
115 *(uint64_t *)opt->
value = (uint64_t)strtoull(*++This->
argv, (
char **)&s, 0);
140 switch (options->
type) {
150 fprintf(stderr,
"wrong option type: %d", options->
type);
209 const char *
const *
usage,
int flags)
211 memset(This, 0,
sizeof(*This));
221 This->
argc = argc - 1;
222 This->
argv = argv + 1;
228 const char *arg = This->
argv[0];
229 if (arg[0] !=
'-' || !arg[1]) {
272 fprintf(stderr,
"error: unknown option `%s`\n", This->
argv[0]);
279 This->
argc *
sizeof(*This->
out));
291 size_t usage_opts_width = 0;
294 fprintf(stdout,
"%s\n", *This->
usage++);
313 len += strlen(
" <int>");
315 len += strlen(
" <int64>");
317 len += strlen(
" <str>");
320 len = (size_t)ceil((
float)len / 4) * 4;
321 if (usage_opts_width < len) {
322 usage_opts_width = len;
325 usage_opts_width += 4;
335 pos = fprintf(stdout,
" ");
337 pos += fprintf(stdout,
"-%c", options->
short_name);
340 pos += fprintf(stdout,
", ");
343 pos += fprintf(stdout,
"--%s", options->
long_name);
346 pos += fprintf(stdout,
" <%s>", options->
name_value);
350 pos += fprintf(stdout,
" <int>");
352 pos += fprintf(stdout,
" <int64>");
354 pos += fprintf(stdout,
" <str>");
357 if (pos <= usage_opts_width) {
358 pad = (int)(usage_opts_width - pos);
361 pad = (int)usage_opts_width;
364 fprintf(stdout,
"%*s%s\n", pad + 2,
"", options->
help);
367 size_t l = strlen(options->
help);
369 fprintf(stdout,
"%*s: ", pad,
"");
370 for (i = 0; i < l; i++)
372 fputc(options->
help[i], stdout);
373 if (options->
help[i] ==
'\n') {
375 fprintf(stdout,
"%*s", (
int)(usage_opts_width + 2),
"");
383 fprintf(stdout,
"%s\n", *This->
usage++);