reduced verbosity and better debugging.
[gnutls.git] / gl / tests / test-getopt_long.h
blob9d55c653250869acc4dfa1fb1bf953d1671e0c3f
1 /* Test of command line argument processing.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
19 static int a_seen;
20 static int b_seen;
21 static int q_seen;
23 static const struct option long_options_required[] =
25 { "alpha", no_argument, NULL, 'a' },
26 { "beta", no_argument, &b_seen, 1 },
27 { "prune", required_argument, NULL, 'p' },
28 { "quetsche", required_argument, &q_seen, 1 },
29 { "xtremely-",no_argument, NULL, 1003 },
30 { "xtra", no_argument, NULL, 1001 },
31 { "xtreme", no_argument, NULL, 1002 },
32 { "xtremely", no_argument, NULL, 1003 },
33 { NULL, 0, NULL, 0 }
36 static const struct option long_options_optional[] =
38 { "alpha", no_argument, NULL, 'a' },
39 { "beta", no_argument, &b_seen, 1 },
40 { "prune", optional_argument, NULL, 'p' },
41 { "quetsche", optional_argument, &q_seen, 1 },
42 { NULL, 0, NULL, 0 }
45 static void
46 getopt_long_loop (int argc, const char **argv,
47 const char *options, const struct option *long_options,
48 const char **p_value, const char **q_value,
49 int *non_options_count, const char **non_options,
50 int *unrecognized)
52 int option_index = -1;
53 int c;
55 opterr = 0;
56 q_seen = 0;
57 while ((c = getopt_long (argc, (char **) argv, options, long_options,
58 &option_index))
59 != -1)
61 switch (c)
63 case 0:
64 /* An option with a non-NULL flag pointer was processed. */
65 if (q_seen)
66 *q_value = optarg;
67 break;
68 case 'a':
69 a_seen++;
70 break;
71 case 'b':
72 b_seen = 1;
73 break;
74 case 'p':
75 *p_value = optarg;
76 break;
77 case 'q':
78 *q_value = optarg;
79 break;
80 case '\1':
81 /* Must only happen with option '-' at the beginning. */
82 ASSERT (options[0] == '-');
83 non_options[(*non_options_count)++] = optarg;
84 break;
85 case ':':
86 /* Must only happen with option ':' at the beginning. */
87 ASSERT (options[0] == ':'
88 || ((options[0] == '-' || options[0] == '+')
89 && options[1] == ':'));
90 /* fall through */
91 case '?':
92 *unrecognized = optopt;
93 break;
94 default:
95 *unrecognized = c;
96 break;
101 /* Reduce casting, so we can use string literals elsewhere.
102 getopt_long takes an array of char*, but luckily does not modify
103 those elements, so we can pass const char*. */
104 static int
105 do_getopt_long (int argc, const char **argv, const char *shortopts,
106 const struct option *longopts, int *longind)
108 return getopt_long (argc, (char **) argv, shortopts, longopts, longind);
111 static void
112 test_getopt_long (void)
114 int start;
116 /* Test disambiguation of options. */
118 int argc = 0;
119 const char *argv[10];
120 int option_index;
121 int c;
123 argv[argc++] = "program";
124 argv[argc++] = "--x";
125 argv[argc] = NULL;
126 optind = 1;
127 opterr = 0;
128 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
129 ASSERT (c == '?');
130 ASSERT (optopt == 0);
133 int argc = 0;
134 const char *argv[10];
135 int option_index;
136 int c;
138 argv[argc++] = "program";
139 argv[argc++] = "--xt";
140 argv[argc] = NULL;
141 optind = 1;
142 opterr = 0;
143 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
144 ASSERT (c == '?');
145 ASSERT (optopt == 0);
148 int argc = 0;
149 const char *argv[10];
150 int option_index;
151 int c;
153 argv[argc++] = "program";
154 argv[argc++] = "--xtr";
155 argv[argc] = NULL;
156 optind = 1;
157 opterr = 0;
158 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
159 ASSERT (c == '?');
160 ASSERT (optopt == 0);
163 int argc = 0;
164 const char *argv[10];
165 int option_index;
166 int c;
168 argv[argc++] = "program";
169 argv[argc++] = "--xtra";
170 argv[argc] = NULL;
171 optind = 1;
172 opterr = 0;
173 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
174 ASSERT (c == 1001);
177 int argc = 0;
178 const char *argv[10];
179 int option_index;
180 int c;
182 argv[argc++] = "program";
183 argv[argc++] = "--xtre";
184 argv[argc] = NULL;
185 optind = 1;
186 opterr = 0;
187 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
188 ASSERT (c == '?');
189 ASSERT (optopt == 0);
192 int argc = 0;
193 const char *argv[10];
194 int option_index;
195 int c;
197 argv[argc++] = "program";
198 argv[argc++] = "--xtrem";
199 argv[argc] = NULL;
200 optind = 1;
201 opterr = 0;
202 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
203 ASSERT (c == '?');
204 ASSERT (optopt == 0);
207 int argc = 0;
208 const char *argv[10];
209 int option_index;
210 int c;
212 argv[argc++] = "program";
213 argv[argc++] = "--xtreme";
214 argv[argc] = NULL;
215 optind = 1;
216 opterr = 0;
217 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
218 ASSERT (c == 1002);
221 int argc = 0;
222 const char *argv[10];
223 int option_index;
224 int c;
226 argv[argc++] = "program";
227 argv[argc++] = "--xtremel";
228 argv[argc] = NULL;
229 optind = 1;
230 opterr = 0;
231 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
232 ASSERT (c == 1003);
235 int argc = 0;
236 const char *argv[10];
237 int option_index;
238 int c;
240 argv[argc++] = "program";
241 argv[argc++] = "--xtremely";
242 argv[argc] = NULL;
243 optind = 1;
244 opterr = 0;
245 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
246 ASSERT (c == 1003);
249 /* Check that -W handles unknown options. */
251 int argc = 0;
252 const char *argv[10];
253 int option_index;
254 int c;
256 argv[argc++] = "program";
257 argv[argc++] = "-W";
258 argv[argc] = NULL;
259 optind = 1;
260 opterr = 0;
261 c = do_getopt_long (argc, argv, "W;", long_options_required, &option_index);
262 ASSERT (c == '?');
263 ASSERT (optopt == 'W');
266 int argc = 0;
267 const char *argv[10];
268 int option_index;
269 int c;
271 argv[argc++] = "program";
272 argv[argc++] = "-Wunknown";
273 argv[argc] = NULL;
274 optind = 1;
275 opterr = 0;
276 c = do_getopt_long (argc, argv, "W;", long_options_required, &option_index);
277 /* glibc and BSD behave differently here, but for now, we allow
278 both behaviors since W support is not frequently used. */
279 if (c == '?')
281 ASSERT (optopt == 0);
282 ASSERT (optarg == NULL);
284 else
286 ASSERT (c == 'W');
287 ASSERT (strcmp (optarg, "unknown") == 0);
291 int argc = 0;
292 const char *argv[10];
293 int option_index;
294 int c;
296 argv[argc++] = "program";
297 argv[argc++] = "-W";
298 argv[argc++] = "unknown";
299 argv[argc] = NULL;
300 optind = 1;
301 opterr = 0;
302 c = do_getopt_long (argc, argv, "W;", long_options_required, &option_index);
303 /* glibc and BSD behave differently here, but for now, we allow
304 both behaviors since W support is not frequently used. */
305 if (c == '?')
307 ASSERT (optopt == 0);
308 ASSERT (optarg == NULL);
310 else
312 ASSERT (c == 'W');
313 ASSERT (strcmp (optarg, "unknown") == 0);
317 /* Test that 'W' does not dump core:
318 http://sourceware.org/bugzilla/show_bug.cgi?id=12922 */
320 int argc = 0;
321 const char *argv[10];
322 int option_index;
323 int c;
325 argv[argc++] = "program";
326 argv[argc++] = "-W";
327 argv[argc++] = "dummy";
328 argv[argc] = NULL;
329 optind = 1;
330 opterr = 0;
331 c = do_getopt_long (argc, argv, "W;", NULL, &option_index);
332 ASSERT (c == 'W');
333 ASSERT (optind == 2);
336 /* Test processing of boolean short options. */
337 for (start = 0; start <= 1; start++)
339 const char *p_value = NULL;
340 const char *q_value = NULL;
341 int non_options_count = 0;
342 const char *non_options[10];
343 int unrecognized = 0;
344 int argc = 0;
345 const char *argv[10];
346 a_seen = 0;
347 b_seen = 0;
349 argv[argc++] = "program";
350 argv[argc++] = "-a";
351 argv[argc++] = "foo";
352 argv[argc++] = "bar";
353 argv[argc] = NULL;
354 optind = start;
355 getopt_long_loop (argc, argv, "ab", long_options_required,
356 &p_value, &q_value,
357 &non_options_count, non_options, &unrecognized);
358 ASSERT (a_seen == 1);
359 ASSERT (b_seen == 0);
360 ASSERT (p_value == NULL);
361 ASSERT (q_value == NULL);
362 ASSERT (non_options_count == 0);
363 ASSERT (unrecognized == 0);
364 ASSERT (optind == 2);
366 for (start = 0; start <= 1; start++)
368 const char *p_value = NULL;
369 const char *q_value = NULL;
370 int non_options_count = 0;
371 const char *non_options[10];
372 int unrecognized = 0;
373 int argc = 0;
374 const char *argv[10];
375 a_seen = 0;
376 b_seen = 0;
378 argv[argc++] = "program";
379 argv[argc++] = "-b";
380 argv[argc++] = "-a";
381 argv[argc++] = "foo";
382 argv[argc++] = "bar";
383 argv[argc] = NULL;
384 optind = start;
385 getopt_long_loop (argc, argv, "ab", long_options_required,
386 &p_value, &q_value,
387 &non_options_count, non_options, &unrecognized);
388 ASSERT (a_seen == 1);
389 ASSERT (b_seen == 1);
390 ASSERT (p_value == NULL);
391 ASSERT (q_value == NULL);
392 ASSERT (non_options_count == 0);
393 ASSERT (unrecognized == 0);
394 ASSERT (optind == 3);
396 for (start = 0; start <= 1; start++)
398 const char *p_value = NULL;
399 const char *q_value = NULL;
400 int non_options_count = 0;
401 const char *non_options[10];
402 int unrecognized = 0;
403 int argc = 0;
404 const char *argv[10];
405 a_seen = 0;
406 b_seen = 0;
408 argv[argc++] = "program";
409 argv[argc++] = "-ba";
410 argv[argc++] = "foo";
411 argv[argc++] = "bar";
412 argv[argc] = NULL;
413 optind = start;
414 getopt_long_loop (argc, argv, "ab", long_options_required,
415 &p_value, &q_value,
416 &non_options_count, non_options, &unrecognized);
417 ASSERT (a_seen == 1);
418 ASSERT (b_seen == 1);
419 ASSERT (p_value == NULL);
420 ASSERT (q_value == NULL);
421 ASSERT (non_options_count == 0);
422 ASSERT (unrecognized == 0);
423 ASSERT (optind == 2);
425 for (start = 0; start <= 1; start++)
427 const char *p_value = NULL;
428 const char *q_value = NULL;
429 int non_options_count = 0;
430 const char *non_options[10];
431 int unrecognized = 0;
432 int argc = 0;
433 const char *argv[10];
434 a_seen = 0;
435 b_seen = 0;
437 argv[argc++] = "program";
438 argv[argc++] = "-ab";
439 argv[argc++] = "-a";
440 argv[argc++] = "foo";
441 argv[argc++] = "bar";
442 argv[argc] = NULL;
443 optind = start;
444 getopt_long_loop (argc, argv, "ab", long_options_required,
445 &p_value, &q_value,
446 &non_options_count, non_options, &unrecognized);
447 ASSERT (a_seen == 2);
448 ASSERT (b_seen == 1);
449 ASSERT (p_value == NULL);
450 ASSERT (q_value == NULL);
451 ASSERT (non_options_count == 0);
452 ASSERT (unrecognized == 0);
453 ASSERT (optind == 3);
456 /* Test processing of boolean long options. */
457 for (start = 0; start <= 1; start++)
459 const char *p_value = NULL;
460 const char *q_value = NULL;
461 int non_options_count = 0;
462 const char *non_options[10];
463 int unrecognized = 0;
464 int argc = 0;
465 const char *argv[10];
466 a_seen = 0;
467 b_seen = 0;
469 argv[argc++] = "program";
470 argv[argc++] = "--alpha";
471 argv[argc++] = "foo";
472 argv[argc++] = "bar";
473 argv[argc] = NULL;
474 optind = start;
475 getopt_long_loop (argc, argv, "ab", long_options_required,
476 &p_value, &q_value,
477 &non_options_count, non_options, &unrecognized);
478 ASSERT (a_seen == 1);
479 ASSERT (b_seen == 0);
480 ASSERT (p_value == NULL);
481 ASSERT (q_value == NULL);
482 ASSERT (non_options_count == 0);
483 ASSERT (unrecognized == 0);
484 ASSERT (optind == 2);
486 for (start = 0; start <= 1; start++)
488 const char *p_value = NULL;
489 const char *q_value = NULL;
490 int non_options_count = 0;
491 const char *non_options[10];
492 int unrecognized = 0;
493 int argc = 0;
494 const char *argv[10];
495 a_seen = 0;
496 b_seen = 0;
498 argv[argc++] = "program";
499 argv[argc++] = "--beta";
500 argv[argc++] = "--alpha";
501 argv[argc++] = "foo";
502 argv[argc++] = "bar";
503 argv[argc] = NULL;
504 optind = start;
505 getopt_long_loop (argc, argv, "ab", long_options_required,
506 &p_value, &q_value,
507 &non_options_count, non_options, &unrecognized);
508 ASSERT (a_seen == 1);
509 ASSERT (b_seen == 1);
510 ASSERT (p_value == NULL);
511 ASSERT (q_value == NULL);
512 ASSERT (non_options_count == 0);
513 ASSERT (unrecognized == 0);
514 ASSERT (optind == 3);
516 for (start = 0; start <= 1; start++)
518 const char *p_value = NULL;
519 const char *q_value = NULL;
520 int non_options_count = 0;
521 const char *non_options[10];
522 int unrecognized = 0;
523 int argc = 0;
524 const char *argv[10];
525 a_seen = 0;
526 b_seen = 0;
528 argv[argc++] = "program";
529 argv[argc++] = "--alpha";
530 argv[argc++] = "--beta";
531 argv[argc++] = "--alpha";
532 argv[argc++] = "--beta";
533 argv[argc++] = "foo";
534 argv[argc++] = "bar";
535 argv[argc] = NULL;
536 optind = start;
537 getopt_long_loop (argc, argv, "ab", long_options_required,
538 &p_value, &q_value,
539 &non_options_count, non_options, &unrecognized);
540 ASSERT (a_seen == 2);
541 ASSERT (b_seen == 1);
542 ASSERT (p_value == NULL);
543 ASSERT (q_value == NULL);
544 ASSERT (non_options_count == 0);
545 ASSERT (unrecognized == 0);
546 ASSERT (optind == 5);
549 /* Test processing of boolean long options via -W. */
550 for (start = 0; start <= 1; start++)
552 const char *p_value = NULL;
553 const char *q_value = NULL;
554 int non_options_count = 0;
555 const char *non_options[10];
556 int unrecognized = 0;
557 int argc = 0;
558 const char *argv[10];
559 a_seen = 0;
560 b_seen = 0;
562 argv[argc++] = "program";
563 argv[argc++] = "-Walpha";
564 argv[argc++] = "foo";
565 argv[argc++] = "bar";
566 argv[argc] = NULL;
567 optind = start;
568 getopt_long_loop (argc, argv, "abW;", long_options_required,
569 &p_value, &q_value,
570 &non_options_count, non_options, &unrecognized);
571 ASSERT (a_seen == 1);
572 ASSERT (b_seen == 0);
573 ASSERT (p_value == NULL);
574 ASSERT (q_value == NULL);
575 ASSERT (non_options_count == 0);
576 ASSERT (unrecognized == 0);
577 ASSERT (optind == 2);
579 for (start = 0; start <= 1; start++)
581 const char *p_value = NULL;
582 const char *q_value = NULL;
583 int non_options_count = 0;
584 const char *non_options[10];
585 int unrecognized = 0;
586 int argc = 0;
587 const char *argv[10];
588 a_seen = 0;
589 b_seen = 0;
591 argv[argc++] = "program";
592 argv[argc++] = "-W";
593 argv[argc++] = "beta";
594 argv[argc++] = "-W";
595 argv[argc++] = "alpha";
596 argv[argc++] = "foo";
597 argv[argc++] = "bar";
598 argv[argc] = NULL;
599 optind = start;
600 getopt_long_loop (argc, argv, "aW;b", long_options_required,
601 &p_value, &q_value,
602 &non_options_count, non_options, &unrecognized);
603 ASSERT (a_seen == 1);
604 ASSERT (b_seen == 1);
605 ASSERT (p_value == NULL);
606 ASSERT (q_value == NULL);
607 ASSERT (non_options_count == 0);
608 ASSERT (unrecognized == 0);
609 ASSERT (optind == 5);
611 for (start = 0; start <= 1; start++)
613 const char *p_value = NULL;
614 const char *q_value = NULL;
615 int non_options_count = 0;
616 const char *non_options[10];
617 int unrecognized = 0;
618 int argc = 0;
619 const char *argv[10];
620 a_seen = 0;
621 b_seen = 0;
623 argv[argc++] = "program";
624 argv[argc++] = "-Walpha";
625 argv[argc++] = "-Wbeta";
626 argv[argc++] = "-Walpha";
627 argv[argc++] = "-Wbeta";
628 argv[argc++] = "foo";
629 argv[argc++] = "bar";
630 argv[argc] = NULL;
631 optind = start;
632 getopt_long_loop (argc, argv, "W;ab", long_options_required,
633 &p_value, &q_value,
634 &non_options_count, non_options, &unrecognized);
635 ASSERT (a_seen == 2);
636 ASSERT (b_seen == 1);
637 ASSERT (p_value == NULL);
638 ASSERT (q_value == NULL);
639 ASSERT (non_options_count == 0);
640 ASSERT (unrecognized == 0);
641 ASSERT (optind == 5);
644 /* Test processing of short options with arguments. */
645 for (start = 0; start <= 1; start++)
647 const char *p_value = NULL;
648 const char *q_value = NULL;
649 int non_options_count = 0;
650 const char *non_options[10];
651 int unrecognized = 0;
652 int argc = 0;
653 const char *argv[10];
654 a_seen = 0;
655 b_seen = 0;
657 argv[argc++] = "program";
658 argv[argc++] = "-pfoo";
659 argv[argc++] = "bar";
660 argv[argc] = NULL;
661 optind = start;
662 getopt_long_loop (argc, argv, "p:q:", long_options_required,
663 &p_value, &q_value,
664 &non_options_count, non_options, &unrecognized);
665 ASSERT (a_seen == 0);
666 ASSERT (b_seen == 0);
667 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
668 ASSERT (q_value == NULL);
669 ASSERT (non_options_count == 0);
670 ASSERT (unrecognized == 0);
671 ASSERT (optind == 2);
673 for (start = 0; start <= 1; start++)
675 const char *p_value = NULL;
676 const char *q_value = NULL;
677 int non_options_count = 0;
678 const char *non_options[10];
679 int unrecognized = 0;
680 int argc = 0;
681 const char *argv[10];
682 a_seen = 0;
683 b_seen = 0;
685 argv[argc++] = "program";
686 argv[argc++] = "-p";
687 argv[argc++] = "foo";
688 argv[argc++] = "bar";
689 argv[argc] = NULL;
690 optind = start;
691 getopt_long_loop (argc, argv, "p:q:", long_options_required,
692 &p_value, &q_value,
693 &non_options_count, non_options, &unrecognized);
694 ASSERT (a_seen == 0);
695 ASSERT (b_seen == 0);
696 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
697 ASSERT (q_value == NULL);
698 ASSERT (non_options_count == 0);
699 ASSERT (unrecognized == 0);
700 ASSERT (optind == 3);
702 for (start = 0; start <= 1; start++)
704 const char *p_value = NULL;
705 const char *q_value = NULL;
706 int non_options_count = 0;
707 const char *non_options[10];
708 int unrecognized = 0;
709 int argc = 0;
710 const char *argv[10];
711 a_seen = 0;
712 b_seen = 0;
714 argv[argc++] = "program";
715 argv[argc++] = "-ab";
716 argv[argc++] = "-q";
717 argv[argc++] = "baz";
718 argv[argc++] = "-pfoo";
719 argv[argc++] = "bar";
720 argv[argc] = NULL;
721 optind = start;
722 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
723 &p_value, &q_value,
724 &non_options_count, non_options, &unrecognized);
725 ASSERT (a_seen == 1);
726 ASSERT (b_seen == 1);
727 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
728 ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
729 ASSERT (non_options_count == 0);
730 ASSERT (unrecognized == 0);
731 ASSERT (optind == 5);
734 /* Test processing of long options with arguments. */
735 for (start = 0; start <= 1; start++)
737 const char *p_value = NULL;
738 const char *q_value = NULL;
739 int non_options_count = 0;
740 const char *non_options[10];
741 int unrecognized = 0;
742 int argc = 0;
743 const char *argv[10];
744 a_seen = 0;
745 b_seen = 0;
747 argv[argc++] = "program";
748 argv[argc++] = "--p=foo";
749 argv[argc++] = "bar";
750 argv[argc] = NULL;
751 optind = start;
752 getopt_long_loop (argc, argv, "p:q:", long_options_required,
753 &p_value, &q_value,
754 &non_options_count, non_options, &unrecognized);
755 ASSERT (a_seen == 0);
756 ASSERT (b_seen == 0);
757 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
758 ASSERT (q_value == NULL);
759 ASSERT (non_options_count == 0);
760 ASSERT (unrecognized == 0);
761 ASSERT (optind == 2);
763 for (start = 0; start <= 1; start++)
765 const char *p_value = NULL;
766 const char *q_value = NULL;
767 int non_options_count = 0;
768 const char *non_options[10];
769 int unrecognized = 0;
770 int argc = 0;
771 const char *argv[10];
772 a_seen = 0;
773 b_seen = 0;
775 argv[argc++] = "program";
776 argv[argc++] = "--p";
777 argv[argc++] = "foo";
778 argv[argc++] = "bar";
779 argv[argc] = NULL;
780 optind = start;
781 getopt_long_loop (argc, argv, "p:q:", long_options_required,
782 &p_value, &q_value,
783 &non_options_count, non_options, &unrecognized);
784 ASSERT (a_seen == 0);
785 ASSERT (b_seen == 0);
786 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
787 ASSERT (q_value == NULL);
788 ASSERT (non_options_count == 0);
789 ASSERT (unrecognized == 0);
790 ASSERT (optind == 3);
792 for (start = 0; start <= 1; start++)
794 const char *p_value = NULL;
795 const char *q_value = NULL;
796 int non_options_count = 0;
797 const char *non_options[10];
798 int unrecognized = 0;
799 int argc = 0;
800 const char *argv[10];
801 a_seen = 0;
802 b_seen = 0;
804 argv[argc++] = "program";
805 argv[argc++] = "-ab";
806 argv[argc++] = "--q";
807 argv[argc++] = "baz";
808 argv[argc++] = "--p=foo";
809 argv[argc++] = "bar";
810 argv[argc] = NULL;
811 optind = start;
812 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
813 &p_value, &q_value,
814 &non_options_count, non_options, &unrecognized);
815 ASSERT (a_seen == 1);
816 ASSERT (b_seen == 1);
817 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
818 ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
819 ASSERT (non_options_count == 0);
820 ASSERT (unrecognized == 0);
821 ASSERT (optind == 5);
824 /* Test processing of long options with arguments via -W. */
825 for (start = 0; start <= 1; start++)
827 const char *p_value = NULL;
828 const char *q_value = NULL;
829 int non_options_count = 0;
830 const char *non_options[10];
831 int unrecognized = 0;
832 int argc = 0;
833 const char *argv[10];
834 a_seen = 0;
835 b_seen = 0;
837 argv[argc++] = "program";
838 argv[argc++] = "-Wp=foo";
839 argv[argc++] = "bar";
840 argv[argc] = NULL;
841 optind = start;
842 getopt_long_loop (argc, argv, "p:q:W;", long_options_required,
843 &p_value, &q_value,
844 &non_options_count, non_options, &unrecognized);
845 ASSERT (a_seen == 0);
846 ASSERT (b_seen == 0);
847 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
848 ASSERT (q_value == NULL);
849 ASSERT (non_options_count == 0);
850 ASSERT (unrecognized == 0);
851 ASSERT (optind == 2);
853 for (start = 0; start <= 1; start++)
855 const char *p_value = NULL;
856 const char *q_value = NULL;
857 int non_options_count = 0;
858 const char *non_options[10];
859 int unrecognized = 0;
860 int argc = 0;
861 const char *argv[10];
862 a_seen = 0;
863 b_seen = 0;
865 argv[argc++] = "program";
866 argv[argc++] = "-W";
867 argv[argc++] = "p";
868 argv[argc++] = "foo";
869 argv[argc++] = "bar";
870 argv[argc] = NULL;
871 optind = start;
872 getopt_long_loop (argc, argv, "p:W;q:", long_options_required,
873 &p_value, &q_value,
874 &non_options_count, non_options, &unrecognized);
875 ASSERT (a_seen == 0);
876 ASSERT (b_seen == 0);
877 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
878 ASSERT (q_value == NULL);
879 ASSERT (non_options_count == 0);
880 ASSERT (unrecognized == 0);
881 ASSERT (optind == 4);
883 for (start = 0; start <= 1; start++)
885 const char *p_value = NULL;
886 const char *q_value = NULL;
887 int non_options_count = 0;
888 const char *non_options[10];
889 int unrecognized = 0;
890 int argc = 0;
891 const char *argv[10];
892 a_seen = 0;
893 b_seen = 0;
895 argv[argc++] = "program";
896 argv[argc++] = "-ab";
897 argv[argc++] = "-Wq";
898 argv[argc++] = "baz";
899 argv[argc++] = "-W";
900 argv[argc++] = "p=foo";
901 argv[argc++] = "bar";
902 argv[argc] = NULL;
903 optind = start;
904 getopt_long_loop (argc, argv, "W;abp:q:", long_options_required,
905 &p_value, &q_value,
906 &non_options_count, non_options, &unrecognized);
907 ASSERT (a_seen == 1);
908 ASSERT (b_seen == 1);
909 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
910 ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
911 ASSERT (non_options_count == 0);
912 ASSERT (unrecognized == 0);
913 ASSERT (optind == 6);
916 /* Test processing of short options with optional arguments. */
917 for (start = 0; start <= 1; start++)
919 const char *p_value = NULL;
920 const char *q_value = NULL;
921 int non_options_count = 0;
922 const char *non_options[10];
923 int unrecognized = 0;
924 int argc = 0;
925 const char *argv[10];
926 a_seen = 0;
927 b_seen = 0;
929 argv[argc++] = "program";
930 argv[argc++] = "-pfoo";
931 argv[argc++] = "bar";
932 argv[argc] = NULL;
933 optind = start;
934 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
935 &p_value, &q_value,
936 &non_options_count, non_options, &unrecognized);
937 ASSERT (a_seen == 0);
938 ASSERT (b_seen == 0);
939 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
940 ASSERT (q_value == NULL);
941 ASSERT (non_options_count == 0);
942 ASSERT (unrecognized == 0);
943 ASSERT (optind == 2);
945 for (start = 0; start <= 1; start++)
947 const char *p_value = NULL;
948 const char *q_value = NULL;
949 int non_options_count = 0;
950 const char *non_options[10];
951 int unrecognized = 0;
952 int argc = 0;
953 const char *argv[10];
954 a_seen = 0;
955 b_seen = 0;
957 argv[argc++] = "program";
958 argv[argc++] = "-p";
959 argv[argc++] = "foo";
960 argv[argc++] = "bar";
961 argv[argc] = NULL;
962 optind = start;
963 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
964 &p_value, &q_value,
965 &non_options_count, non_options, &unrecognized);
966 ASSERT (a_seen == 0);
967 ASSERT (b_seen == 0);
968 ASSERT (p_value == NULL);
969 ASSERT (q_value == NULL);
970 ASSERT (non_options_count == 0);
971 ASSERT (unrecognized == 0);
972 ASSERT (optind == 2);
974 for (start = 0; start <= 1; start++)
976 const char *p_value = NULL;
977 const char *q_value = NULL;
978 int non_options_count = 0;
979 const char *non_options[10];
980 int unrecognized = 0;
981 int argc = 0;
982 const char *argv[10];
983 a_seen = 0;
984 b_seen = 0;
986 argv[argc++] = "program";
987 argv[argc++] = "-p";
988 argv[argc++] = "-a";
989 argv[argc++] = "bar";
990 argv[argc] = NULL;
991 optind = start;
992 getopt_long_loop (argc, argv, "abp::q::", long_options_optional,
993 &p_value, &q_value,
994 &non_options_count, non_options, &unrecognized);
995 ASSERT (a_seen == 1);
996 ASSERT (b_seen == 0);
997 ASSERT (p_value == NULL);
998 ASSERT (q_value == NULL);
999 ASSERT (non_options_count == 0);
1000 ASSERT (unrecognized == 0);
1001 ASSERT (optind == 3);
1004 /* Test processing of long options with optional arguments. */
1005 for (start = 0; start <= 1; start++)
1007 const char *p_value = NULL;
1008 const char *q_value = NULL;
1009 int non_options_count = 0;
1010 const char *non_options[10];
1011 int unrecognized = 0;
1012 int argc = 0;
1013 const char *argv[10];
1014 a_seen = 0;
1015 b_seen = 0;
1017 argv[argc++] = "program";
1018 argv[argc++] = "--p=foo";
1019 argv[argc++] = "bar";
1020 argv[argc] = NULL;
1021 optind = start;
1022 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
1023 &p_value, &q_value,
1024 &non_options_count, non_options, &unrecognized);
1025 ASSERT (a_seen == 0);
1026 ASSERT (b_seen == 0);
1027 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1028 ASSERT (q_value == NULL);
1029 ASSERT (non_options_count == 0);
1030 ASSERT (unrecognized == 0);
1031 ASSERT (optind == 2);
1033 for (start = 0; start <= 1; start++)
1035 const char *p_value = NULL;
1036 const char *q_value = NULL;
1037 int non_options_count = 0;
1038 const char *non_options[10];
1039 int unrecognized = 0;
1040 int argc = 0;
1041 const char *argv[10];
1042 a_seen = 0;
1043 b_seen = 0;
1045 argv[argc++] = "program";
1046 argv[argc++] = "--p";
1047 argv[argc++] = "foo";
1048 argv[argc++] = "bar";
1049 argv[argc] = NULL;
1050 optind = start;
1051 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
1052 &p_value, &q_value,
1053 &non_options_count, non_options, &unrecognized);
1054 ASSERT (a_seen == 0);
1055 ASSERT (b_seen == 0);
1056 ASSERT (p_value == NULL);
1057 ASSERT (q_value == NULL);
1058 ASSERT (non_options_count == 0);
1059 ASSERT (unrecognized == 0);
1060 ASSERT (optind == 2);
1062 for (start = 0; start <= 1; start++)
1064 const char *p_value = NULL;
1065 const char *q_value = NULL;
1066 int non_options_count = 0;
1067 const char *non_options[10];
1068 int unrecognized = 0;
1069 int argc = 0;
1070 const char *argv[10];
1071 a_seen = 0;
1072 b_seen = 0;
1074 argv[argc++] = "program";
1075 argv[argc++] = "--p=";
1076 argv[argc++] = "foo";
1077 argv[argc++] = "bar";
1078 argv[argc] = NULL;
1079 optind = start;
1080 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
1081 &p_value, &q_value,
1082 &non_options_count, non_options, &unrecognized);
1083 ASSERT (a_seen == 0);
1084 ASSERT (b_seen == 0);
1085 ASSERT (p_value != NULL && *p_value == '\0');
1086 ASSERT (q_value == NULL);
1087 ASSERT (non_options_count == 0);
1088 ASSERT (unrecognized == 0);
1089 ASSERT (optind == 2);
1091 for (start = 0; start <= 1; start++)
1093 const char *p_value = NULL;
1094 const char *q_value = NULL;
1095 int non_options_count = 0;
1096 const char *non_options[10];
1097 int unrecognized = 0;
1098 int argc = 0;
1099 const char *argv[10];
1100 a_seen = 0;
1101 b_seen = 0;
1103 argv[argc++] = "program";
1104 argv[argc++] = "--p";
1105 argv[argc++] = "-a";
1106 argv[argc++] = "bar";
1107 argv[argc] = NULL;
1108 optind = start;
1109 getopt_long_loop (argc, argv, "abp::q::", long_options_optional,
1110 &p_value, &q_value,
1111 &non_options_count, non_options, &unrecognized);
1112 ASSERT (a_seen == 1);
1113 ASSERT (b_seen == 0);
1114 ASSERT (p_value == NULL);
1115 ASSERT (q_value == NULL);
1116 ASSERT (non_options_count == 0);
1117 ASSERT (unrecognized == 0);
1118 ASSERT (optind == 3);
1121 /* Test processing of long options with optional arguments via -W. */
1122 for (start = 0; start <= 1; start++)
1124 const char *p_value = NULL;
1125 const char *q_value = NULL;
1126 int non_options_count = 0;
1127 const char *non_options[10];
1128 int unrecognized = 0;
1129 int argc = 0;
1130 const char *argv[10];
1131 a_seen = 0;
1132 b_seen = 0;
1134 argv[argc++] = "program";
1135 argv[argc++] = "-Wp=foo";
1136 argv[argc++] = "bar";
1137 argv[argc] = NULL;
1138 optind = start;
1139 getopt_long_loop (argc, argv, "p::q::W;", long_options_optional,
1140 &p_value, &q_value,
1141 &non_options_count, non_options, &unrecognized);
1142 ASSERT (a_seen == 0);
1143 ASSERT (b_seen == 0);
1144 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1145 ASSERT (q_value == NULL);
1146 ASSERT (non_options_count == 0);
1147 ASSERT (unrecognized == 0);
1148 ASSERT (optind == 2);
1150 for (start = 0; start <= 1; start++)
1152 const char *p_value = NULL;
1153 const char *q_value = NULL;
1154 int non_options_count = 0;
1155 const char *non_options[10];
1156 int unrecognized = 0;
1157 int argc = 0;
1158 const char *argv[10];
1159 a_seen = 0;
1160 b_seen = 0;
1162 argv[argc++] = "program";
1163 argv[argc++] = "-Wp";
1164 argv[argc++] = "foo";
1165 argv[argc++] = "bar";
1166 argv[argc] = NULL;
1167 optind = start;
1168 getopt_long_loop (argc, argv, "p::q::W;", long_options_optional,
1169 &p_value, &q_value,
1170 &non_options_count, non_options, &unrecognized);
1171 ASSERT (a_seen == 0);
1172 ASSERT (b_seen == 0);
1173 ASSERT (p_value == NULL);
1174 ASSERT (q_value == NULL);
1175 ASSERT (non_options_count == 0);
1176 ASSERT (unrecognized == 0);
1177 ASSERT (optind == 2);
1179 for (start = 0; start <= 1; start++)
1181 const char *p_value = NULL;
1182 const char *q_value = NULL;
1183 int non_options_count = 0;
1184 const char *non_options[10];
1185 int unrecognized = 0;
1186 int argc = 0;
1187 const char *argv[10];
1188 a_seen = 0;
1189 b_seen = 0;
1191 argv[argc++] = "program";
1192 argv[argc++] = "-Wp=";
1193 argv[argc++] = "foo";
1194 argv[argc++] = "bar";
1195 argv[argc] = NULL;
1196 optind = start;
1197 getopt_long_loop (argc, argv, "W;p::q::", long_options_optional,
1198 &p_value, &q_value,
1199 &non_options_count, non_options, &unrecognized);
1200 ASSERT (a_seen == 0);
1201 ASSERT (b_seen == 0);
1202 ASSERT (p_value != NULL && *p_value == '\0');
1203 ASSERT (q_value == NULL);
1204 ASSERT (non_options_count == 0);
1205 ASSERT (unrecognized == 0);
1206 ASSERT (optind == 2);
1208 for (start = 0; start <= 1; start++)
1210 const char *p_value = NULL;
1211 const char *q_value = NULL;
1212 int non_options_count = 0;
1213 const char *non_options[10];
1214 int unrecognized = 0;
1215 int argc = 0;
1216 const char *argv[10];
1217 a_seen = 0;
1218 b_seen = 0;
1220 argv[argc++] = "program";
1221 argv[argc++] = "-W";
1222 argv[argc++] = "p=";
1223 argv[argc++] = "foo";
1224 argv[argc++] = "bar";
1225 argv[argc] = NULL;
1226 optind = start;
1227 getopt_long_loop (argc, argv, "W;p::q::", long_options_optional,
1228 &p_value, &q_value,
1229 &non_options_count, non_options, &unrecognized);
1230 ASSERT (a_seen == 0);
1231 ASSERT (b_seen == 0);
1232 ASSERT (p_value != NULL && *p_value == '\0');
1233 ASSERT (q_value == NULL);
1234 ASSERT (non_options_count == 0);
1235 ASSERT (unrecognized == 0);
1236 ASSERT (optind == 3);
1238 for (start = 0; start <= 1; start++)
1240 const char *p_value = NULL;
1241 const char *q_value = NULL;
1242 int non_options_count = 0;
1243 const char *non_options[10];
1244 int unrecognized = 0;
1245 int argc = 0;
1246 const char *argv[10];
1247 a_seen = 0;
1248 b_seen = 0;
1250 argv[argc++] = "program";
1251 argv[argc++] = "-W";
1252 argv[argc++] = "p";
1253 argv[argc++] = "-a";
1254 argv[argc++] = "bar";
1255 argv[argc] = NULL;
1256 optind = start;
1257 getopt_long_loop (argc, argv, "W;abp::q::", long_options_optional,
1258 &p_value, &q_value,
1259 &non_options_count, non_options, &unrecognized);
1260 ASSERT (a_seen == 1);
1261 ASSERT (b_seen == 0);
1262 /* ASSERT (p_value == NULL); */
1263 ASSERT (q_value == NULL);
1264 ASSERT (non_options_count == 0);
1265 ASSERT (unrecognized == 0);
1266 ASSERT (optind == 4);
1269 /* Check that invalid options are recognized. */
1270 for (start = 0; start <= 1; start++)
1272 const char *p_value = NULL;
1273 const char *q_value = NULL;
1274 int non_options_count = 0;
1275 const char *non_options[10];
1276 int unrecognized = 0;
1277 int argc = 0;
1278 const char *argv[10];
1279 a_seen = 0;
1280 b_seen = 0;
1282 argv[argc++] = "program";
1283 argv[argc++] = "-p";
1284 argv[argc++] = "foo";
1285 argv[argc++] = "-x";
1286 argv[argc++] = "-a";
1287 argv[argc++] = "bar";
1288 argv[argc] = NULL;
1289 optind = start;
1290 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1291 &p_value, &q_value,
1292 &non_options_count, non_options, &unrecognized);
1293 ASSERT (a_seen == 1);
1294 ASSERT (b_seen == 0);
1295 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1296 ASSERT (q_value == NULL);
1297 ASSERT (non_options_count == 0);
1298 ASSERT (unrecognized == 'x');
1299 ASSERT (optind == 5);
1301 for (start = 0; start <= 1; start++)
1303 const char *p_value = NULL;
1304 const char *q_value = NULL;
1305 int non_options_count = 0;
1306 const char *non_options[10];
1307 int unrecognized = 0;
1308 int argc = 0;
1309 const char *argv[10];
1310 a_seen = 0;
1311 b_seen = 0;
1313 argv[argc++] = "program";
1314 argv[argc++] = "-p";
1315 argv[argc++] = "foo";
1316 argv[argc++] = "-:";
1317 argv[argc++] = "-a";
1318 argv[argc++] = "bar";
1319 argv[argc] = NULL;
1320 optind = start;
1321 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1322 &p_value, &q_value,
1323 &non_options_count, non_options, &unrecognized);
1324 ASSERT (a_seen == 1);
1325 ASSERT (b_seen == 0);
1326 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1327 ASSERT (q_value == NULL);
1328 ASSERT (non_options_count == 0);
1329 ASSERT (unrecognized == ':');
1330 ASSERT (optind == 5);
1333 /* Check that unexpected arguments are recognized. */
1334 for (start = 0; start <= 1; start++)
1336 const char *p_value = NULL;
1337 const char *q_value = NULL;
1338 int non_options_count = 0;
1339 const char *non_options[10];
1340 int unrecognized = 0;
1341 int argc = 0;
1342 const char *argv[10];
1343 a_seen = 0;
1344 b_seen = 0;
1346 argv[argc++] = "program";
1347 argv[argc++] = "-p";
1348 argv[argc++] = "foo";
1349 argv[argc++] = "--a=";
1350 argv[argc++] = "bar";
1351 argv[argc] = NULL;
1352 optind = start;
1353 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1354 &p_value, &q_value,
1355 &non_options_count, non_options, &unrecognized);
1356 ASSERT (a_seen == 0);
1357 ASSERT (b_seen == 0);
1358 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1359 ASSERT (q_value == NULL);
1360 ASSERT (non_options_count == 0);
1361 ASSERT (unrecognized == 'a');
1362 ASSERT (optind == 4);
1364 for (start = 0; start <= 1; start++)
1366 const char *p_value = NULL;
1367 const char *q_value = NULL;
1368 int non_options_count = 0;
1369 const char *non_options[10];
1370 int unrecognized = 0;
1371 int argc = 0;
1372 const char *argv[10];
1373 a_seen = 0;
1374 b_seen = 0;
1376 argv[argc++] = "program";
1377 argv[argc++] = "-p";
1378 argv[argc++] = "foo";
1379 argv[argc++] = "--b=";
1380 argv[argc++] = "bar";
1381 argv[argc] = NULL;
1382 optind = start;
1383 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1384 &p_value, &q_value,
1385 &non_options_count, non_options, &unrecognized);
1386 ASSERT (a_seen == 0);
1387 ASSERT (b_seen == 0);
1388 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1389 ASSERT (q_value == NULL);
1390 ASSERT (non_options_count == 0);
1391 /* When flag is non-zero, glibc sets optopt anyway, but BSD
1392 leaves optopt unchanged. */
1393 ASSERT (unrecognized == 1 || unrecognized == 0);
1394 ASSERT (optind == 4);
1397 /* Check that by default, non-options arguments are moved to the end. */
1398 for (start = 0; start <= 1; start++)
1400 const char *p_value = NULL;
1401 const char *q_value = NULL;
1402 int non_options_count = 0;
1403 const char *non_options[10];
1404 int unrecognized = 0;
1405 int argc = 0;
1406 const char *argv[10];
1407 a_seen = 0;
1408 b_seen = 0;
1410 argv[argc++] = "program";
1411 argv[argc++] = "donald";
1412 argv[argc++] = "-p";
1413 argv[argc++] = "billy";
1414 argv[argc++] = "duck";
1415 argv[argc++] = "-a";
1416 argv[argc++] = "bar";
1417 argv[argc] = NULL;
1418 optind = start;
1419 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1420 &p_value, &q_value,
1421 &non_options_count, non_options, &unrecognized);
1422 ASSERT (strcmp (argv[0], "program") == 0);
1423 ASSERT (strcmp (argv[1], "-p") == 0);
1424 ASSERT (strcmp (argv[2], "billy") == 0);
1425 ASSERT (strcmp (argv[3], "-a") == 0);
1426 ASSERT (strcmp (argv[4], "donald") == 0);
1427 ASSERT (strcmp (argv[5], "duck") == 0);
1428 ASSERT (strcmp (argv[6], "bar") == 0);
1429 ASSERT (argv[7] == NULL);
1430 ASSERT (a_seen == 1);
1431 ASSERT (b_seen == 0);
1432 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1433 ASSERT (q_value == NULL);
1434 ASSERT (non_options_count == 0);
1435 ASSERT (unrecognized == 0);
1436 ASSERT (optind == 4);
1439 /* Check that '--' ends the argument processing. */
1440 for (start = 0; start <= 1; start++)
1442 const char *p_value = NULL;
1443 const char *q_value = NULL;
1444 int non_options_count = 0;
1445 const char *non_options[10];
1446 int unrecognized = 0;
1447 int argc = 0;
1448 const char *argv[20];
1449 a_seen = 0;
1450 b_seen = 0;
1452 argv[argc++] = "program";
1453 argv[argc++] = "donald";
1454 argv[argc++] = "-p";
1455 argv[argc++] = "billy";
1456 argv[argc++] = "duck";
1457 argv[argc++] = "-a";
1458 argv[argc++] = "--";
1459 argv[argc++] = "-b";
1460 argv[argc++] = "foo";
1461 argv[argc++] = "-q";
1462 argv[argc++] = "johnny";
1463 argv[argc++] = "bar";
1464 argv[argc] = NULL;
1465 optind = start;
1466 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1467 &p_value, &q_value,
1468 &non_options_count, non_options, &unrecognized);
1469 ASSERT (strcmp (argv[0], "program") == 0);
1470 ASSERT (strcmp (argv[1], "-p") == 0);
1471 ASSERT (strcmp (argv[2], "billy") == 0);
1472 ASSERT (strcmp (argv[3], "-a") == 0);
1473 ASSERT (strcmp (argv[4], "--") == 0);
1474 ASSERT (strcmp (argv[5], "donald") == 0);
1475 ASSERT (strcmp (argv[6], "duck") == 0);
1476 ASSERT (strcmp (argv[7], "-b") == 0);
1477 ASSERT (strcmp (argv[8], "foo") == 0);
1478 ASSERT (strcmp (argv[9], "-q") == 0);
1479 ASSERT (strcmp (argv[10], "johnny") == 0);
1480 ASSERT (strcmp (argv[11], "bar") == 0);
1481 ASSERT (argv[12] == NULL);
1482 ASSERT (a_seen == 1);
1483 ASSERT (b_seen == 0);
1484 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1485 ASSERT (q_value == NULL);
1486 ASSERT (non_options_count == 0);
1487 ASSERT (unrecognized == 0);
1488 ASSERT (optind == 5);
1491 /* Check that the '-' flag causes non-options to be returned in order. */
1492 for (start = 0; start <= 1; start++)
1494 const char *p_value = NULL;
1495 const char *q_value = NULL;
1496 int non_options_count = 0;
1497 const char *non_options[10];
1498 int unrecognized = 0;
1499 int argc = 0;
1500 const char *argv[10];
1501 a_seen = 0;
1502 b_seen = 0;
1504 argv[argc++] = "program";
1505 argv[argc++] = "donald";
1506 argv[argc++] = "-p";
1507 argv[argc++] = "billy";
1508 argv[argc++] = "duck";
1509 argv[argc++] = "-a";
1510 argv[argc++] = "bar";
1511 argv[argc] = NULL;
1512 optind = start;
1513 getopt_long_loop (argc, argv, "-abp:q:", long_options_required,
1514 &p_value, &q_value,
1515 &non_options_count, non_options, &unrecognized);
1516 ASSERT (strcmp (argv[0], "program") == 0);
1517 ASSERT (strcmp (argv[1], "donald") == 0);
1518 ASSERT (strcmp (argv[2], "-p") == 0);
1519 ASSERT (strcmp (argv[3], "billy") == 0);
1520 ASSERT (strcmp (argv[4], "duck") == 0);
1521 ASSERT (strcmp (argv[5], "-a") == 0);
1522 ASSERT (strcmp (argv[6], "bar") == 0);
1523 ASSERT (argv[7] == NULL);
1524 ASSERT (a_seen == 1);
1525 ASSERT (b_seen == 0);
1526 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1527 ASSERT (q_value == NULL);
1528 ASSERT (non_options_count == 3);
1529 ASSERT (strcmp (non_options[0], "donald") == 0);
1530 ASSERT (strcmp (non_options[1], "duck") == 0);
1531 ASSERT (strcmp (non_options[2], "bar") == 0);
1532 ASSERT (unrecognized == 0);
1533 ASSERT (optind == 7);
1536 /* Check that '--' ends the argument processing. */
1537 for (start = 0; start <= 1; start++)
1539 const char *p_value = NULL;
1540 const char *q_value = NULL;
1541 int non_options_count = 0;
1542 const char *non_options[10];
1543 int unrecognized = 0;
1544 int argc = 0;
1545 const char *argv[20];
1546 a_seen = 0;
1547 b_seen = 0;
1549 argv[argc++] = "program";
1550 argv[argc++] = "donald";
1551 argv[argc++] = "-p";
1552 argv[argc++] = "billy";
1553 argv[argc++] = "duck";
1554 argv[argc++] = "-a";
1555 argv[argc++] = "--";
1556 argv[argc++] = "-b";
1557 argv[argc++] = "foo";
1558 argv[argc++] = "-q";
1559 argv[argc++] = "johnny";
1560 argv[argc++] = "bar";
1561 argv[argc] = NULL;
1562 optind = start;
1563 getopt_long_loop (argc, argv, "-abp:q:", long_options_required,
1564 &p_value, &q_value,
1565 &non_options_count, non_options, &unrecognized);
1566 ASSERT (strcmp (argv[0], "program") == 0);
1567 ASSERT (strcmp (argv[1], "donald") == 0);
1568 ASSERT (strcmp (argv[2], "-p") == 0);
1569 ASSERT (strcmp (argv[3], "billy") == 0);
1570 ASSERT (strcmp (argv[4], "duck") == 0);
1571 ASSERT (strcmp (argv[5], "-a") == 0);
1572 ASSERT (strcmp (argv[6], "--") == 0);
1573 ASSERT (strcmp (argv[7], "-b") == 0);
1574 ASSERT (strcmp (argv[8], "foo") == 0);
1575 ASSERT (strcmp (argv[9], "-q") == 0);
1576 ASSERT (strcmp (argv[10], "johnny") == 0);
1577 ASSERT (strcmp (argv[11], "bar") == 0);
1578 ASSERT (argv[12] == NULL);
1579 ASSERT (a_seen == 1);
1580 ASSERT (b_seen == 0);
1581 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1582 ASSERT (q_value == NULL);
1583 if (non_options_count == 2)
1585 /* glibc behaviour. */
1586 ASSERT (non_options_count == 2);
1587 ASSERT (strcmp (non_options[0], "donald") == 0);
1588 ASSERT (strcmp (non_options[1], "duck") == 0);
1589 ASSERT (unrecognized == 0);
1590 ASSERT (optind == 7);
1592 else
1594 /* Another valid behaviour. */
1595 ASSERT (non_options_count == 7);
1596 ASSERT (strcmp (non_options[0], "donald") == 0);
1597 ASSERT (strcmp (non_options[1], "duck") == 0);
1598 ASSERT (strcmp (non_options[2], "-b") == 0);
1599 ASSERT (strcmp (non_options[3], "foo") == 0);
1600 ASSERT (strcmp (non_options[4], "-q") == 0);
1601 ASSERT (strcmp (non_options[5], "johnny") == 0);
1602 ASSERT (strcmp (non_options[6], "bar") == 0);
1603 ASSERT (unrecognized == 0);
1604 ASSERT (optind == 12);
1608 /* Check that the '-' flag has to come first. */
1609 for (start = 0; start <= 1; start++)
1611 const char *p_value = NULL;
1612 const char *q_value = NULL;
1613 int non_options_count = 0;
1614 const char *non_options[10];
1615 int unrecognized = 0;
1616 int argc = 0;
1617 const char *argv[10];
1618 a_seen = 0;
1619 b_seen = 0;
1621 argv[argc++] = "program";
1622 argv[argc++] = "donald";
1623 argv[argc++] = "-p";
1624 argv[argc++] = "billy";
1625 argv[argc++] = "duck";
1626 argv[argc++] = "-a";
1627 argv[argc++] = "bar";
1628 argv[argc] = NULL;
1629 optind = start;
1630 getopt_long_loop (argc, argv, "abp:q:-", long_options_required,
1631 &p_value, &q_value,
1632 &non_options_count, non_options, &unrecognized);
1633 ASSERT (strcmp (argv[0], "program") == 0);
1634 ASSERT (strcmp (argv[1], "-p") == 0);
1635 ASSERT (strcmp (argv[2], "billy") == 0);
1636 ASSERT (strcmp (argv[3], "-a") == 0);
1637 ASSERT (strcmp (argv[4], "donald") == 0);
1638 ASSERT (strcmp (argv[5], "duck") == 0);
1639 ASSERT (strcmp (argv[6], "bar") == 0);
1640 ASSERT (argv[7] == NULL);
1641 ASSERT (a_seen == 1);
1642 ASSERT (b_seen == 0);
1643 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1644 ASSERT (q_value == NULL);
1645 ASSERT (non_options_count == 0);
1646 ASSERT (unrecognized == 0);
1647 ASSERT (optind == 4);
1650 /* Check that the '+' flag causes the first non-option to terminate the
1651 loop. */
1652 for (start = 0; start <= 1; start++)
1654 const char *p_value = NULL;
1655 const char *q_value = NULL;
1656 int non_options_count = 0;
1657 const char *non_options[10];
1658 int unrecognized = 0;
1659 int argc = 0;
1660 const char *argv[10];
1661 a_seen = 0;
1662 b_seen = 0;
1664 argv[argc++] = "program";
1665 argv[argc++] = "donald";
1666 argv[argc++] = "-p";
1667 argv[argc++] = "billy";
1668 argv[argc++] = "duck";
1669 argv[argc++] = "-a";
1670 argv[argc++] = "bar";
1671 argv[argc] = NULL;
1672 optind = start;
1673 getopt_long_loop (argc, argv, "+abp:q:", long_options_required,
1674 &p_value, &q_value,
1675 &non_options_count, non_options, &unrecognized);
1676 ASSERT (strcmp (argv[0], "program") == 0);
1677 ASSERT (strcmp (argv[1], "donald") == 0);
1678 ASSERT (strcmp (argv[2], "-p") == 0);
1679 ASSERT (strcmp (argv[3], "billy") == 0);
1680 ASSERT (strcmp (argv[4], "duck") == 0);
1681 ASSERT (strcmp (argv[5], "-a") == 0);
1682 ASSERT (strcmp (argv[6], "bar") == 0);
1683 ASSERT (argv[7] == NULL);
1684 ASSERT (a_seen == 0);
1685 ASSERT (b_seen == 0);
1686 ASSERT (p_value == NULL);
1687 ASSERT (q_value == NULL);
1688 ASSERT (non_options_count == 0);
1689 ASSERT (unrecognized == 0);
1690 ASSERT (optind == 1);
1692 for (start = 0; start <= 1; start++)
1694 const char *p_value = NULL;
1695 const char *q_value = NULL;
1696 int non_options_count = 0;
1697 const char *non_options[10];
1698 int unrecognized = 0;
1699 int argc = 0;
1700 const char *argv[10];
1701 a_seen = 0;
1702 b_seen = 0;
1704 argv[argc++] = "program";
1705 argv[argc++] = "-+";
1706 argv[argc] = NULL;
1707 optind = start;
1708 getopt_long_loop (argc, argv, "+abp:q:", long_options_required,
1709 &p_value, &q_value,
1710 &non_options_count, non_options, &unrecognized);
1711 ASSERT (a_seen == 0);
1712 ASSERT (b_seen == 0);
1713 ASSERT (p_value == NULL);
1714 ASSERT (q_value == NULL);
1715 ASSERT (non_options_count == 0);
1716 ASSERT (unrecognized == '+');
1717 ASSERT (optind == 2);
1720 /* Check that '--' ends the argument processing. */
1721 for (start = 0; start <= 1; start++)
1723 const char *p_value = NULL;
1724 const char *q_value = NULL;
1725 int non_options_count = 0;
1726 const char *non_options[10];
1727 int unrecognized = 0;
1728 int argc = 0;
1729 const char *argv[20];
1730 a_seen = 0;
1731 b_seen = 0;
1733 argv[argc++] = "program";
1734 argv[argc++] = "donald";
1735 argv[argc++] = "-p";
1736 argv[argc++] = "billy";
1737 argv[argc++] = "duck";
1738 argv[argc++] = "-a";
1739 argv[argc++] = "--";
1740 argv[argc++] = "-b";
1741 argv[argc++] = "foo";
1742 argv[argc++] = "-q";
1743 argv[argc++] = "johnny";
1744 argv[argc++] = "bar";
1745 argv[argc] = NULL;
1746 optind = start;
1747 getopt_long_loop (argc, argv, "+abp:q:", long_options_required,
1748 &p_value, &q_value,
1749 &non_options_count, non_options, &unrecognized);
1750 ASSERT (strcmp (argv[0], "program") == 0);
1751 ASSERT (strcmp (argv[1], "donald") == 0);
1752 ASSERT (strcmp (argv[2], "-p") == 0);
1753 ASSERT (strcmp (argv[3], "billy") == 0);
1754 ASSERT (strcmp (argv[4], "duck") == 0);
1755 ASSERT (strcmp (argv[5], "-a") == 0);
1756 ASSERT (strcmp (argv[6], "--") == 0);
1757 ASSERT (strcmp (argv[7], "-b") == 0);
1758 ASSERT (strcmp (argv[8], "foo") == 0);
1759 ASSERT (strcmp (argv[9], "-q") == 0);
1760 ASSERT (strcmp (argv[10], "johnny") == 0);
1761 ASSERT (strcmp (argv[11], "bar") == 0);
1762 ASSERT (argv[12] == NULL);
1763 ASSERT (a_seen == 0);
1764 ASSERT (b_seen == 0);
1765 ASSERT (p_value == NULL);
1766 ASSERT (q_value == NULL);
1767 ASSERT (non_options_count == 0);
1768 ASSERT (unrecognized == 0);
1769 ASSERT (optind == 1);
1772 /* Check that the '+' flag has to come first. */
1773 for (start = 0; start <= 1; start++)
1775 const char *p_value = NULL;
1776 const char *q_value = NULL;
1777 int non_options_count = 0;
1778 const char *non_options[10];
1779 int unrecognized = 0;
1780 int argc = 0;
1781 const char *argv[10];
1782 a_seen = 0;
1783 b_seen = 0;
1785 argv[argc++] = "program";
1786 argv[argc++] = "donald";
1787 argv[argc++] = "-p";
1788 argv[argc++] = "billy";
1789 argv[argc++] = "duck";
1790 argv[argc++] = "-a";
1791 argv[argc++] = "bar";
1792 argv[argc] = NULL;
1793 optind = start;
1794 getopt_long_loop (argc, argv, "abp:q:+", long_options_required,
1795 &p_value, &q_value,
1796 &non_options_count, non_options, &unrecognized);
1797 ASSERT (strcmp (argv[0], "program") == 0);
1798 ASSERT (strcmp (argv[1], "-p") == 0);
1799 ASSERT (strcmp (argv[2], "billy") == 0);
1800 ASSERT (strcmp (argv[3], "-a") == 0);
1801 ASSERT (strcmp (argv[4], "donald") == 0);
1802 ASSERT (strcmp (argv[5], "duck") == 0);
1803 ASSERT (strcmp (argv[6], "bar") == 0);
1804 ASSERT (argv[7] == NULL);
1805 ASSERT (a_seen == 1);
1806 ASSERT (b_seen == 0);
1807 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1808 ASSERT (q_value == NULL);
1809 ASSERT (non_options_count == 0);
1810 ASSERT (unrecognized == 0);
1811 ASSERT (optind == 4);
1815 /* Test behavior of getopt_long when POSIXLY_CORRECT is set in the
1816 environment. Options with optional arguments should not change
1817 behavior just because of an environment variable.
1818 http://lists.gnu.org/archive/html/bug-m4/2006-09/msg00028.html */
1819 static void
1820 test_getopt_long_posix (void)
1822 int start;
1824 /* Check that POSIXLY_CORRECT stops parsing the same as leading '+'. */
1825 for (start = 0; start <= 1; start++)
1827 const char *p_value = NULL;
1828 const char *q_value = NULL;
1829 int non_options_count = 0;
1830 const char *non_options[10];
1831 int unrecognized = 0;
1832 int argc = 0;
1833 const char *argv[10];
1834 a_seen = 0;
1835 b_seen = 0;
1837 argv[argc++] = "program";
1838 argv[argc++] = "donald";
1839 argv[argc++] = "-p";
1840 argv[argc++] = "billy";
1841 argv[argc++] = "duck";
1842 argv[argc++] = "-a";
1843 argv[argc++] = "bar";
1844 argv[argc] = NULL;
1845 optind = start;
1846 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1847 &p_value, &q_value,
1848 &non_options_count, non_options, &unrecognized);
1849 ASSERT (strcmp (argv[0], "program") == 0);
1850 ASSERT (strcmp (argv[1], "donald") == 0);
1851 ASSERT (strcmp (argv[2], "-p") == 0);
1852 ASSERT (strcmp (argv[3], "billy") == 0);
1853 ASSERT (strcmp (argv[4], "duck") == 0);
1854 ASSERT (strcmp (argv[5], "-a") == 0);
1855 ASSERT (strcmp (argv[6], "bar") == 0);
1856 ASSERT (argv[7] == NULL);
1857 ASSERT (a_seen == 0);
1858 ASSERT (b_seen == 0);
1859 ASSERT (p_value == NULL);
1860 ASSERT (q_value == NULL);
1861 ASSERT (non_options_count == 0);
1862 ASSERT (unrecognized == 0);
1863 ASSERT (optind == 1);
1866 /* Check that POSIXLY_CORRECT doesn't change optional arguments. */
1867 for (start = 0; start <= 1; start++)
1869 const char *p_value = NULL;
1870 const char *q_value = NULL;
1871 int non_options_count = 0;
1872 const char *non_options[10];
1873 int unrecognized = 0;
1874 int argc = 0;
1875 const char *argv[10];
1876 a_seen = 0;
1877 b_seen = 0;
1879 argv[argc++] = "program";
1880 argv[argc++] = "-p";
1881 argv[argc++] = "billy";
1882 argv[argc] = NULL;
1883 optind = start;
1884 getopt_long_loop (argc, argv, "p::", long_options_required,
1885 &p_value, &q_value,
1886 &non_options_count, non_options, &unrecognized);
1887 ASSERT (a_seen == 0);
1888 ASSERT (b_seen == 0);
1889 ASSERT (p_value == NULL);
1890 ASSERT (q_value == NULL);
1891 ASSERT (non_options_count == 0);
1892 ASSERT (unrecognized == 0);
1893 ASSERT (optind == 2);
1896 /* Check that leading - still sees options after non-options. */
1897 for (start = 0; start <= 1; start++)
1899 const char *p_value = NULL;
1900 const char *q_value = NULL;
1901 int non_options_count = 0;
1902 const char *non_options[10];
1903 int unrecognized = 0;
1904 int argc = 0;
1905 const char *argv[10];
1906 a_seen = 0;
1907 b_seen = 0;
1909 argv[argc++] = "program";
1910 argv[argc++] = "-a";
1911 argv[argc++] = "billy";
1912 argv[argc++] = "-b";
1913 argv[argc] = NULL;
1914 optind = start;
1915 getopt_long_loop (argc, argv, "-ab", long_options_required,
1916 &p_value, &q_value,
1917 &non_options_count, non_options, &unrecognized);
1918 ASSERT (a_seen == 1);
1919 ASSERT (b_seen == 1);
1920 ASSERT (p_value == NULL);
1921 ASSERT (q_value == NULL);
1922 ASSERT (non_options_count == 1);
1923 ASSERT (strcmp (non_options[0], "billy") == 0);
1924 ASSERT (unrecognized == 0);
1925 ASSERT (optind == 4);
1929 /* Reduce casting, so we can use string literals elsewhere.
1930 getopt_long_only takes an array of char*, but luckily does not
1931 modify those elements, so we can pass const char*. */
1932 static int
1933 do_getopt_long_only (int argc, const char **argv, const char *shortopts,
1934 const struct option *longopts, int *longind)
1936 return getopt_long_only (argc, (char **) argv, shortopts, longopts, longind);
1939 static void
1940 test_getopt_long_only (void)
1942 /* Test disambiguation of options. */
1944 int argc = 0;
1945 const char *argv[10];
1946 int option_index;
1947 int c;
1949 argv[argc++] = "program";
1950 argv[argc++] = "-x";
1951 argv[argc] = NULL;
1952 optind = 1;
1953 opterr = 0;
1954 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
1955 &option_index);
1956 ASSERT (c == '?');
1957 ASSERT (optopt == 0);
1960 int argc = 0;
1961 const char *argv[10];
1962 int option_index;
1963 int c;
1965 argv[argc++] = "program";
1966 argv[argc++] = "-x";
1967 argv[argc] = NULL;
1968 optind = 1;
1969 opterr = 0;
1970 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
1971 &option_index);
1972 ASSERT (c == 'x');
1973 ASSERT (optopt == 0);
1976 int argc = 0;
1977 const char *argv[10];
1978 int option_index;
1979 int c;
1981 argv[argc++] = "program";
1982 argv[argc++] = "--x";
1983 argv[argc] = NULL;
1984 optind = 1;
1985 opterr = 0;
1986 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
1987 &option_index);
1988 ASSERT (c == '?');
1989 ASSERT (optopt == 0);
1992 int argc = 0;
1993 const char *argv[10];
1994 int option_index;
1995 int c;
1997 argv[argc++] = "program";
1998 argv[argc++] = "-b";
1999 argv[argc] = NULL;
2000 optind = 1;
2001 opterr = 0;
2002 b_seen = 0;
2003 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
2004 &option_index);
2005 ASSERT (c == 'b');
2006 ASSERT (b_seen == 0);
2009 int argc = 0;
2010 const char *argv[10];
2011 int option_index;
2012 int c;
2014 argv[argc++] = "program";
2015 argv[argc++] = "--b";
2016 argv[argc] = NULL;
2017 optind = 1;
2018 opterr = 0;
2019 b_seen = 0;
2020 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
2021 &option_index);
2022 ASSERT (c == 0);
2023 ASSERT (b_seen == 1);
2026 int argc = 0;
2027 const char *argv[10];
2028 int option_index;
2029 int c;
2031 argv[argc++] = "program";
2032 argv[argc++] = "-xt";
2033 argv[argc] = NULL;
2034 optind = 1;
2035 opterr = 0;
2036 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
2037 &option_index);
2038 ASSERT (c == '?');
2039 ASSERT (optopt == 0);
2042 int argc = 0;
2043 const char *argv[10];
2044 int option_index;
2045 int c;
2047 argv[argc++] = "program";
2048 argv[argc++] = "-xt";
2049 argv[argc] = NULL;
2050 optind = 1;
2051 opterr = 0;
2052 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
2053 &option_index);
2054 ASSERT (c == '?');
2055 ASSERT (optopt == 0);
2058 int argc = 0;
2059 const char *argv[10];
2060 int option_index;
2061 int c;
2063 argv[argc++] = "program";
2064 argv[argc++] = "-xtra";
2065 argv[argc] = NULL;
2066 optind = 1;
2067 opterr = 0;
2068 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
2069 &option_index);
2070 ASSERT (c == 1001);
2073 int argc = 0;
2074 const char *argv[10];
2075 int option_index;
2076 int c;
2078 argv[argc++] = "program";
2079 argv[argc++] = "-xtreme";
2080 argv[argc] = NULL;
2081 optind = 1;
2082 opterr = 0;
2083 c = do_getopt_long_only (argc, argv, "abx:", long_options_required,
2084 &option_index);
2085 ASSERT (c == 1002);
2088 int argc = 0;
2089 const char *argv[10];
2090 int option_index;
2091 int c;
2093 argv[argc++] = "program";
2094 argv[argc++] = "-xtremel";
2095 argv[argc] = NULL;
2096 optind = 1;
2097 opterr = 0;
2098 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
2099 &option_index);
2100 /* glibc getopt_long_only is intentionally different from
2101 getopt_long when handling a prefix that is common to two
2102 spellings, when both spellings have the same option directives.
2103 BSD getopt_long_only treats both cases the same. */
2104 ASSERT (c == 1003 || c == '?');
2105 ASSERT (optind == 2);
2108 int argc = 0;
2109 const char *argv[10];
2110 int option_index;
2111 int c;
2113 argv[argc++] = "program";
2114 argv[argc++] = "-xtremel";
2115 argv[argc] = NULL;
2116 optind = 1;
2117 opterr = 0;
2118 c = do_getopt_long_only (argc, argv, "abx::", long_options_required,
2119 &option_index);
2120 /* glibc getopt_long_only is intentionally different from
2121 getopt_long when handling a prefix that is common to two
2122 spellings, when both spellings have the same option directives.
2123 BSD getopt_long_only treats both cases the same. */
2124 ASSERT (c == 1003 || c == '?');
2125 ASSERT (optind == 2);
2126 ASSERT (optarg == NULL);
2129 int argc = 0;
2130 const char *argv[10];
2131 int option_index;
2132 int c;
2134 argv[argc++] = "program";
2135 argv[argc++] = "-xtras";
2136 argv[argc] = NULL;
2137 optind = 1;
2138 opterr = 0;
2139 c = do_getopt_long_only (argc, argv, "abx::", long_options_required,
2140 &option_index);
2141 ASSERT (c == 'x');
2142 ASSERT (strcmp (optarg, "tras") == 0);