aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README3
-rw-r--r--include/errors.h3
-rw-r--r--lib/errors/usage.c26
3 files changed, 31 insertions, 1 deletions
diff --git a/README b/README
index 51585b3..0df0068 100644
--- a/README
+++ b/README
@@ -77,6 +77,7 @@ FEATURES:
• Functions for getting and setting the program name
• Functions for printing diagnostics and optionally crashing with
strerror() support
+ • Generate usage strings
• macros.h
• MIN()/MAX()/CLAMP()
• Better assertion macro
@@ -112,7 +113,7 @@ FEATURES:
PLANNED FEATURES:
- • String Case Conversions (unicode/string.h)
+ • Titlecase Conversions (unicode/string.h)
• Unicode Normalization (unicode/string.h)
• Line- and Sentence Segmentation (unicode/string.h)
diff --git a/include/errors.h b/include/errors.h
index 961ecf1..c8576f0 100644
--- a/include/errors.h
+++ b/include/errors.h
@@ -5,6 +5,9 @@
#include "_attrs.h"
+void usage(const char *, ...);
+#define usage(...) usage(__VA_ARGS__, nullptr)
+
[[gnu::format(printf, 1, 2)]] void warn(const char *, ...);
void vwarn(const char *, va_list);
diff --git a/lib/errors/usage.c b/lib/errors/usage.c
new file mode 100644
index 0000000..50ca04f
--- /dev/null
+++ b/lib/errors/usage.c
@@ -0,0 +1,26 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "errors.h"
+#include "macros.h"
+
+void
+(usage)(const char *s, ...)
+{
+ ASSUME(s != nullptr);
+ ASSUME(mlib_progname() != nullptr);
+
+ va_list ap;
+ va_start(ap, s);
+
+ fprintf(stderr, "Usage: %s %s\n", mlib_progname(), s);
+
+ while ((s = va_arg(ap, char *)) != nullptr) {
+ for (size_t i = 0; i < lengthof("Usage: ") - 1; i++)
+ fputc(' ', stderr);
+ fprintf(stderr, "%s %s\n", mlib_progname(), s);
+ }
+
+ va_end(ap);
+}