commit df45a90f0cd064391bce5df79a67d6af7815eb64
Author: afiw <afiw@hopeserv.net>
Date: Sun, 22 Mar 2026 03:44:00 +0100
Initial commit
Diffstat:
| A | Makefile | | | 25 | +++++++++++++++++++++++++ |
| A | arg.h | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | example.c | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 128 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,24 @@
+.POSIX:
+.SUFFIXES:
+
+CC=cc
+STDCFLAGS=-std=c23
+WCFLAGS=-Wall -Wextra -Wpedantic -Werror
+
+PROG=example
+OBJS=example.o
+
+all: ${PROG}
+
+${PROG}: ${OBJS}
+ ${CC} ${CFLAGS} ${LDFLAGS} ${STDCFLAGS} -o ${PROG} ${OBJS}
+
+.SUFFIXES: .c .o
+
+.c.o:
+ ${CC} ${CFLAGS} ${CPPFLAGS} ${STDCFLAGS} ${WCFLAGS} -c -o $@ $<
+
+example.o: arg.h
+
+clean:
+ rm -f ${PROG} ${OBJS}
+\ No newline at end of file
diff --git a/arg.h b/arg.h
@@ -0,0 +1,53 @@
+/*
+ * MIT/Expat license
+ *
+ * Copyright © 2021 Plan 9 Foundation
+ * Copyright © 2026 9front authors
+ * Copyright © 2026 afiw
+ *
+ * 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.
+ */
+
+extern char *argv0;
+
+#define ARGBEGIN for(argv0 = argv[0], argv++, argc--;\
+ argv[0] != nullptr && argv[0][0] == '-' && argv[0][1] != '\0';\
+ argc--, argv++){\
+ char *argh__s = &argv[0][1];\
+ [[maybe_unused]] char *argh__t;\
+ char argh__c = '\0';\
+ if(argh__s[0] == '-' && argh__s[1] == '\0'){\
+ argc--;\
+ argv++;\
+ break;\
+ }\
+ while((argh__c = argh__s[0]) != '\0'){\
+ argh__s++;\
+ switch(argh__c)
+
+#define ARGEND }\
+ }
+
+#define ARGF() (argh__t = argh__s, argh__s = "",\
+ (argh__t[0] != '\0' ? argh__t : argv[1] != nullptr ? (argc--, *++argv) : nullptr))
+
+#define EARGF() (argh__t = argh__s, argh__s = "",\
+ (argh__t[0] != '\0' ? argh__t : argv[1] != nullptr ? (argc--, *++argv) : ((x), abort())))
+
+#define ARGC() (argh__c)
diff --git a/example.c b/example.c
@@ -0,0 +1,49 @@
+/*
+ * MIT/Expat license
+ *
+ * Copyright © 2026 afiw
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+
+#include "arg.h"
+
+char *argv0;
+
+int
+main(int argc, char **argv)
+{
+ ARGBEGIN{
+ case 'a':
+ printf("got -a\n");
+ break;
+ case 'b':
+ printf("got -b\n");
+ break;
+ case 'c':
+ printf("got -c with argument %s\n", ARGF());
+ break;
+ default:
+ printf("got unknown flag %c\n", ARGC());
+ abort();
+ }ARGEND
+}
+\ No newline at end of file