about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-10 13:49:57 -0500
committerJune McEnroe <june@causal.agency>2021-01-15 15:59:31 -0500
commit5d963809faa7b3c5c91dad5a29df8d8e6b1f5d09 (patch)
treebdd6a17f16d1932d0297458a6f91d68bc2293f0a
parentColor contexts like catgirl (diff)
downloadlitterbox-5d963809faa7b3c5c91dad5a29df8d8e6b1f5d09.tar.gz
litterbox-5d963809faa7b3c5c91dad5a29df8d8e6b1f5d09.zip
Allow interspersing flags and config files 1.7
Don't wait for getopt_long to move all the arguments to the end. This
allows overriding options set by config files by placing flags after
them on the command line.
-rw-r--r--config.c20
1 files changed, 8 insertions, 12 deletions
diff --git a/config.c b/config.c
index b8b4efa..861bf88 100644
--- a/config.c
+++ b/config.c
@@ -56,21 +56,17 @@ int getopt_config(
 	const char *optstring, const struct option *longopts, int *longindex
 ) {
 	static int opt;
-	if (opt >= 0) {
-		opt = getopt_long(argc, argv, optstring, longopts, longindex);
-	}
-	if (opt >= 0) return opt;
-
 	for (;;) {
 		if (!file) {
-			if (optind < argc) {
-				num = 0;
-				path = argv[optind++];
-				file = configOpen(path, "r");
-				if (!file) return clean('?');
-			} else {
-				return clean(-1);
+			if (optind == argc) return clean(-1);
+			if (opt >= 0 && argv[optind][0] == '-') {
+				opt = getopt_long(argc, argv, optstring, longopts, longindex);
+				if (opt >= 0 || optind == argc) return clean(opt);
 			}
+			num = 0;
+			path = argv[optind++];
+			file = configOpen(path, "r");
+			if (!file) return clean('?');
 		}
 
 		for (;;) {