implemented POPT_ARGFLAG_DOC_HIDDEN, POPT_CBFLAG_PRE, POPT_CBFLAG_POST
authorewt <devnull@localhost>
Wed, 21 Oct 1998 21:44:18 +0000 (21:44 +0000)
committerewt <devnull@localhost>
Wed, 21 Oct 1998 21:44:18 +0000 (21:44 +0000)
CVS patchset: 2473
CVS date: 1998/10/21 21:44:18

popt/popt.3
popt/popt.c
popt/popt.h
popt/popthelp.c
popt/test1.c

index de119ab..e59a299 100644 (file)
@@ -68,10 +68,12 @@ consists of a - character followed by a single alphanumeric character.
 A 
 .BR "long option" ,
 common in GNU utilities, consists of two - characters followed by a
-string made up of letters, numbers and hyphens.  Either type of option
-may be followed by an argument.  A space separates a short option from
-its arguments; either a space or an = separates a long option from an
-argument. 
+string made up of letters, numbers and hyphens.  Long options are
+optionally allowed to begin with a single -, primarily to allow command-line
+compatibility between popt applications and X toolkit applications.
+Either type of option may be followed by an argument.  A space separates a 
+short option from its arguments; either a space or an = separates a long 
+option from an argument. 
 .sp
 The popt library is highly portable and should work on any POSIX 
 platform.  The latest version is always available from: 
@@ -125,6 +127,11 @@ The rest of the valid values are shown in the following table:
 .BR POPT_ARG_LONG "   A long integer is expected       long"
 .sp
 .fi
+If \fIargInfo\fR value is logically or'd with \fBPOPT_ARGFLAG_ONEDASH\fR,
+the long argument may be given with a single - instead of two. For example,
+if \fB--longopt\fR is an option with \fBPOPT_ARGFLAG_ONEDASH\fR, is
+specified, \fB-longopt\fR is accepted as well.
+.sp
 .RI "The next element, " arg ", allows popt to automatically update "
 .RI "program variables when the option is used. If " arg " is " 
 .BR NULL ", it is ignored and popt takes no special action. " 
@@ -346,6 +353,10 @@ without modifying the list.
 All the leftover arguments are returned in a manner identical to 
 .IR argv ".  The final element in the returned array points to "
 .BR NULL ", indicating the end of the arguments.
+.sp
+.SS "5. AUTOMATIC HELP MESSAGES"
+The \fBpopt\fR library can automatically generate help messages which
+describe the options a program accepts. 
 .PP 
 .SH "ERROR HANDLING"
 All of the popt functions that can return errors return integers. 
index 22d5e08..ec29c5f 100644 (file)
@@ -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;
 
index a759f21..7f953ac 100644 (file)
@@ -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);
 
index 17f110e..df5ed26 100644 (file)
@@ -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);
index 8f53723..cdda82b 100644 (file)
@@ -7,7 +7,8 @@
 
 #include "popt.h"
 
-void option_callback(poptContext con, const struct poptOption * opt, 
+void option_callback(poptContext con, enum poptCallbackReason reason,
+                    const struct poptOption * opt, 
                     char * arg, void * data) {
     fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg);    
 }
@@ -41,6 +42,8 @@ int main(int argc, char ** argv) {
        { "arg3", '3', POPT_ARG_INT, &arg3, 0, "A third argument", "ANARG" },
        { "shortoption", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0,
                "Needs a single -", NULL },
+       { "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0, 
+               "This shouldn't show up", NULL },
        { "unused", '\0', POPT_ARG_STRING, NULL, 0, 
            "Unused option for help testing", "UNUSED" },
        { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0, NULL },