callback data to pass */
#define POPT_ARG_MASK 0x0000FFFF
#define POPT_ARGFLAG_ONEDASH 0x80000000 /* allow -longoption */
+#define POPT_ARGFLAG_DOC_HIDDEN 0x40000000 /* don't show in help/usage */
+#define POPT_CBFLAG_PRE 0x80000000 /* call the callback before parse */
+#define POPT_CBFLAG_POST 0x40000000 /* call the callback after parse */
#define POPT_ERROR_NOARG -10
#define POPT_ERROR_BADOPT -11
typedef struct poptOption * poptOption;
#define POPT_CB_USE_INCLUDE_DATA ((void *) -1)
+enum poptCallbackReason { POPT_CALLBACK_REASON_PRE,
+ POPT_CALLBACK_REASON_POST,
+ POPT_CALLBACK_REASON_OPTION };
typedef void (*poptCallbackType)(poptContext con,
+ enum poptCallbackReason reason,
const struct poptOption * opt,
const char * arg, void * data);
con->execAbsolute = allowAbsolute;
}
+static void invokeCallbacks(poptContext con, const struct poptOption * table,
+ int post) {
+ const struct poptOption * opt = table;
+ poptCallbackType cb;
+
+ while (opt->longName || opt->shortName || opt->arg) {
+ if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
+ invokeCallbacks(con, opt->arg, post);
+ } else if (((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) &&
+ ((!post && (opt->argInfo & POPT_CBFLAG_PRE)) ||
+ ( post && (opt->argInfo & POPT_CBFLAG_POST)))) {
+ cb = opt->arg;
+ cb(con, post ? POPT_CALLBACK_REASON_POST : POPT_CALLBACK_REASON_PRE,
+ NULL, NULL, opt->descrip);
+ }
+ opt++;
+ }
+}
+
poptContext poptGetContext(char * name, int argc, char ** argv,
const struct poptOption * options, int flags) {
poptContext con = malloc(sizeof(*con));
if (name)
con->appName = strcpy(malloc(strlen(name) + 1), name);
+ invokeCallbacks(con, con->options, 0);
+
return con;
}
&& con->os > con->optionStack)
con->os--;
if (!con->os->nextCharArg && con->os->next == con->os->argc) {
+ invokeCallbacks(con, con->options, 1);
if (con->doExec) execCommand(con);
return -1;
}
}
if (cb)
- cb(con, opt, con->os->nextArg, cbData);
+ cb(con, POPT_CALLBACK_REASON_OPTION, opt, con->os->nextArg, cbData);
else if (opt->val)
done = 1;
callback data to pass */
#define POPT_ARG_MASK 0x0000FFFF
#define POPT_ARGFLAG_ONEDASH 0x80000000 /* allow -longoption */
+#define POPT_ARGFLAG_DOC_HIDDEN 0x40000000 /* don't show in help/usage */
+#define POPT_CBFLAG_PRE 0x80000000 /* call the callback before parse */
+#define POPT_CBFLAG_POST 0x40000000 /* call the callback after parse */
#define POPT_ERROR_NOARG -10
#define POPT_ERROR_BADOPT -11
typedef struct poptOption * poptOption;
#define POPT_CB_USE_INCLUDE_DATA ((void *) -1)
+enum poptCallbackReason { POPT_CALLBACK_REASON_PRE,
+ POPT_CALLBACK_REASON_POST,
+ POPT_CALLBACK_REASON_OPTION };
typedef void (*poptCallbackType)(poptContext con,
+ enum poptCallbackReason reason,
const struct poptOption * opt,
const char * arg, void * data);
#include "popt.h"
#include "poptint.h"
-static void displayArgs(poptContext con, struct poptOption * key,
+static void displayArgs(poptContext con, enum poptCallbackReason foo,
+ struct poptOption * key,
const char * arg, void * data) {
if (key->shortName== '?')
poptPrintHelp(con, stderr, 0);
while (opt->longName || opt->shortName || opt->arg) {
if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
this = maxArgWidth(opt->arg);
- } else {
+ if (this > max) max = this;
+ } else if (!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
this = opt->shortName ? 2 : 0;
if (opt->longName) {
if (this) this += 2;
s = getArgDescrip(opt);
if (s)
this += strlen(s) + 1;
+ if (this > max) max = this;
}
- if (this > max) max = this;
-
opt++;
}
opt = table;
while (opt->longName || opt->shortName || opt->arg) {
- if (opt->longName || opt->shortName)
+ if ((opt->longName || opt->shortName) &&
+ !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
singleOptionHelp(f, left, opt);
opt++;
}
opt = table;
while (opt->longName || opt->shortName || opt->arg) {
- if (opt->longName || opt->shortName)
+ if ((opt->longName || opt->shortName) &&
+ !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
cursor = singleOptionUsage(f, cursor, opt);
else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE)
cursor = singleTableUsage(f, cursor, opt->arg);