summaryrefslogtreecommitdiffstats
path: root/util/argconfig.h
diff options
context:
space:
mode:
Diffstat (limited to 'util/argconfig.h')
-rw-r--r--util/argconfig.h130
1 files changed, 86 insertions, 44 deletions
diff --git a/util/argconfig.h b/util/argconfig.h
index 81eaaf4..eaf8375 100644
--- a/util/argconfig.h
+++ b/util/argconfig.h
@@ -39,6 +39,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdbool.h>
+#include <stdint.h>
enum argconfig_types {
CFG_FLAG,
@@ -53,13 +54,6 @@ enum argconfig_types {
CFG_SHORT,
CFG_POSITIVE,
CFG_INCREMENT,
- CFG_SUBOPTS,
- CFG_FILE_A,
- CFG_FILE_W,
- CFG_FILE_R,
- CFG_FILE_AP,
- CFG_FILE_WP,
- CFG_FILE_RP,
};
#define OPT_ARGS(n) \
@@ -67,40 +61,96 @@ enum argconfig_types {
#define OPT_END() { NULL }
-#define OPT_FLAG(l, s, v, d) \
- {l, s, NULL, CFG_FLAG, v, no_argument, d}
+#define OPT_FLAG(l, s, v, d, ...) \
+ {l, s, NULL, CFG_FLAG, v, no_argument, d, false, __VA_ARGS__}
-#define OPT_SUFFIX(l, s, v, d) \
- {l, s, "IONUM", CFG_LONG_SUFFIX, v, required_argument, d}
+#define OPT_SUFFIX(l, s, v, d, ...) \
+ {l, s, "IONUM", CFG_LONG_SUFFIX, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_UINT(l, s, v, d) \
- {l, s, "NUM", CFG_POSITIVE, v, required_argument, d}
+#define OPT_UINT(l, s, v, d, ...) \
+ {l, s, "NUM", CFG_POSITIVE, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_INT(l, s, v, d) \
- {l, s, "NUM", CFG_INT, v, required_argument, d}
+#define OPT_INT(l, s, v, d, ...) \
+ {l, s, "NUM", CFG_INT, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_LONG(l, s, v, d) \
- {l, s, "NUM", CFG_LONG, v, required_argument, d}
+#define OPT_LONG(l, s, v, d, ...) \
+ {l, s, "NUM", CFG_LONG, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_DOUBLE(l, s, v, d) \
- {l, s, "NUM", CFG_DOUBLE, v, required_argument, d}
+#define OPT_DOUBLE(l, s, v, d, ...) \
+ {l, s, "NUM", CFG_DOUBLE, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_BYTE(l, s, v, d) \
- {l, s, "NUM", CFG_BYTE, v, required_argument, d}
+#define OPT_BYTE(l, s, v, d, ...) \
+ {l, s, "NUM", CFG_BYTE, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_SHRT(l, s, v, d) \
- {l, s, "NUM", CFG_SHORT, v, required_argument, d}
+#define OPT_SHRT(l, s, v, d, ...) \
+ {l, s, "NUM", CFG_SHORT, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_INCR(l, s, v, d) \
- {l, s, "NUM", CFG_INCREMENT, v, no_argument, d}
+#define OPT_INCR(l, s, v, d, ...) \
+ {l, s, "NUM", CFG_INCREMENT, v, no_argument, d, false, __VA_ARGS__}
-#define OPT_STRING(l, s, m, v, d) \
- {l, s, m, CFG_STRING, v, required_argument, d}
+#define OPT_STRING(l, s, m, v, d, ...) \
+ {l, s, m, CFG_STRING, v, required_argument, d, false, __VA_ARGS__}
-#define OPT_FMT(l, s, v, d) OPT_STRING(l, s, "FMT", v, d)
-#define OPT_FILE(l, s, v, d) OPT_STRING(l, s, "FILE", v, d)
-#define OPT_LIST(l, s, v, d) OPT_STRING(l, s, "LIST", v, d)
-#define OPT_STR(l, s, v, d) OPT_STRING(l, s, "STRING", v, d)
+#define OPT_FMT(l, s, v, d, ...) OPT_STRING(l, s, "FMT", v, d, __VA_ARGS__)
+#define OPT_FILE(l, s, v, d, ...) OPT_STRING(l, s, "FILE", v, d, __VA_ARGS__)
+#define OPT_LIST(l, s, v, d, ...) OPT_STRING(l, s, "LIST", v, d, __VA_ARGS__)
+#define OPT_STR(l, s, v, d, ...) OPT_STRING(l, s, "STRING", v, d, __VA_ARGS__)
+
+#define OPT_VALS(n) \
+ struct argconfig_opt_val n[]
+
+#define VAL_END() { NULL }
+
+#define VAL_FLAG(s, l, v) \
+ {s, CFG_FLAG, .val.flag = v}
+
+#define VAL_LONG_SUFFIX(s, v) \
+ {s, CFG_LONG_SUFFIX, .val.long_suffix = v}
+
+#define VAL_UINT(s, v) \
+ {s, CFG_POSITIVE, v}
+
+#define VAL_INT(s, v) \
+ {s, CFG_INT, .val.int_val = v}
+
+#define VAL_LONG(s, v) \
+ {s, CFG_LONG, .val.long_val = v}
+
+#define VAL_DOUBLE(s, v) \
+ {s, CFG_DOUBLE, .val.double_val = v}
+
+#define VAL_BYTE(s, v) \
+ {s, CFG_BYTE, .val.byte = v}
+
+#define VAL_SHRT(s, v) \
+ {s, CFG_SHORT, .val.short_val = v}
+
+#define VAL_INCR(s, v) \
+ {s, CFG_INCREMENT, .val.increment = v}
+
+#define VAL_STRING(s, m, v) \
+ {s, CFG_STRING, .val.string = v}
+
+union argconfig_val {
+ char *string;
+ size_t size;
+ int int_val;
+ int bool_val;
+ uint8_t byte;
+ uint16_t short_val;
+ uint32_t positive;
+ int increment;
+ unsigned long long_val;
+ uint64_t long_suffix;
+ double double_val;
+ bool flag;
+};
+
+struct argconfig_opt_val {
+ const char *str;
+ enum argconfig_types type;
+ union argconfig_val val;
+};
struct argconfig_commandline_options {
const char *option;
@@ -111,28 +161,20 @@ struct argconfig_commandline_options {
int argument_type;
const char *help;
bool seen;
+ struct argconfig_opt_val *opt_val;
};
-#define CFG_MAX_SUBOPTS 500
-#define MAX_HELP_FUNC 20
-
-typedef void argconfig_help_func();
void argconfig_append_usage(const char *str);
void argconfig_print_help(const char *program_desc,
struct argconfig_commandline_options *options);
int argconfig_parse(int argc, char *argv[], const char *program_desc,
struct argconfig_commandline_options *options);
-int argconfig_parse_subopt_string(char *string, char **options,
- size_t max_options);
-int argconfig_parse_comma_sep_array(char *string, int *ret,
- unsigned max_length);
+int argconfig_parse_comma_sep_array(char *string, int *ret, unsigned int max_length);
int argconfig_parse_comma_sep_array_short(char *string, unsigned short *ret,
- unsigned max_length);
-int argconfig_parse_comma_sep_array_long(char *string,
- unsigned long long *ret,
- unsigned max_length);
+ unsigned int max_length);
+int argconfig_parse_comma_sep_array_long(char *string, unsigned long long *ret,
+ unsigned int max_length);
int argconfig_parse_byte(const char *opt, const char *str, unsigned char *val);
-void argconfig_register_help_func(argconfig_help_func * f);
void print_word_wrapped(const char *s, int indent, int start, FILE *stream);
bool argconfig_parse_seen(struct argconfig_commandline_options *options,