Browse Source

option: add function to format a list of short and long options together with their respective descriptions

S.J.R. van Schaik 4 years ago
parent
commit
d2d2adacec
3 changed files with 25 additions and 0 deletions
  1. 1 0
      Makefile
  2. 9 0
      include/option.h
  3. 15 0
      source/option.c

+ 1 - 0
Makefile

@@ -14,6 +14,7 @@ obj-y += source/file.o
14 14
 obj-y += source/image.o
15 15
 obj-y += source/info.o
16 16
 obj-y += source/main.o
17
+obj-y += source/option.o
17 18
 obj-y += source/pack.o
18 19
 obj-y += source/sign.o
19 20
 obj-y += source/unpack.o

+ 9 - 0
include/option.h

@@ -0,0 +1,9 @@
1
+#pragma once
2
+
3
+struct opt_desc {
4
+	const char *short_name;
5
+	const char *long_name;
6
+	const char *desc;
7
+};
8
+
9
+void format_options(struct opt_desc *entries);

+ 15 - 0
source/option.c

@@ -0,0 +1,15 @@
1
+#include <stdio.h>
2
+
3
+#include <option.h>
4
+
5
+void format_options(struct opt_desc *entries)
6
+{
7
+	struct opt_desc *entry;
8
+
9
+	for (entry = entries; entry->long_name; ++entry) {
10
+		fprintf(stderr, "%7s %-16s %s\n",
11
+			entry->short_name ? entry->short_name : "",
12
+			entry->long_name,
13
+			entry->desc);
14
+	}
15
+}