| 1 | /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING |
| 2 | file accompanying popt source distributions, available from |
| 3 | ftp://ftp.redhat.com/pub/code/popt */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | #include "popt.h" |
| 9 | |
| 10 | void option_callback(poptContext con, enum poptCallbackReason reason, |
| 11 | const struct poptOption * opt, |
| 12 | char * arg, void * data) { |
| 13 | fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg); |
| 14 | } |
| 15 | |
| 16 | int main(int argc, char ** argv) { |
| 17 | int rc; |
| 18 | int arg1 = 0; |
| 19 | char * arg2 = "(none)"; |
| 20 | poptContext optCon; |
| 21 | char ** rest; |
| 22 | int arg3 = 0; |
| 23 | int inc = 0; |
| 24 | int help = 0; |
| 25 | int usage = 0; |
| 26 | int shortopt = 0; |
| 27 | struct poptOption moreCallbackArgs[] = { |
| 28 | { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA, |
| 29 | option_callback, 0, NULL }, |
| 30 | { "cb2", 'c', POPT_ARG_STRING, NULL, 'c', "Test argument callbacks" }, |
| 31 | { NULL, '\0', 0, NULL, 0 } |
| 32 | }; |
| 33 | struct poptOption callbackArgs[] = { |
| 34 | { NULL, '\0', POPT_ARG_CALLBACK, option_callback, 0, "sampledata" }, |
| 35 | { "cb", 'c', POPT_ARG_STRING, NULL, 'c', "Test argument callbacks" }, |
| 36 | { "long", '\0', 0, NULL, 'l', "Unused option for help testing" }, |
| 37 | { NULL, '\0', 0, NULL, 0 } |
| 38 | }; |
| 39 | struct poptOption moreArgs[] = { |
| 40 | { "inc", 'i', 0, &inc, 0, "An included argument" }, |
| 41 | { NULL, '\0', 0, NULL, 0 } |
| 42 | }; |
| 43 | struct poptOption options[] = { |
| 44 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreCallbackArgs, 0, "arg for cb2" }, |
| 45 | { "arg1", '\0', 0, &arg1, 0, "First argument with a really long" |
| 46 | " description. After all, we have to test argument help" |
| 47 | " wrapping somehow, right?", NULL }, |
| 48 | { "arg2", '2', POPT_ARG_STRING, &arg2, 0, "Another argument", "ARG" }, |
| 49 | { "arg3", '3', POPT_ARG_INT, &arg3, 0, "A third argument", "ANARG" }, |
| 50 | { "shortoption", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0, |
| 51 | "Needs a single -", NULL }, |
| 52 | { "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0, |
| 53 | "This shouldn't show up", NULL }, |
| 54 | { "unused", '\0', POPT_ARG_STRING, NULL, 0, |
| 55 | "Unused option for help testing", "UNUSED" }, |
| 56 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0, NULL }, |
| 57 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0, "Callback arguments" }, |
| 58 | POPT_AUTOHELP |
| 59 | { NULL, '\0', 0, NULL, 0 } |
| 60 | }; |
| 61 | |
| 62 | optCon = poptGetContext("test1", argc, argv, options, 0); |
| 63 | poptReadConfigFile(optCon, "./test-poptrc"); |
| 64 | |
| 65 | if ((rc = poptGetNextOpt(optCon)) < -1) { |
| 66 | fprintf(stderr, "test1: bad argument %s: %s\n", |
| 67 | poptBadOption(optCon, POPT_BADOPTION_NOALIAS), |
| 68 | poptStrerror(rc)); |
| 69 | return 2; |
| 70 | } |
| 71 | |
| 72 | if (help) { |
| 73 | poptPrintHelp(optCon, stdout, 0); |
| 74 | return 0; |
| 75 | } if (usage) { |
| 76 | poptPrintUsage(optCon, stdout, 0); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2); |
| 81 | |
| 82 | if (arg3) |
| 83 | fprintf(stdout, " arg3: %d", arg3); |
| 84 | if (inc) |
| 85 | fprintf(stdout, " inc: %d", inc); |
| 86 | if (shortopt) |
| 87 | fprintf(stdout, " short: %d", shortopt); |
| 88 | |
| 89 | rest = poptGetArgs(optCon); |
| 90 | if (rest) { |
| 91 | fprintf(stdout, " rest:"); |
| 92 | while (*rest) { |
| 93 | fprintf(stdout, " %s", *rest); |
| 94 | rest++; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fprintf(stdout, "\n"); |
| 99 | |
| 100 | return 0; |
| 101 | } |