summaryrefslogtreecommitdiff
path: root/include/libHX/option.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/libHX/option.h')
-rw-r--r--include/libHX/option.h90
1 files changed, 63 insertions, 27 deletions
diff --git a/include/libHX/option.h b/include/libHX/option.h
index ef9c626..9eff6a9 100644
--- a/include/libHX/option.h
+++ b/include/libHX/option.h
@@ -28,12 +28,9 @@ extern struct HXformat_map *HXformat_init(void);
extern void HXformat_free(struct HXformat_map *);
extern int HXformat_add(struct HXformat_map *, const char *, const void *,
unsigned int);
-#define HXformat_aprintf(a, b, c) HXformat3_aprintf((a), (b), (c))
-#define HXformat_fprintf(a, b, c) HXformat3_fprintf((a), (b), (c))
-#define HXformat_sprintf(a, b, c, d) HXformat3_sprintf((a), (b), (c), (d))
-extern ssize_t HXformat3_aprintf(const struct HXformat_map *, hxmc_t **, const char *);
-extern ssize_t HXformat3_fprintf(const struct HXformat_map *, FILE *, const char *);
-extern ssize_t HXformat3_sprintf(const struct HXformat_map *, char *, size_t, const char *);
+extern ssize_t HXformat_aprintf(const struct HXformat_map *, hxmc_t **, const char *);
+extern ssize_t HXformat_fprintf(const struct HXformat_map *, FILE *, const char *);
+extern ssize_t HXformat_sprintf(const struct HXformat_map *, char *, size_t, const char *);
/*
* OPT.C
@@ -60,7 +57,7 @@ extern ssize_t HXformat3_sprintf(const struct HXformat_map *, char *, size_t, co
* %HXTYPE_FLOAT: [fo] (float *) Read a floating point number
* %HXTYPE_DOUBLE: [fo] (double *) Read a floating point number
* %HXTYPE_STRING: [fo] (char **) Any string.
- * %HXTYPE_STRP: [f-] (const char *const *) A string.
+ * %HXTYPE_STRP: [fo] (const char *const *) A string.
* %HXTYPE_STRDQ: [-o] (struct HXdeque *) A string.
* %HXTYPE_UINT8: [-o] (uint8_t *) An integer.
* %HXTYPE_UINT16: [-o] (uint8_t *) An integer.
@@ -135,24 +132,32 @@ enum {
};
/**
- * Flags (4th arg) to HX_getopt.
- * %HXOPT_PTHRU: pass-through unknown options to new argv
- * %HXOPT_DESTROY_OLD: destroy old argv after parsing is successful
+ * Flags to HX_getopt.
* %HXOPT_QUIET: do not output any warnings to stderr
* %HXOPT_HELPONERR: print out help when a parsing error occurs
* %HXOPT_USAGEONERR: print out short usage when a parsing error occurs
- * %HXOPT_RQ_ORDER: require option order/POSIX mode:
- * first non-option terminates option processing
- * %HXOPT_KEEP_ARGV: do not replace argc/argv at all
+ * %HXOPT_RQ_ORDER: Options and non-options must not be mixed (first
+ * non-option stops parsing) and the environment variable
+ * POSIXLY_CORRECT is ignored.
+ * %HXOPT_ANY_ORDER: Options and non-options may be mixed and the
+ * environment variable POSIXLY_CORRECT is ignored.
+ * %HXOPT_CONST_INPUT: User declaration that elements in input argv can NOT
+ * be reordered by the parser.
+ * %HXOPT_ITER_OPTS: (HX_getopt6 only) Populate result.desc and .oarg.
+ * %HXOPT_ITER_ARGS: (HX_getopt6 only) Populate result.uarg.
+ * %HXOPT_DUP_ARGS: (HX_getopt6 only) Populate result.dup_argv.
*/
enum {
- HXOPT_PTHRU = 1 << 0,
- HXOPT_DESTROY_OLD = 1 << 1,
- HXOPT_QUIET = 1 << 2,
- HXOPT_HELPONERR = 1 << 3,
- HXOPT_USAGEONERR = 1 << 4,
- HXOPT_RQ_ORDER = 1 << 5,
- HXOPT_KEEP_ARGV = 1 << 6,
+ HXOPT_QUIET = 0x4U,
+ HXOPT_HELPONERR = 0x8U,
+ HXOPT_USAGEONERR = 0x10U,
+ HXOPT_RQ_ORDER = 0x20U,
+ HXOPT_ANY_ORDER = 0x80U,
+ HXOPT_CONST_INPUT = 0x100U,
+ HXOPT_ITER_OPTS = 0x200U,
+ HXOPT_ITER_ARGS = 0x400U,
+ HXOPT_ITER_OA = HXOPT_ITER_OPTS | HXOPT_ITER_ARGS,
+ HXOPT_DUP_ARGS = 0x800U,
};
/**
@@ -162,6 +167,7 @@ enum {
* %HXOPT_ERR_VOID: long option takes no value
* %HXOPT_ERR_MIS: option requires a value argument
* %HXOPT_ERR_AMBIG: long option abbreviation was ambiguous
+ * %HXOPT_ERR_FLAGS: illegal flag combination (API misuse)
*/
enum {
HXOPT_ERR_SUCCESS = 0,
@@ -169,6 +175,7 @@ enum {
HXOPT_ERR_VOID,
HXOPT_ERR_MIS,
HXOPT_ERR_AMBIG,
+ HXOPT_ERR_FLAGS,
};
/**
@@ -229,13 +236,28 @@ struct HXoption {
const char *help, *htyp;
};
-#ifndef LIBHX_ZVECFREE_DECLARATION
-#define LIBHX_ZVECFREE_DECLARATION
-extern void HX_zvecfree(char **);
-#endif
-extern int HX_getopt(const struct HXoption *, int *, char ***, unsigned int);
-extern int HX_getopt5(const struct HXoption *, char **argv, int *nargc, char ***nargv, unsigned int flags);
-#define HX_getopt(a, b, c, d) HX_getopt((a), (b), const_cast3(char ***, (c)), (d))
+/**
+ * @nopts: Number of options found.
+ * @nargs: Number of non-option arguments found.
+ * @desc: Under %HXOPT_ITER_OPTS, filled with pointers to options, else %nullptr.
+ * @oarg: Under %HXOPT_ITER_OPTS, filled with pointers to optargs, else %nullptr.
+ * @uarg: Under %HXOPT_ITER_ARGS, filled with pointers to non-opt args
+ * (always orig_argv, never dup_argv), else %nullptr.
+ * @dup_argc: String count for dup_argv.
+ * @dup_argv: Filled with copies of non-option arguments if %HXOPT_DUP_ARGS.
+ * dup_argv[0] will be the program name (useful for feeding to
+ * another HX_getopt6 call).
+ */
+struct HXopt6_result {
+ int nopts, nargs;
+ const struct HXoption **desc;
+ char **oarg, **uarg;
+ int dup_argc;
+ char **dup_argv;
+};
+
+extern int HX_getopt6(const struct HXoption *, int, char **argv, struct HXopt6_result *, unsigned int flags);
+extern void HX_getopt6_clean(struct HXopt6_result *);
extern void HX_getopt_help(const struct HXoptcb *, FILE *);
extern void HX_getopt_help_cb(const struct HXoptcb *);
extern void HX_getopt_usage(const struct HXoptcb *, FILE *);
@@ -265,6 +287,20 @@ extern void HX_shconfig_free(const struct HXoption *);
{"usage", 0, HXTYPE_NONE, NULL, NULL, HX_getopt_usage_cb, \
0, "Display brief usage message"}
# define HXOPT_TABLEEND {NULL, 0, HXTYPE_XSNTMARK}
+
+struct HXopt6_auto_result : public HXopt6_result {
+ HXopt6_auto_result() : HXopt6_result() {}
+ ~HXopt6_auto_result() { HX_getopt6_clean(this); }
+ /*
+ * Unlike the unique_tie class, no operator~ is provided for
+ * HXopt6_auto_result, because reusing a result is not outright
+ * possible;
+ * auto ret = HX_getopt6(&table, argc, argv, &result, HXOPT_ITER_ARGS);
+ * ret = HX_getopt6(&table, result.nargs, result.uarg, &~result, HXOPT_ITER_ARGS):
+ * would kill off result before nargs/uarg is loaded.
+ */
+};
+
#endif
#endif /* _LIBHX_OPTION_H */