summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Troan <ewt@src.gnome.org>1998-10-21 21:44:21 +0000
committerErik Troan <ewt@src.gnome.org>1998-10-21 21:44:21 +0000
commit0fe1a70475dfa7889d61c1b58b277d9317c79934 (patch)
tree399df9da5158539223f908a98836d1f5d3b277ef
parentc01ab27ea73e74fe353df354b8e7e0bb9fde01e7 (diff)
downloadgnome-common-0fe1a70475dfa7889d61c1b58b277d9317c79934.tar.gz
implemented POPT_ARGFLAG_DOC_HIDDEN, POPT_CBFLAG_PRE, POPT_CBFLAG_POST
svn path=/trunk/; revision=451
-rw-r--r--support/popt-gnome.h7
-rw-r--r--support/popt.c24
-rw-r--r--support/popt.h7
-rw-r--r--support/popthelp.c15
4 files changed, 46 insertions, 7 deletions
diff --git a/support/popt-gnome.h b/support/popt-gnome.h
index a759f21..7f953ac 100644
--- a/support/popt-gnome.h
+++ b/support/popt-gnome.h
@@ -20,6 +20,9 @@
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
@@ -62,7 +65,11 @@ typedef struct poptContext_s * poptContext;
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);
diff --git a/support/popt.c b/support/popt.c
index 22d5e08..ec29c5f 100644
--- a/support/popt.c
+++ b/support/popt.c
@@ -41,6 +41,25 @@ void poptSetExecPath(poptContext con, const char * path, int allowAbsolute) {
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));
@@ -67,6 +86,8 @@ poptContext poptGetContext(char * name, int argc, char ** argv,
if (name)
con->appName = strcpy(malloc(strlen(name) + 1), name);
+ invokeCallbacks(con, con->options, 0);
+
return con;
}
@@ -264,6 +285,7 @@ int poptGetNextOpt(poptContext 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;
}
@@ -391,7 +413,7 @@ int poptGetNextOpt(poptContext con) {
}
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;
diff --git a/support/popt.h b/support/popt.h
index a759f21..7f953ac 100644
--- a/support/popt.h
+++ b/support/popt.h
@@ -20,6 +20,9 @@
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
@@ -62,7 +65,11 @@ typedef struct poptContext_s * poptContext;
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);
diff --git a/support/popthelp.c b/support/popthelp.c
index 17f110e..df5ed26 100644
--- a/support/popthelp.c
+++ b/support/popthelp.c
@@ -14,7 +14,8 @@
#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);
@@ -94,7 +95,8 @@ static int maxArgWidth(const struct poptOption * opt) {
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;
@@ -104,10 +106,9 @@ static int maxArgWidth(const struct poptOption * opt) {
s = getArgDescrip(opt);
if (s)
this += strlen(s) + 1;
+ if (this > max) max = this;
}
- if (this > max) max = this;
-
opt++;
}
@@ -120,7 +121,8 @@ static void singleTableHelp(FILE * f, const struct poptOption * table,
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++;
}
@@ -204,7 +206,8 @@ int singleTableUsage(FILE * f, int cursor, const struct poptOption * table) {
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);