1
0
Fork 0

turn readopt into a single-header library

This commit is contained in:
Lukas Wurzinger 2022-09-25 12:40:12 +02:00
parent 0650e1ddb9
commit 3ecc2317ab
11 changed files with 600 additions and 759 deletions

View file

@ -1,23 +0,0 @@
CC = cc
LDFLAGS = -lreadopt
MACROS = -D_POSIX_C_SOURCE=200809L
CFLAGS = $(MACROS) --std=c99 -O2 -g -Wall -Wextra -Wpedantic
SOURCES = test.c
OBJECTS = $(SOURCES:.c=.o)
RM = rm -f --
CP = cp --
all: test
%.o: %.c
$(CC) $(CFLAGS) -c $<
test: $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
clean:
$(RM) $(OBJECTS)
.PHONY: clean

View file

@ -1,4 +1,4 @@
# vim: set tabstop=2 shiftwidth=2 expandtab :
# vim:set tabstop=2 shiftwidth=2 expandtab:
include config.ninja

View file

@ -1,9 +1,9 @@
# vim: set tabstop=2 shiftwidth=2 expandtab :
# vim:set tabstop=2 shiftwidth=2 expandtab:
cc = cc
macros = -D _POSIX_C_SOURCE=200809L -D NDEBUG
cflags = --std=c99 -Wall -Wextra -Wpedantic -g $macros -O2
macros = -D NDEBUG
cflags = -std=c99 -Wall -Wextra -Wpedantic -g $macros -O2
ldflags =
ldlibs = -lreadopt
ldlibs =
target = ./test

View file

@ -1,200 +1,163 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#define READOPT_IMPLEMENTATION
#include <readopt.h>
#include "../readopt.h"
int
main(int argc, char **argv)
int main(int argc, char **argv)
{
if (!*argv)
return EXIT_FAILURE;
struct readopt_opt opts[] = {
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("e", "x"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("expr", "expression"),
},
.cont = {
.req = 1,
.oper.bounds.val = {
1,
4,
},
},
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("c"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("config"),
},
.cont = {
.req = 1,
.oper = {
.name = "file",
.bounds.val = {
2,
},
},
},
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("i"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("uri"),
},
.cont = {
.req = 1,
.oper.bounds.inf = 1,
},
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("b"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("backup", "backup-file"),
},
.cont = {
.req = 1,
.oper.bounds.inf = 1,
},
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("v"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("verbose"),
},
.cont.oper.bounds.val = {
3,
},
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("s"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("sort"),
},
.cont.oper.bounds.inf = 1,
},
{
.names = {
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("help"),
},
.cont.oper.bounds.val = {
1,
},
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("V"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("version"),
},
.cont.oper.bounds.val = {
1,
},
},
{0},
};
struct readopt_opt opts[] = {
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("e", "x"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("expr", "expression")
},
.cont = {
.req = 1,
.oper.bounds.val = {1, 4}
}
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("c"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("config")
},
.cont = {
.req = 1,
.oper = {
.name = "file",
.bounds.val = {2},
}
}
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("i"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("uri")
},
.cont = {
.req = 1,
.oper.bounds.inf = 1
}
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("b"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("backup", "backup-file")
},
.cont = {
.req = 1,
.oper.bounds.inf = 1
}
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("v"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("verbose")
},
.cont.oper.bounds.val = {3}
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("s"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("sort")
},
.cont.oper.bounds.inf = 1
},
{
.names = {
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("help")
},
.cont.oper.bounds.val = {1}
},
{
.names = {
[READOPT_FORM_SHORT] = READOPT_ALLOC_STRINGS("V"),
[READOPT_FORM_LONG] = READOPT_ALLOC_STRINGS("version")
},
.cont.oper.bounds.val = {1}
},
{0}
};
struct readopt_oper opers[] = {
{.name = "pattern",
.bounds.inf = 1},
{.name = "file",
.bounds = {
.val = {1},
.inf = 1}},
{.name = "name", .bounds = {.val = {1}, .inf = 1}},
{0}};
struct readopt_oper opers[] = {
{
.name = "pattern",
.bounds.inf = 1
},
{
.name = "file",
.bounds = {
.val = {1},
.inf = 1
}
},
{
.name = "name",
.bounds = {
.val = {1},
.inf = 1
}
},
{0}
};
struct readopt_parser rp;
readopt_parser_init(
&rp,
opts,
opers,
(struct readopt_view_strings){
.strings = (const char **)argv + 1,
.len = argc - 1});
struct readopt_parser rp;
readopt_parser_init(
&rp,
opts,
opers,
(struct readopt_view_strings){
.strings = (const char **)argv + 1,
.len = argc - 1
}
);
while (readopt_parse(&rp))
;
while (readopt_parse(&rp));
fprintf(stderr, "error: %d\n", rp.error);
if (rp.error != READOPT_ERROR_SUCCESS)
{
return 1;
}
fputs("error: ", stderr);
readopt_put_error(rp.error, &(struct readopt_write_context){
.dest.stream = stderr
});
fputc('\n', stderr);
if (rp.error != READOPT_ERROR_SUCCESS) {
return EXIT_FAILURE;
}
printf("opt:\n");
{
struct readopt_opt *curr = rp.opts;
for (size_t i = 0; readopt_validate_opt(curr + i); i++)
{
for (size_t j = 0; j < sizeof curr[i].names / sizeof *curr[i].names; j++)
{
if (curr[i].names[j])
{
for (size_t k = 0; curr[i].names[j][k]; k++)
{
printf("%s ", curr[i].names[j][k]);
}
}
}
printf("{ [%zu] ", curr[i].cont.oper.val.len);
if (curr[i].cont.req)
{
struct readopt_view_strings val = curr[i].cont.oper.val;
for (size_t j = 0; j < val.len; j++)
{
printf("%s ", val.strings[j]);
}
}
printf("}\n");
}
}
printf("opt:\n");
{
struct readopt_opt *curr = rp.opts;
for (size_t i = 0; readopt_validate_opt(curr + i); i++) {
for (size_t j = 0; j < sizeof curr[i].names / sizeof *curr[i].names; j++) {
if (curr[i].names[j]) {
for (size_t k = 0; curr[i].names[j][k]; k++) {
printf("%s ", curr[i].names[j][k]);
}
}
}
printf("{ [%zu] ", curr[i].cont.oper.val.len);
if (curr[i].cont.req) {
struct readopt_view_strings val = curr[i].cont.oper.val;
for (size_t j = 0; j < val.len; j++) {
printf("%s ", val.strings[j]);
}
}
printf("}\n");
}
}
printf("oper:\n");
{
struct readopt_oper *curr = rp.opers;
for (size_t i = 0; readopt_validate_oper(curr + i); i++)
{
printf("%s { [%zu] ", curr[i].name, curr[i].val.len);
for (size_t j = 0; j < curr[i].val.len; j++)
{
printf("%s ", curr[i].val.strings[j]);
}
printf("}\n");
}
}
printf("oper:\n");
{
struct readopt_oper *curr = rp.opers;
for (size_t i = 0; readopt_validate_oper(curr + i); i++) {
printf("%s { [%zu] ", curr[i].name, curr[i].val.len);
for (size_t j = 0; j < curr[i].val.len; j++) {
printf("%s ", curr[i].val.strings[j]);
}
printf("}\n");
}
}
printf("usage (plain) (stream):\n");
readopt_put_usage(&rp, &(struct readopt_format_context){
.fmt = READOPT_FORMAT_PLAIN,
.wr = &(struct readopt_write_context){
.dest.stream = stdout,
}
});
printf("\nusage (mdoc) (stream):\n");
readopt_put_usage(&rp, &(struct readopt_format_context){
.fmt = READOPT_FORMAT_MDOC,
.wr = &(struct readopt_write_context){
.dest.stream = stdout,
}
});
size_t sz = 0;
char *buf = NULL;
/* alternatively, use fmemopen and increase the buffer size via the callback */
FILE *stream = open_memstream(&buf, &sz);
readopt_put_usage(&rp, &(struct readopt_format_context){
.fmt = READOPT_FORMAT_MDOC,
.wr = &(struct readopt_write_context){
.dest = {
.stream = stream,
.size = SIZE_MAX
}
}
});
fflush(stream);
printf("usage (mdoc) (buffer):\n%s", buf);
fclose(stream);
free(buf);
return 0;
return 0;
}