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 "system.h" |
---|
6 | |
---|
7 | static int pass2 = 0; |
---|
8 | static void option_callback(poptContext con, enum poptCallbackReason reason, |
---|
9 | const struct poptOption * opt, |
---|
10 | char * arg, void * data) { |
---|
11 | if (pass2) |
---|
12 | fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg); |
---|
13 | } |
---|
14 | |
---|
15 | int arg1 = 0; |
---|
16 | char * arg2 = "(none)"; |
---|
17 | int arg3 = 0; |
---|
18 | int inc = 0; |
---|
19 | int shortopt = 0; |
---|
20 | int singleDash = 0; |
---|
21 | |
---|
22 | static struct poptOption moreCallbackArgs[] = { |
---|
23 | { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA, |
---|
24 | (void *)option_callback, 0, NULL }, |
---|
25 | { "cb2", 'c', POPT_ARG_STRING, NULL, 'c', "Test argument callbacks" }, |
---|
26 | { NULL, '\0', 0, NULL, 0 } |
---|
27 | }; |
---|
28 | static struct poptOption callbackArgs[] = { |
---|
29 | { NULL, '\0', POPT_ARG_CALLBACK, (void *)option_callback, 0, "sampledata" }, |
---|
30 | { "cb", 'c', POPT_ARG_STRING, NULL, 'c', "Test argument callbacks" }, |
---|
31 | { "long", '\0', 0, NULL, 'l', "Unused option for help testing" }, |
---|
32 | { NULL, '\0', 0, NULL, 0 } |
---|
33 | }; |
---|
34 | static struct poptOption moreArgs[] = { |
---|
35 | { "inc", 'i', 0, &inc, 0, "An included argument" }, |
---|
36 | { NULL, '\0', 0, NULL, 0 } |
---|
37 | }; |
---|
38 | static struct poptOption options[] = { |
---|
39 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreCallbackArgs, 0, "arg for cb2" }, |
---|
40 | { "arg1", '\0', 0, &arg1, 0, "First argument with a really long" |
---|
41 | " description. After all, we have to test argument help" |
---|
42 | " wrapping somehow, right?", NULL }, |
---|
43 | { "arg2", '2', POPT_ARG_STRING, &arg2, 0, "Another argument", "ARG" }, |
---|
44 | { "arg3", '3', POPT_ARG_INT, &arg3, 0, "A third argument", "ANARG" }, |
---|
45 | { "shortoption", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0, |
---|
46 | "Needs a single -", NULL }, |
---|
47 | { "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0, |
---|
48 | "This shouldn't show up", NULL }, |
---|
49 | { "unused", '\0', POPT_ARG_STRING, NULL, 0, |
---|
50 | "Unused option for help testing", "UNUSED" }, |
---|
51 | { NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0 }, |
---|
52 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0, NULL }, |
---|
53 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0, "Callback arguments" }, |
---|
54 | POPT_AUTOHELP |
---|
55 | { NULL, '\0', 0, NULL, 0 } |
---|
56 | }; |
---|
57 | |
---|
58 | static void resetVars(void) |
---|
59 | { |
---|
60 | arg1 = 0; |
---|
61 | arg2 = "(none)"; |
---|
62 | arg3 = 0; |
---|
63 | inc = 0; |
---|
64 | shortopt = 0; |
---|
65 | singleDash = 0; |
---|
66 | pass2 = 0; |
---|
67 | } |
---|
68 | |
---|
69 | int main(int argc, const char ** argv) { |
---|
70 | int rc; |
---|
71 | int ec = 0; |
---|
72 | poptContext optCon; |
---|
73 | const char ** rest; |
---|
74 | int help = 0; |
---|
75 | int usage = 0; |
---|
76 | |
---|
77 | #if HAVE_MCHECK_H && HAVE_MTRACE |
---|
78 | mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ |
---|
79 | #endif |
---|
80 | |
---|
81 | resetVars(); |
---|
82 | optCon = poptGetContext("test1", argc, argv, options, 0); |
---|
83 | poptReadConfigFile(optCon, "./test-poptrc"); |
---|
84 | |
---|
85 | #if 1 |
---|
86 | while ((rc = poptGetNextOpt(optCon)) > 0) /* Read all the options ... */ |
---|
87 | ; |
---|
88 | |
---|
89 | poptResetContext(optCon); /* ... and then start over. */ |
---|
90 | resetVars(); |
---|
91 | #endif |
---|
92 | |
---|
93 | pass2 = 1; |
---|
94 | if ((rc = poptGetNextOpt(optCon)) < -1) { |
---|
95 | fprintf(stderr, "test1: bad argument %s: %s\n", |
---|
96 | poptBadOption(optCon, POPT_BADOPTION_NOALIAS), |
---|
97 | poptStrerror(rc)); |
---|
98 | ec = 2; |
---|
99 | goto exit; |
---|
100 | } |
---|
101 | |
---|
102 | if (help) { |
---|
103 | poptPrintHelp(optCon, stdout, 0); |
---|
104 | goto exit; |
---|
105 | } |
---|
106 | if (usage) { |
---|
107 | poptPrintUsage(optCon, stdout, 0); |
---|
108 | goto exit; |
---|
109 | } |
---|
110 | |
---|
111 | fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2); |
---|
112 | |
---|
113 | if (arg3) |
---|
114 | fprintf(stdout, " arg3: %d", arg3); |
---|
115 | if (inc) |
---|
116 | fprintf(stdout, " inc: %d", inc); |
---|
117 | if (shortopt) |
---|
118 | fprintf(stdout, " short: %d", shortopt); |
---|
119 | if (singleDash) |
---|
120 | fprintf(stdout, " -"); |
---|
121 | |
---|
122 | rest = poptGetArgs(optCon); |
---|
123 | if (rest) { |
---|
124 | fprintf(stdout, " rest:"); |
---|
125 | while (*rest) { |
---|
126 | fprintf(stdout, " %s", *rest); |
---|
127 | rest++; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | fprintf(stdout, "\n"); |
---|
132 | |
---|
133 | exit: |
---|
134 | poptFreeContext(optCon); |
---|
135 | #if HAVE_MCHECK_H && HAVE_MTRACE |
---|
136 | muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ |
---|
137 | #endif |
---|
138 | return ec; |
---|
139 | } |
---|