gen-strtab.awk: Work around IRIX 6.2 nawk bug.
[dxcommon.git] / t / gnugetopt_test.h
blob59e8c0cf9a1491226971ef3fb2b87df2bbd4c2c3
1 /* Test of command line argument processing.
2 Copyright (C) 2009-2024 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 <https://www.gnu.org/licenses/>. */
18 * This file has been modified from its original version,
19 * postprocessed by import-gnu-getopt.awk on 2024-11-17
22 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
24 static int a_seen;
25 static int b_seen;
26 static int q_seen;
28 static const struct option long_options_required[] =
30 { "alpha", no_argument, NULL, 'a' },
31 { "beta", no_argument, &b_seen, 1 },
32 { "prune", required_argument, NULL, 'p' },
33 { "quetsche", required_argument, &q_seen, 1 },
34 { "xtremely-",no_argument, NULL, 1003 },
35 { "xtra", no_argument, NULL, 1001 },
36 { "xtreme", no_argument, NULL, 1002 },
37 { "xtremely", no_argument, NULL, 1003 },
38 { NULL, 0, NULL, 0 }
41 static const struct option long_options_optional[] =
43 { "alpha", no_argument, NULL, 'a' },
44 { "beta", no_argument, &b_seen, 1 },
45 { "prune", optional_argument, NULL, 'p' },
46 { "quetsche", optional_argument, &q_seen, 1 },
47 { NULL, 0, NULL, 0 }
50 static void
51 getopt_long_loop (int argc, const char **argv,
52 const char *options, const struct option *long_options,
53 const char **p_value, const char **q_value,
54 int *non_options_count, const char **non_options,
55 int *unrecognized)
57 int option_index = -1;
58 int c;
60 opterr = 0;
61 q_seen = 0;
62 while ((c = getopt_long (argc, (char **) argv, options, long_options,
63 &option_index))
64 != -1)
66 switch (c)
68 case 0:
69 /* An option with a non-NULL flag pointer was processed. */
70 if (q_seen)
71 *q_value = optarg;
72 break;
73 case 'a':
74 a_seen++;
75 break;
76 case 'b':
77 b_seen = 1;
78 break;
79 case 'p':
80 *p_value = optarg;
81 break;
82 case 'q':
83 *q_value = optarg;
84 break;
85 case '\1':
86 /* Must only happen with option '-' at the beginning. */
87 ASSERT (options[0] == '-');
88 non_options[(*non_options_count)++] = optarg;
89 break;
90 case ':':
91 /* Must only happen with option ':' at the beginning. */
92 ASSERT (options[0] == ':'
93 || ((options[0] == '-' || options[0] == '+')
94 && options[1] == ':'));
95 FALLTHROUGH;
96 case '?':
97 *unrecognized = optopt;
98 break;
99 default:
100 *unrecognized = c;
101 break;
106 /* Reduce casting, so we can use string literals elsewhere.
107 getopt_long takes an array of char*, but luckily does not modify
108 those elements, so we can pass const char*. */
109 static int
110 do_getopt_long (int argc, const char **argv, const char *shortopts,
111 const struct option *longopts, int *longind)
113 optind = 1, opterr = 0;
114 return getopt_long (argc, (char **) argv, shortopts, longopts, longind);
117 static void
118 test_getopt_long (void)
120 const char *argv[20];
121 int c;
122 int start;
124 /* Test disambiguation of options. */
126 int argc = 0;
127 int option_index;
129 argv[argc++] = "program";
130 argv[argc++] = "--x";
131 argv[argc] = NULL;
132 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
133 ASSERT (c == '?');
134 ASSERT (optopt == 0);
137 int argc = 0;
138 int option_index;
140 argv[argc++] = "program";
141 argv[argc++] = "--xt";
142 argv[argc] = NULL;
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 int option_index;
151 argv[argc++] = "program";
152 argv[argc++] = "--xtr";
153 argv[argc] = NULL;
154 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
155 ASSERT (c == '?');
156 ASSERT (optopt == 0);
159 int argc = 0;
160 int option_index;
162 argv[argc++] = "program";
163 argv[argc++] = "--xtra";
164 argv[argc] = NULL;
165 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
166 ASSERT (c == 1001);
169 int argc = 0;
170 int option_index;
172 argv[argc++] = "program";
173 argv[argc++] = "--xtre";
174 argv[argc] = NULL;
175 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
176 ASSERT (c == '?');
177 ASSERT (optopt == 0);
180 int argc = 0;
181 int option_index;
183 argv[argc++] = "program";
184 argv[argc++] = "--xtrem";
185 argv[argc] = NULL;
186 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
187 ASSERT (c == '?');
188 ASSERT (optopt == 0);
191 int argc = 0;
192 int option_index;
194 argv[argc++] = "program";
195 argv[argc++] = "--xtreme";
196 argv[argc] = NULL;
197 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
198 ASSERT (c == 1002);
201 int argc = 0;
202 int option_index;
204 argv[argc++] = "program";
205 argv[argc++] = "--xtremel";
206 argv[argc] = NULL;
207 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
208 ASSERT (c == 1003);
211 int argc = 0;
212 int option_index;
214 argv[argc++] = "program";
215 argv[argc++] = "--xtremely";
216 argv[argc] = NULL;
217 c = do_getopt_long (argc, argv, "ab", long_options_required, &option_index);
218 ASSERT (c == 1003);
221 /* Check that -W handles unknown options. */
223 int argc = 0;
224 int option_index;
226 argv[argc++] = "program";
227 argv[argc++] = "-W";
228 argv[argc] = NULL;
229 c = do_getopt_long (argc, argv, "W;", long_options_required, &option_index);
230 ASSERT (c == '?');
231 ASSERT (optopt == 'W');
234 int argc = 0;
235 int option_index;
237 argv[argc++] = "program";
238 argv[argc++] = "-Wunknown";
239 argv[argc] = NULL;
240 c = do_getopt_long (argc, argv, "W;", long_options_required, &option_index);
241 /* glibc and BSD behave differently here, but for now, we allow
242 both behaviors since W support is not frequently used. */
243 if (c == '?')
245 ASSERT (optopt == 0);
246 ASSERT (optarg == NULL);
248 else
250 ASSERT (c == 'W');
251 ASSERT (strcmp (optarg, "unknown") == 0);
255 int argc = 0;
256 int option_index;
258 argv[argc++] = "program";
259 argv[argc++] = "-W";
260 argv[argc++] = "unknown";
261 argv[argc] = NULL;
262 c = do_getopt_long (argc, argv, "W;", long_options_required, &option_index);
263 /* glibc and BSD behave differently here, but for now, we allow
264 both behaviors since W support is not frequently used. */
265 if (c == '?')
267 ASSERT (optopt == 0);
268 ASSERT (optarg == NULL);
270 else
272 ASSERT (c == 'W');
273 ASSERT (strcmp (optarg, "unknown") == 0);
277 /* Test that 'W' does not dump core:
278 https://sourceware.org/bugzilla/show_bug.cgi?id=12922 */
280 int argc = 0;
281 int option_index;
283 argv[argc++] = "program";
284 argv[argc++] = "-W";
285 argv[argc++] = "dummy";
286 argv[argc] = NULL;
287 c = do_getopt_long (argc, argv, "W;", NULL, &option_index);
288 ASSERT (c == 'W');
289 ASSERT (optind == 2);
292 /* Test processing of boolean short options. */
293 for (start = 0; start <= 1; start++)
295 const char *p_value = NULL;
296 const char *q_value = NULL;
297 int non_options_count = 0;
298 const char *non_options[10];
299 int unrecognized = 0;
300 int argc = 0;
301 a_seen = 0;
302 b_seen = 0;
304 argv[argc++] = "program";
305 argv[argc++] = "-a";
306 argv[argc++] = "foo";
307 argv[argc++] = "bar";
308 argv[argc] = NULL;
309 optind = start;
310 getopt_long_loop (argc, argv, "ab", long_options_required,
311 &p_value, &q_value,
312 &non_options_count, non_options, &unrecognized);
313 ASSERT (a_seen == 1);
314 ASSERT (b_seen == 0);
315 ASSERT (p_value == NULL);
316 ASSERT (q_value == NULL);
317 ASSERT (non_options_count == 0);
318 ASSERT (unrecognized == 0);
319 ASSERT (optind == 2);
321 for (start = 0; start <= 1; start++)
323 const char *p_value = NULL;
324 const char *q_value = NULL;
325 int non_options_count = 0;
326 const char *non_options[10];
327 int unrecognized = 0;
328 int argc = 0;
329 a_seen = 0;
330 b_seen = 0;
332 argv[argc++] = "program";
333 argv[argc++] = "-b";
334 argv[argc++] = "-a";
335 argv[argc++] = "foo";
336 argv[argc++] = "bar";
337 argv[argc] = NULL;
338 optind = start;
339 getopt_long_loop (argc, argv, "ab", long_options_required,
340 &p_value, &q_value,
341 &non_options_count, non_options, &unrecognized);
342 ASSERT (a_seen == 1);
343 ASSERT (b_seen == 1);
344 ASSERT (p_value == NULL);
345 ASSERT (q_value == NULL);
346 ASSERT (non_options_count == 0);
347 ASSERT (unrecognized == 0);
348 ASSERT (optind == 3);
350 for (start = 0; start <= 1; start++)
352 const char *p_value = NULL;
353 const char *q_value = NULL;
354 int non_options_count = 0;
355 const char *non_options[10];
356 int unrecognized = 0;
357 int argc = 0;
358 a_seen = 0;
359 b_seen = 0;
361 argv[argc++] = "program";
362 argv[argc++] = "-ba";
363 argv[argc++] = "foo";
364 argv[argc++] = "bar";
365 argv[argc] = NULL;
366 optind = start;
367 getopt_long_loop (argc, argv, "ab", long_options_required,
368 &p_value, &q_value,
369 &non_options_count, non_options, &unrecognized);
370 ASSERT (a_seen == 1);
371 ASSERT (b_seen == 1);
372 ASSERT (p_value == NULL);
373 ASSERT (q_value == NULL);
374 ASSERT (non_options_count == 0);
375 ASSERT (unrecognized == 0);
376 ASSERT (optind == 2);
378 for (start = 0; start <= 1; start++)
380 const char *p_value = NULL;
381 const char *q_value = NULL;
382 int non_options_count = 0;
383 const char *non_options[10];
384 int unrecognized = 0;
385 int argc = 0;
386 a_seen = 0;
387 b_seen = 0;
389 argv[argc++] = "program";
390 argv[argc++] = "-ab";
391 argv[argc++] = "-a";
392 argv[argc++] = "foo";
393 argv[argc++] = "bar";
394 argv[argc] = NULL;
395 optind = start;
396 getopt_long_loop (argc, argv, "ab", long_options_required,
397 &p_value, &q_value,
398 &non_options_count, non_options, &unrecognized);
399 ASSERT (a_seen == 2);
400 ASSERT (b_seen == 1);
401 ASSERT (p_value == NULL);
402 ASSERT (q_value == NULL);
403 ASSERT (non_options_count == 0);
404 ASSERT (unrecognized == 0);
405 ASSERT (optind == 3);
408 /* Test processing of boolean long options. */
409 for (start = 0; start <= 1; start++)
411 const char *p_value = NULL;
412 const char *q_value = NULL;
413 int non_options_count = 0;
414 const char *non_options[10];
415 int unrecognized = 0;
416 int argc = 0;
417 a_seen = 0;
418 b_seen = 0;
420 argv[argc++] = "program";
421 argv[argc++] = "--alpha";
422 argv[argc++] = "foo";
423 argv[argc++] = "bar";
424 argv[argc] = NULL;
425 optind = start;
426 getopt_long_loop (argc, argv, "ab", long_options_required,
427 &p_value, &q_value,
428 &non_options_count, non_options, &unrecognized);
429 ASSERT (a_seen == 1);
430 ASSERT (b_seen == 0);
431 ASSERT (p_value == NULL);
432 ASSERT (q_value == NULL);
433 ASSERT (non_options_count == 0);
434 ASSERT (unrecognized == 0);
435 ASSERT (optind == 2);
437 for (start = 0; start <= 1; start++)
439 const char *p_value = NULL;
440 const char *q_value = NULL;
441 int non_options_count = 0;
442 const char *non_options[10];
443 int unrecognized = 0;
444 int argc = 0;
445 a_seen = 0;
446 b_seen = 0;
448 argv[argc++] = "program";
449 argv[argc++] = "--beta";
450 argv[argc++] = "--alpha";
451 argv[argc++] = "foo";
452 argv[argc++] = "bar";
453 argv[argc] = NULL;
454 optind = start;
455 getopt_long_loop (argc, argv, "ab", long_options_required,
456 &p_value, &q_value,
457 &non_options_count, non_options, &unrecognized);
458 ASSERT (a_seen == 1);
459 ASSERT (b_seen == 1);
460 ASSERT (p_value == NULL);
461 ASSERT (q_value == NULL);
462 ASSERT (non_options_count == 0);
463 ASSERT (unrecognized == 0);
464 ASSERT (optind == 3);
466 for (start = 0; start <= 1; start++)
468 const char *p_value = NULL;
469 const char *q_value = NULL;
470 int non_options_count = 0;
471 const char *non_options[10];
472 int unrecognized = 0;
473 int argc = 0;
474 a_seen = 0;
475 b_seen = 0;
477 argv[argc++] = "program";
478 argv[argc++] = "--alpha";
479 argv[argc++] = "--beta";
480 argv[argc++] = "--alpha";
481 argv[argc++] = "--beta";
482 argv[argc++] = "foo";
483 argv[argc++] = "bar";
484 argv[argc] = NULL;
485 optind = start;
486 getopt_long_loop (argc, argv, "ab", long_options_required,
487 &p_value, &q_value,
488 &non_options_count, non_options, &unrecognized);
489 ASSERT (a_seen == 2);
490 ASSERT (b_seen == 1);
491 ASSERT (p_value == NULL);
492 ASSERT (q_value == NULL);
493 ASSERT (non_options_count == 0);
494 ASSERT (unrecognized == 0);
495 ASSERT (optind == 5);
498 /* Test processing of boolean long options via -W. */
499 for (start = 0; start <= 1; start++)
501 const char *p_value = NULL;
502 const char *q_value = NULL;
503 int non_options_count = 0;
504 const char *non_options[10];
505 int unrecognized = 0;
506 int argc = 0;
507 a_seen = 0;
508 b_seen = 0;
510 argv[argc++] = "program";
511 argv[argc++] = "-Walpha";
512 argv[argc++] = "foo";
513 argv[argc++] = "bar";
514 argv[argc] = NULL;
515 optind = start;
516 getopt_long_loop (argc, argv, "abW;", long_options_required,
517 &p_value, &q_value,
518 &non_options_count, non_options, &unrecognized);
519 ASSERT (a_seen == 1);
520 ASSERT (b_seen == 0);
521 ASSERT (p_value == NULL);
522 ASSERT (q_value == NULL);
523 ASSERT (non_options_count == 0);
524 ASSERT (unrecognized == 0);
525 ASSERT (optind == 2);
527 for (start = 0; start <= 1; start++)
529 const char *p_value = NULL;
530 const char *q_value = NULL;
531 int non_options_count = 0;
532 const char *non_options[10];
533 int unrecognized = 0;
534 int argc = 0;
535 a_seen = 0;
536 b_seen = 0;
538 argv[argc++] = "program";
539 argv[argc++] = "-W";
540 argv[argc++] = "beta";
541 argv[argc++] = "-W";
542 argv[argc++] = "alpha";
543 argv[argc++] = "foo";
544 argv[argc++] = "bar";
545 argv[argc] = NULL;
546 optind = start;
547 getopt_long_loop (argc, argv, "aW;b", long_options_required,
548 &p_value, &q_value,
549 &non_options_count, non_options, &unrecognized);
550 ASSERT (a_seen == 1);
551 ASSERT (b_seen == 1);
552 ASSERT (p_value == NULL);
553 ASSERT (q_value == NULL);
554 ASSERT (non_options_count == 0);
555 ASSERT (unrecognized == 0);
556 ASSERT (optind == 5);
558 for (start = 0; start <= 1; start++)
560 const char *p_value = NULL;
561 const char *q_value = NULL;
562 int non_options_count = 0;
563 const char *non_options[10];
564 int unrecognized = 0;
565 int argc = 0;
566 a_seen = 0;
567 b_seen = 0;
569 argv[argc++] = "program";
570 argv[argc++] = "-Walpha";
571 argv[argc++] = "-Wbeta";
572 argv[argc++] = "-Walpha";
573 argv[argc++] = "-Wbeta";
574 argv[argc++] = "foo";
575 argv[argc++] = "bar";
576 argv[argc] = NULL;
577 optind = start;
578 getopt_long_loop (argc, argv, "W;ab", long_options_required,
579 &p_value, &q_value,
580 &non_options_count, non_options, &unrecognized);
581 ASSERT (a_seen == 2);
582 ASSERT (b_seen == 1);
583 ASSERT (p_value == NULL);
584 ASSERT (q_value == NULL);
585 ASSERT (non_options_count == 0);
586 ASSERT (unrecognized == 0);
587 ASSERT (optind == 5);
590 /* Test processing of short options with arguments. */
591 for (start = 0; start <= 1; start++)
593 const char *p_value = NULL;
594 const char *q_value = NULL;
595 int non_options_count = 0;
596 const char *non_options[10];
597 int unrecognized = 0;
598 int argc = 0;
599 a_seen = 0;
600 b_seen = 0;
602 argv[argc++] = "program";
603 argv[argc++] = "-pfoo";
604 argv[argc++] = "bar";
605 argv[argc] = NULL;
606 optind = start;
607 getopt_long_loop (argc, argv, "p:q:", long_options_required,
608 &p_value, &q_value,
609 &non_options_count, non_options, &unrecognized);
610 ASSERT (a_seen == 0);
611 ASSERT (b_seen == 0);
612 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
613 ASSERT (q_value == NULL);
614 ASSERT (non_options_count == 0);
615 ASSERT (unrecognized == 0);
616 ASSERT (optind == 2);
618 for (start = 0; start <= 1; start++)
620 const char *p_value = NULL;
621 const char *q_value = NULL;
622 int non_options_count = 0;
623 const char *non_options[10];
624 int unrecognized = 0;
625 int argc = 0;
626 a_seen = 0;
627 b_seen = 0;
629 argv[argc++] = "program";
630 argv[argc++] = "-p";
631 argv[argc++] = "foo";
632 argv[argc++] = "bar";
633 argv[argc] = NULL;
634 optind = start;
635 getopt_long_loop (argc, argv, "p:q:", long_options_required,
636 &p_value, &q_value,
637 &non_options_count, non_options, &unrecognized);
638 ASSERT (a_seen == 0);
639 ASSERT (b_seen == 0);
640 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
641 ASSERT (q_value == NULL);
642 ASSERT (non_options_count == 0);
643 ASSERT (unrecognized == 0);
644 ASSERT (optind == 3);
646 for (start = 0; start <= 1; start++)
648 const char *p_value = NULL;
649 const char *q_value = NULL;
650 int non_options_count = 0;
651 const char *non_options[10];
652 int unrecognized = 0;
653 int argc = 0;
654 a_seen = 0;
655 b_seen = 0;
657 argv[argc++] = "program";
658 argv[argc++] = "-ab";
659 argv[argc++] = "-q";
660 argv[argc++] = "baz";
661 argv[argc++] = "-pfoo";
662 argv[argc++] = "bar";
663 argv[argc] = NULL;
664 optind = start;
665 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
666 &p_value, &q_value,
667 &non_options_count, non_options, &unrecognized);
668 ASSERT (a_seen == 1);
669 ASSERT (b_seen == 1);
670 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
671 ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
672 ASSERT (non_options_count == 0);
673 ASSERT (unrecognized == 0);
674 ASSERT (optind == 5);
677 /* Test processing of long options with arguments. */
678 for (start = 0; start <= 1; start++)
680 const char *p_value = NULL;
681 const char *q_value = NULL;
682 int non_options_count = 0;
683 const char *non_options[10];
684 int unrecognized = 0;
685 int argc = 0;
686 a_seen = 0;
687 b_seen = 0;
689 argv[argc++] = "program";
690 argv[argc++] = "--p=foo";
691 argv[argc++] = "bar";
692 argv[argc] = NULL;
693 optind = start;
694 getopt_long_loop (argc, argv, "p:q:", long_options_required,
695 &p_value, &q_value,
696 &non_options_count, non_options, &unrecognized);
697 ASSERT (a_seen == 0);
698 ASSERT (b_seen == 0);
699 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
700 ASSERT (q_value == NULL);
701 ASSERT (non_options_count == 0);
702 ASSERT (unrecognized == 0);
703 ASSERT (optind == 2);
705 for (start = 0; start <= 1; start++)
707 const char *p_value = NULL;
708 const char *q_value = NULL;
709 int non_options_count = 0;
710 const char *non_options[10];
711 int unrecognized = 0;
712 int argc = 0;
713 a_seen = 0;
714 b_seen = 0;
716 argv[argc++] = "program";
717 argv[argc++] = "--p";
718 argv[argc++] = "foo";
719 argv[argc++] = "bar";
720 argv[argc] = NULL;
721 optind = start;
722 getopt_long_loop (argc, argv, "p:q:", long_options_required,
723 &p_value, &q_value,
724 &non_options_count, non_options, &unrecognized);
725 ASSERT (a_seen == 0);
726 ASSERT (b_seen == 0);
727 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
728 ASSERT (q_value == NULL);
729 ASSERT (non_options_count == 0);
730 ASSERT (unrecognized == 0);
731 ASSERT (optind == 3);
733 for (start = 0; start <= 1; start++)
735 const char *p_value = NULL;
736 const char *q_value = NULL;
737 int non_options_count = 0;
738 const char *non_options[10];
739 int unrecognized = 0;
740 int argc = 0;
741 a_seen = 0;
742 b_seen = 0;
744 argv[argc++] = "program";
745 argv[argc++] = "-ab";
746 argv[argc++] = "--q";
747 argv[argc++] = "baz";
748 argv[argc++] = "--p=foo";
749 argv[argc++] = "bar";
750 argv[argc] = NULL;
751 optind = start;
752 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
753 &p_value, &q_value,
754 &non_options_count, non_options, &unrecognized);
755 ASSERT (a_seen == 1);
756 ASSERT (b_seen == 1);
757 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
758 ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
759 ASSERT (non_options_count == 0);
760 ASSERT (unrecognized == 0);
761 ASSERT (optind == 5);
764 /* Test processing of long options with arguments via -W. */
765 for (start = 0; start <= 1; start++)
767 const char *p_value = NULL;
768 const char *q_value = NULL;
769 int non_options_count = 0;
770 const char *non_options[10];
771 int unrecognized = 0;
772 int argc = 0;
773 a_seen = 0;
774 b_seen = 0;
776 argv[argc++] = "program";
777 argv[argc++] = "-Wp=foo";
778 argv[argc++] = "bar";
779 argv[argc] = NULL;
780 optind = start;
781 getopt_long_loop (argc, argv, "p:q:W;", 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 == 2);
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 a_seen = 0;
801 b_seen = 0;
803 argv[argc++] = "program";
804 argv[argc++] = "-W";
805 argv[argc++] = "p";
806 argv[argc++] = "foo";
807 argv[argc++] = "bar";
808 argv[argc] = NULL;
809 optind = start;
810 getopt_long_loop (argc, argv, "p:W;q:", long_options_required,
811 &p_value, &q_value,
812 &non_options_count, non_options, &unrecognized);
813 ASSERT (a_seen == 0);
814 ASSERT (b_seen == 0);
815 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
816 ASSERT (q_value == NULL);
817 ASSERT (non_options_count == 0);
818 ASSERT (unrecognized == 0);
819 ASSERT (optind == 4);
821 for (start = 0; start <= 1; start++)
823 const char *p_value = NULL;
824 const char *q_value = NULL;
825 int non_options_count = 0;
826 const char *non_options[10];
827 int unrecognized = 0;
828 int argc = 0;
829 a_seen = 0;
830 b_seen = 0;
832 argv[argc++] = "program";
833 argv[argc++] = "-ab";
834 argv[argc++] = "-Wq";
835 argv[argc++] = "baz";
836 argv[argc++] = "-W";
837 argv[argc++] = "p=foo";
838 argv[argc++] = "bar";
839 argv[argc] = NULL;
840 optind = start;
841 getopt_long_loop (argc, argv, "W;abp:q:", long_options_required,
842 &p_value, &q_value,
843 &non_options_count, non_options, &unrecognized);
844 ASSERT (a_seen == 1);
845 ASSERT (b_seen == 1);
846 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
847 ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
848 ASSERT (non_options_count == 0);
849 ASSERT (unrecognized == 0);
850 ASSERT (optind == 6);
853 /* Test processing of short options with optional arguments. */
854 for (start = 0; start <= 1; start++)
856 const char *p_value = NULL;
857 const char *q_value = NULL;
858 int non_options_count = 0;
859 const char *non_options[10];
860 int unrecognized = 0;
861 int argc = 0;
862 a_seen = 0;
863 b_seen = 0;
865 argv[argc++] = "program";
866 argv[argc++] = "-pfoo";
867 argv[argc++] = "bar";
868 argv[argc] = NULL;
869 optind = start;
870 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
871 &p_value, &q_value,
872 &non_options_count, non_options, &unrecognized);
873 ASSERT (a_seen == 0);
874 ASSERT (b_seen == 0);
875 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
876 ASSERT (q_value == NULL);
877 ASSERT (non_options_count == 0);
878 ASSERT (unrecognized == 0);
879 ASSERT (optind == 2);
881 for (start = 0; start <= 1; start++)
883 const char *p_value = NULL;
884 const char *q_value = NULL;
885 int non_options_count = 0;
886 const char *non_options[10];
887 int unrecognized = 0;
888 int argc = 0;
889 a_seen = 0;
890 b_seen = 0;
892 argv[argc++] = "program";
893 argv[argc++] = "-p";
894 argv[argc++] = "foo";
895 argv[argc++] = "bar";
896 argv[argc] = NULL;
897 optind = start;
898 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
899 &p_value, &q_value,
900 &non_options_count, non_options, &unrecognized);
901 ASSERT (a_seen == 0);
902 ASSERT (b_seen == 0);
903 ASSERT (p_value == NULL);
904 ASSERT (q_value == NULL);
905 ASSERT (non_options_count == 0);
906 ASSERT (unrecognized == 0);
907 ASSERT (optind == 2);
909 for (start = 0; start <= 1; start++)
911 const char *p_value = NULL;
912 const char *q_value = NULL;
913 int non_options_count = 0;
914 const char *non_options[10];
915 int unrecognized = 0;
916 int argc = 0;
917 a_seen = 0;
918 b_seen = 0;
920 argv[argc++] = "program";
921 argv[argc++] = "-p";
922 argv[argc++] = "-a";
923 argv[argc++] = "bar";
924 argv[argc] = NULL;
925 optind = start;
926 getopt_long_loop (argc, argv, "abp::q::", long_options_optional,
927 &p_value, &q_value,
928 &non_options_count, non_options, &unrecognized);
929 ASSERT (a_seen == 1);
930 ASSERT (b_seen == 0);
931 ASSERT (p_value == NULL);
932 ASSERT (q_value == NULL);
933 ASSERT (non_options_count == 0);
934 ASSERT (unrecognized == 0);
935 ASSERT (optind == 3);
938 /* Test processing of long options with optional arguments. */
939 for (start = 0; start <= 1; start++)
941 const char *p_value = NULL;
942 const char *q_value = NULL;
943 int non_options_count = 0;
944 const char *non_options[10];
945 int unrecognized = 0;
946 int argc = 0;
947 a_seen = 0;
948 b_seen = 0;
950 argv[argc++] = "program";
951 argv[argc++] = "--p=foo";
952 argv[argc++] = "bar";
953 argv[argc] = NULL;
954 optind = start;
955 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
956 &p_value, &q_value,
957 &non_options_count, non_options, &unrecognized);
958 ASSERT (a_seen == 0);
959 ASSERT (b_seen == 0);
960 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
961 ASSERT (q_value == NULL);
962 ASSERT (non_options_count == 0);
963 ASSERT (unrecognized == 0);
964 ASSERT (optind == 2);
966 for (start = 0; start <= 1; start++)
968 const char *p_value = NULL;
969 const char *q_value = NULL;
970 int non_options_count = 0;
971 const char *non_options[10];
972 int unrecognized = 0;
973 int argc = 0;
974 a_seen = 0;
975 b_seen = 0;
977 argv[argc++] = "program";
978 argv[argc++] = "--p";
979 argv[argc++] = "foo";
980 argv[argc++] = "bar";
981 argv[argc] = NULL;
982 optind = start;
983 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
984 &p_value, &q_value,
985 &non_options_count, non_options, &unrecognized);
986 ASSERT (a_seen == 0);
987 ASSERT (b_seen == 0);
988 ASSERT (p_value == NULL);
989 ASSERT (q_value == NULL);
990 ASSERT (non_options_count == 0);
991 ASSERT (unrecognized == 0);
992 ASSERT (optind == 2);
994 for (start = 0; start <= 1; start++)
996 const char *p_value = NULL;
997 const char *q_value = NULL;
998 int non_options_count = 0;
999 const char *non_options[10];
1000 int unrecognized = 0;
1001 int argc = 0;
1002 a_seen = 0;
1003 b_seen = 0;
1005 argv[argc++] = "program";
1006 argv[argc++] = "--p=";
1007 argv[argc++] = "foo";
1008 argv[argc++] = "bar";
1009 argv[argc] = NULL;
1010 optind = start;
1011 getopt_long_loop (argc, argv, "p::q::", long_options_optional,
1012 &p_value, &q_value,
1013 &non_options_count, non_options, &unrecognized);
1014 ASSERT (a_seen == 0);
1015 ASSERT (b_seen == 0);
1016 ASSERT (p_value != NULL && *p_value == '\0');
1017 ASSERT (q_value == NULL);
1018 ASSERT (non_options_count == 0);
1019 ASSERT (unrecognized == 0);
1020 ASSERT (optind == 2);
1022 for (start = 0; start <= 1; start++)
1024 const char *p_value = NULL;
1025 const char *q_value = NULL;
1026 int non_options_count = 0;
1027 const char *non_options[10];
1028 int unrecognized = 0;
1029 int argc = 0;
1030 a_seen = 0;
1031 b_seen = 0;
1033 argv[argc++] = "program";
1034 argv[argc++] = "--p";
1035 argv[argc++] = "-a";
1036 argv[argc++] = "bar";
1037 argv[argc] = NULL;
1038 optind = start;
1039 getopt_long_loop (argc, argv, "abp::q::", long_options_optional,
1040 &p_value, &q_value,
1041 &non_options_count, non_options, &unrecognized);
1042 ASSERT (a_seen == 1);
1043 ASSERT (b_seen == 0);
1044 ASSERT (p_value == NULL);
1045 ASSERT (q_value == NULL);
1046 ASSERT (non_options_count == 0);
1047 ASSERT (unrecognized == 0);
1048 ASSERT (optind == 3);
1051 /* Test processing of long options with optional arguments via -W. */
1052 for (start = 0; start <= 1; start++)
1054 const char *p_value = NULL;
1055 const char *q_value = NULL;
1056 int non_options_count = 0;
1057 const char *non_options[10];
1058 int unrecognized = 0;
1059 int argc = 0;
1060 a_seen = 0;
1061 b_seen = 0;
1063 argv[argc++] = "program";
1064 argv[argc++] = "-Wp=foo";
1065 argv[argc++] = "bar";
1066 argv[argc] = NULL;
1067 optind = start;
1068 getopt_long_loop (argc, argv, "p::q::W;", long_options_optional,
1069 &p_value, &q_value,
1070 &non_options_count, non_options, &unrecognized);
1071 ASSERT (a_seen == 0);
1072 ASSERT (b_seen == 0);
1073 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1074 ASSERT (q_value == NULL);
1075 ASSERT (non_options_count == 0);
1076 ASSERT (unrecognized == 0);
1077 ASSERT (optind == 2);
1079 for (start = 0; start <= 1; start++)
1081 const char *p_value = NULL;
1082 const char *q_value = NULL;
1083 int non_options_count = 0;
1084 const char *non_options[10];
1085 int unrecognized = 0;
1086 int argc = 0;
1087 a_seen = 0;
1088 b_seen = 0;
1090 argv[argc++] = "program";
1091 argv[argc++] = "-Wp";
1092 argv[argc++] = "foo";
1093 argv[argc++] = "bar";
1094 argv[argc] = NULL;
1095 optind = start;
1096 getopt_long_loop (argc, argv, "p::q::W;", long_options_optional,
1097 &p_value, &q_value,
1098 &non_options_count, non_options, &unrecognized);
1099 ASSERT (a_seen == 0);
1100 ASSERT (b_seen == 0);
1101 ASSERT (p_value == NULL);
1102 ASSERT (q_value == NULL);
1103 ASSERT (non_options_count == 0);
1104 ASSERT (unrecognized == 0);
1105 ASSERT (optind == 2);
1107 for (start = 0; start <= 1; start++)
1109 const char *p_value = NULL;
1110 const char *q_value = NULL;
1111 int non_options_count = 0;
1112 const char *non_options[10];
1113 int unrecognized = 0;
1114 int argc = 0;
1115 a_seen = 0;
1116 b_seen = 0;
1118 argv[argc++] = "program";
1119 argv[argc++] = "-Wp=";
1120 argv[argc++] = "foo";
1121 argv[argc++] = "bar";
1122 argv[argc] = NULL;
1123 optind = start;
1124 getopt_long_loop (argc, argv, "W;p::q::", long_options_optional,
1125 &p_value, &q_value,
1126 &non_options_count, non_options, &unrecognized);
1127 ASSERT (a_seen == 0);
1128 ASSERT (b_seen == 0);
1129 ASSERT (p_value != NULL && *p_value == '\0');
1130 ASSERT (q_value == NULL);
1131 ASSERT (non_options_count == 0);
1132 ASSERT (unrecognized == 0);
1133 ASSERT (optind == 2);
1135 for (start = 0; start <= 1; start++)
1137 const char *p_value = NULL;
1138 const char *q_value = NULL;
1139 int non_options_count = 0;
1140 const char *non_options[10];
1141 int unrecognized = 0;
1142 int argc = 0;
1143 a_seen = 0;
1144 b_seen = 0;
1146 argv[argc++] = "program";
1147 argv[argc++] = "-W";
1148 argv[argc++] = "p=";
1149 argv[argc++] = "foo";
1150 argv[argc++] = "bar";
1151 argv[argc] = NULL;
1152 optind = start;
1153 getopt_long_loop (argc, argv, "W;p::q::", long_options_optional,
1154 &p_value, &q_value,
1155 &non_options_count, non_options, &unrecognized);
1156 ASSERT (a_seen == 0);
1157 ASSERT (b_seen == 0);
1158 ASSERT (p_value != NULL && *p_value == '\0');
1159 ASSERT (q_value == NULL);
1160 ASSERT (non_options_count == 0);
1161 ASSERT (unrecognized == 0);
1162 ASSERT (optind == 3);
1164 for (start = 0; start <= 1; start++)
1166 const char *p_value = NULL;
1167 const char *q_value = NULL;
1168 int non_options_count = 0;
1169 const char *non_options[10];
1170 int unrecognized = 0;
1171 int argc = 0;
1172 a_seen = 0;
1173 b_seen = 0;
1175 argv[argc++] = "program";
1176 argv[argc++] = "-W";
1177 argv[argc++] = "p";
1178 argv[argc++] = "-a";
1179 argv[argc++] = "bar";
1180 argv[argc] = NULL;
1181 optind = start;
1182 getopt_long_loop (argc, argv, "W;abp::q::", long_options_optional,
1183 &p_value, &q_value,
1184 &non_options_count, non_options, &unrecognized);
1185 ASSERT (a_seen == 1);
1186 ASSERT (b_seen == 0);
1187 /* ASSERT (p_value == NULL); */
1188 ASSERT (q_value == NULL);
1189 ASSERT (non_options_count == 0);
1190 ASSERT (unrecognized == 0);
1191 ASSERT (optind == 4);
1194 /* Check that invalid options are recognized. */
1195 for (start = 0; start <= 1; start++)
1197 const char *p_value = NULL;
1198 const char *q_value = NULL;
1199 int non_options_count = 0;
1200 const char *non_options[10];
1201 int unrecognized = 0;
1202 int argc = 0;
1203 a_seen = 0;
1204 b_seen = 0;
1206 argv[argc++] = "program";
1207 argv[argc++] = "-p";
1208 argv[argc++] = "foo";
1209 argv[argc++] = "-x";
1210 argv[argc++] = "-a";
1211 argv[argc++] = "bar";
1212 argv[argc] = NULL;
1213 optind = start;
1214 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1215 &p_value, &q_value,
1216 &non_options_count, non_options, &unrecognized);
1217 ASSERT (a_seen == 1);
1218 ASSERT (b_seen == 0);
1219 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1220 ASSERT (q_value == NULL);
1221 ASSERT (non_options_count == 0);
1222 ASSERT (unrecognized == 'x');
1223 ASSERT (optind == 5);
1225 for (start = 0; start <= 1; start++)
1227 const char *p_value = NULL;
1228 const char *q_value = NULL;
1229 int non_options_count = 0;
1230 const char *non_options[10];
1231 int unrecognized = 0;
1232 int argc = 0;
1233 a_seen = 0;
1234 b_seen = 0;
1236 argv[argc++] = "program";
1237 argv[argc++] = "-p";
1238 argv[argc++] = "foo";
1239 argv[argc++] = "-:";
1240 argv[argc++] = "-a";
1241 argv[argc++] = "bar";
1242 argv[argc] = NULL;
1243 optind = start;
1244 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1245 &p_value, &q_value,
1246 &non_options_count, non_options, &unrecognized);
1247 ASSERT (a_seen == 1);
1248 ASSERT (b_seen == 0);
1249 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1250 ASSERT (q_value == NULL);
1251 ASSERT (non_options_count == 0);
1252 ASSERT (unrecognized == ':');
1253 ASSERT (optind == 5);
1256 /* Check that unexpected arguments are recognized. */
1257 for (start = 0; start <= 1; start++)
1259 const char *p_value = NULL;
1260 const char *q_value = NULL;
1261 int non_options_count = 0;
1262 const char *non_options[10];
1263 int unrecognized = 0;
1264 int argc = 0;
1265 a_seen = 0;
1266 b_seen = 0;
1268 argv[argc++] = "program";
1269 argv[argc++] = "-p";
1270 argv[argc++] = "foo";
1271 argv[argc++] = "--a=";
1272 argv[argc++] = "bar";
1273 argv[argc] = NULL;
1274 optind = start;
1275 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1276 &p_value, &q_value,
1277 &non_options_count, non_options, &unrecognized);
1278 ASSERT (a_seen == 0);
1279 ASSERT (b_seen == 0);
1280 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1281 ASSERT (q_value == NULL);
1282 ASSERT (non_options_count == 0);
1283 ASSERT (unrecognized == 'a');
1284 ASSERT (optind == 4);
1286 for (start = 0; start <= 1; start++)
1288 const char *p_value = NULL;
1289 const char *q_value = NULL;
1290 int non_options_count = 0;
1291 const char *non_options[10];
1292 int unrecognized = 0;
1293 int argc = 0;
1294 a_seen = 0;
1295 b_seen = 0;
1297 argv[argc++] = "program";
1298 argv[argc++] = "-p";
1299 argv[argc++] = "foo";
1300 argv[argc++] = "--b=";
1301 argv[argc++] = "bar";
1302 argv[argc] = NULL;
1303 optind = start;
1304 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1305 &p_value, &q_value,
1306 &non_options_count, non_options, &unrecognized);
1307 ASSERT (a_seen == 0);
1308 ASSERT (b_seen == 0);
1309 ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
1310 ASSERT (q_value == NULL);
1311 ASSERT (non_options_count == 0);
1312 /* When flag is non-zero, glibc sets optopt anyway, but BSD
1313 leaves optopt unchanged. */
1314 ASSERT (unrecognized == 1 || unrecognized == 0);
1315 ASSERT (optind == 4);
1318 /* Check that by default, non-options arguments are moved to the end. */
1319 for (start = 0; start <= 1; start++)
1321 const char *p_value = NULL;
1322 const char *q_value = NULL;
1323 int non_options_count = 0;
1324 const char *non_options[10];
1325 int unrecognized = 0;
1326 int argc = 0;
1327 a_seen = 0;
1328 b_seen = 0;
1330 argv[argc++] = "program";
1331 argv[argc++] = "donald";
1332 argv[argc++] = "-p";
1333 argv[argc++] = "billy";
1334 argv[argc++] = "duck";
1335 argv[argc++] = "-a";
1336 argv[argc++] = "bar";
1337 argv[argc] = NULL;
1338 optind = start;
1339 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1340 &p_value, &q_value,
1341 &non_options_count, non_options, &unrecognized);
1342 ASSERT (strcmp (argv[0], "program") == 0);
1343 ASSERT (strcmp (argv[1], "-p") == 0);
1344 ASSERT (strcmp (argv[2], "billy") == 0);
1345 ASSERT (strcmp (argv[3], "-a") == 0);
1346 ASSERT (strcmp (argv[4], "donald") == 0);
1347 ASSERT (strcmp (argv[5], "duck") == 0);
1348 ASSERT (strcmp (argv[6], "bar") == 0);
1349 ASSERT (argv[7] == NULL);
1350 ASSERT (a_seen == 1);
1351 ASSERT (b_seen == 0);
1352 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1353 ASSERT (q_value == NULL);
1354 ASSERT (non_options_count == 0);
1355 ASSERT (unrecognized == 0);
1356 ASSERT (optind == 4);
1359 /* Check that '--' ends the argument processing. */
1360 for (start = 0; start <= 1; start++)
1362 const char *p_value = NULL;
1363 const char *q_value = NULL;
1364 int non_options_count = 0;
1365 const char *non_options[10];
1366 int unrecognized = 0;
1367 int argc = 0;
1368 a_seen = 0;
1369 b_seen = 0;
1371 argv[argc++] = "program";
1372 argv[argc++] = "donald";
1373 argv[argc++] = "-p";
1374 argv[argc++] = "billy";
1375 argv[argc++] = "duck";
1376 argv[argc++] = "-a";
1377 argv[argc++] = "--";
1378 argv[argc++] = "-b";
1379 argv[argc++] = "foo";
1380 argv[argc++] = "-q";
1381 argv[argc++] = "johnny";
1382 argv[argc++] = "bar";
1383 argv[argc] = NULL;
1384 optind = start;
1385 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1386 &p_value, &q_value,
1387 &non_options_count, non_options, &unrecognized);
1388 ASSERT (strcmp (argv[0], "program") == 0);
1389 ASSERT (strcmp (argv[1], "-p") == 0);
1390 ASSERT (strcmp (argv[2], "billy") == 0);
1391 ASSERT (strcmp (argv[3], "-a") == 0);
1392 ASSERT (strcmp (argv[4], "--") == 0);
1393 ASSERT (strcmp (argv[5], "donald") == 0);
1394 ASSERT (strcmp (argv[6], "duck") == 0);
1395 ASSERT (strcmp (argv[7], "-b") == 0);
1396 ASSERT (strcmp (argv[8], "foo") == 0);
1397 ASSERT (strcmp (argv[9], "-q") == 0);
1398 ASSERT (strcmp (argv[10], "johnny") == 0);
1399 ASSERT (strcmp (argv[11], "bar") == 0);
1400 ASSERT (argv[12] == NULL);
1401 ASSERT (a_seen == 1);
1402 ASSERT (b_seen == 0);
1403 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1404 ASSERT (q_value == NULL);
1405 ASSERT (non_options_count == 0);
1406 ASSERT (unrecognized == 0);
1407 ASSERT (optind == 5);
1410 /* Check that the '-' flag causes non-options to be returned in order. */
1411 for (start = 0; start <= 1; start++)
1413 const char *p_value = NULL;
1414 const char *q_value = NULL;
1415 int non_options_count = 0;
1416 const char *non_options[10];
1417 int unrecognized = 0;
1418 int argc = 0;
1419 a_seen = 0;
1420 b_seen = 0;
1422 argv[argc++] = "program";
1423 argv[argc++] = "donald";
1424 argv[argc++] = "-p";
1425 argv[argc++] = "billy";
1426 argv[argc++] = "duck";
1427 argv[argc++] = "-a";
1428 argv[argc++] = "bar";
1429 argv[argc] = NULL;
1430 optind = start;
1431 getopt_long_loop (argc, argv, "-abp:q:", long_options_required,
1432 &p_value, &q_value,
1433 &non_options_count, non_options, &unrecognized);
1434 ASSERT (strcmp (argv[0], "program") == 0);
1435 ASSERT (strcmp (argv[1], "donald") == 0);
1436 ASSERT (strcmp (argv[2], "-p") == 0);
1437 ASSERT (strcmp (argv[3], "billy") == 0);
1438 ASSERT (strcmp (argv[4], "duck") == 0);
1439 ASSERT (strcmp (argv[5], "-a") == 0);
1440 ASSERT (strcmp (argv[6], "bar") == 0);
1441 ASSERT (argv[7] == NULL);
1442 ASSERT (a_seen == 1);
1443 ASSERT (b_seen == 0);
1444 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1445 ASSERT (q_value == NULL);
1446 ASSERT (non_options_count == 3);
1447 ASSERT (strcmp (non_options[0], "donald") == 0);
1448 ASSERT (strcmp (non_options[1], "duck") == 0);
1449 ASSERT (strcmp (non_options[2], "bar") == 0);
1450 ASSERT (unrecognized == 0);
1451 ASSERT (optind == 7);
1454 /* Check that '--' ends the argument processing. */
1455 for (start = 0; start <= 1; start++)
1457 const char *p_value = NULL;
1458 const char *q_value = NULL;
1459 int non_options_count = 0;
1460 const char *non_options[10];
1461 int unrecognized = 0;
1462 int argc = 0;
1463 a_seen = 0;
1464 b_seen = 0;
1466 argv[argc++] = "program";
1467 argv[argc++] = "donald";
1468 argv[argc++] = "-p";
1469 argv[argc++] = "billy";
1470 argv[argc++] = "duck";
1471 argv[argc++] = "-a";
1472 argv[argc++] = "--";
1473 argv[argc++] = "-b";
1474 argv[argc++] = "foo";
1475 argv[argc++] = "-q";
1476 argv[argc++] = "johnny";
1477 argv[argc++] = "bar";
1478 argv[argc] = NULL;
1479 optind = start;
1480 getopt_long_loop (argc, argv, "-abp:q:", long_options_required,
1481 &p_value, &q_value,
1482 &non_options_count, non_options, &unrecognized);
1483 ASSERT (strcmp (argv[0], "program") == 0);
1484 ASSERT (strcmp (argv[1], "donald") == 0);
1485 ASSERT (strcmp (argv[2], "-p") == 0);
1486 ASSERT (strcmp (argv[3], "billy") == 0);
1487 ASSERT (strcmp (argv[4], "duck") == 0);
1488 ASSERT (strcmp (argv[5], "-a") == 0);
1489 ASSERT (strcmp (argv[6], "--") == 0);
1490 ASSERT (strcmp (argv[7], "-b") == 0);
1491 ASSERT (strcmp (argv[8], "foo") == 0);
1492 ASSERT (strcmp (argv[9], "-q") == 0);
1493 ASSERT (strcmp (argv[10], "johnny") == 0);
1494 ASSERT (strcmp (argv[11], "bar") == 0);
1495 ASSERT (argv[12] == NULL);
1496 ASSERT (a_seen == 1);
1497 ASSERT (b_seen == 0);
1498 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1499 ASSERT (q_value == NULL);
1500 if (non_options_count == 2)
1502 /* glibc behaviour. */
1503 ASSERT (non_options_count == 2);
1504 ASSERT (strcmp (non_options[0], "donald") == 0);
1505 ASSERT (strcmp (non_options[1], "duck") == 0);
1506 ASSERT (unrecognized == 0);
1507 ASSERT (optind == 7);
1509 else
1511 /* Another valid behaviour. */
1512 ASSERT (non_options_count == 7);
1513 ASSERT (strcmp (non_options[0], "donald") == 0);
1514 ASSERT (strcmp (non_options[1], "duck") == 0);
1515 ASSERT (strcmp (non_options[2], "-b") == 0);
1516 ASSERT (strcmp (non_options[3], "foo") == 0);
1517 ASSERT (strcmp (non_options[4], "-q") == 0);
1518 ASSERT (strcmp (non_options[5], "johnny") == 0);
1519 ASSERT (strcmp (non_options[6], "bar") == 0);
1520 ASSERT (unrecognized == 0);
1521 ASSERT (optind == 12);
1525 /* Check that the '-' flag has to come first. */
1526 for (start = 0; start <= 1; start++)
1528 const char *p_value = NULL;
1529 const char *q_value = NULL;
1530 int non_options_count = 0;
1531 const char *non_options[10];
1532 int unrecognized = 0;
1533 int argc = 0;
1534 a_seen = 0;
1535 b_seen = 0;
1537 argv[argc++] = "program";
1538 argv[argc++] = "donald";
1539 argv[argc++] = "-p";
1540 argv[argc++] = "billy";
1541 argv[argc++] = "duck";
1542 argv[argc++] = "-a";
1543 argv[argc++] = "bar";
1544 argv[argc] = NULL;
1545 optind = start;
1546 getopt_long_loop (argc, argv, "abp:q:-", long_options_required,
1547 &p_value, &q_value,
1548 &non_options_count, non_options, &unrecognized);
1549 ASSERT (strcmp (argv[0], "program") == 0);
1550 ASSERT (strcmp (argv[1], "-p") == 0);
1551 ASSERT (strcmp (argv[2], "billy") == 0);
1552 ASSERT (strcmp (argv[3], "-a") == 0);
1553 ASSERT (strcmp (argv[4], "donald") == 0);
1554 ASSERT (strcmp (argv[5], "duck") == 0);
1555 ASSERT (strcmp (argv[6], "bar") == 0);
1556 ASSERT (argv[7] == NULL);
1557 ASSERT (a_seen == 1);
1558 ASSERT (b_seen == 0);
1559 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1560 ASSERT (q_value == NULL);
1561 ASSERT (non_options_count == 0);
1562 ASSERT (unrecognized == 0);
1563 ASSERT (optind == 4);
1566 /* Check that the '+' flag causes the first non-option to terminate the
1567 loop. */
1568 for (start = 0; start <= 1; start++)
1570 const char *p_value = NULL;
1571 const char *q_value = NULL;
1572 int non_options_count = 0;
1573 const char *non_options[10];
1574 int unrecognized = 0;
1575 int argc = 0;
1576 a_seen = 0;
1577 b_seen = 0;
1579 argv[argc++] = "program";
1580 argv[argc++] = "donald";
1581 argv[argc++] = "-p";
1582 argv[argc++] = "billy";
1583 argv[argc++] = "duck";
1584 argv[argc++] = "-a";
1585 argv[argc++] = "bar";
1586 argv[argc] = NULL;
1587 optind = start;
1588 getopt_long_loop (argc, argv, "+abp:q:", long_options_required,
1589 &p_value, &q_value,
1590 &non_options_count, non_options, &unrecognized);
1591 ASSERT (strcmp (argv[0], "program") == 0);
1592 ASSERT (strcmp (argv[1], "donald") == 0);
1593 ASSERT (strcmp (argv[2], "-p") == 0);
1594 ASSERT (strcmp (argv[3], "billy") == 0);
1595 ASSERT (strcmp (argv[4], "duck") == 0);
1596 ASSERT (strcmp (argv[5], "-a") == 0);
1597 ASSERT (strcmp (argv[6], "bar") == 0);
1598 ASSERT (argv[7] == NULL);
1599 ASSERT (a_seen == 0);
1600 ASSERT (b_seen == 0);
1601 ASSERT (p_value == NULL);
1602 ASSERT (q_value == NULL);
1603 ASSERT (non_options_count == 0);
1604 ASSERT (unrecognized == 0);
1605 ASSERT (optind == 1);
1607 for (start = 0; start <= 1; start++)
1609 const char *p_value = NULL;
1610 const char *q_value = NULL;
1611 int non_options_count = 0;
1612 const char *non_options[10];
1613 int unrecognized = 0;
1614 int argc = 0;
1615 a_seen = 0;
1616 b_seen = 0;
1618 argv[argc++] = "program";
1619 argv[argc++] = "-+";
1620 argv[argc] = NULL;
1621 optind = start;
1622 getopt_long_loop (argc, argv, "+abp:q:", long_options_required,
1623 &p_value, &q_value,
1624 &non_options_count, non_options, &unrecognized);
1625 ASSERT (a_seen == 0);
1626 ASSERT (b_seen == 0);
1627 ASSERT (p_value == NULL);
1628 ASSERT (q_value == NULL);
1629 ASSERT (non_options_count == 0);
1630 ASSERT (unrecognized == '+');
1631 ASSERT (optind == 2);
1634 /* Check that '--' ends the argument processing. */
1635 for (start = 0; start <= 1; start++)
1637 const char *p_value = NULL;
1638 const char *q_value = NULL;
1639 int non_options_count = 0;
1640 const char *non_options[10];
1641 int unrecognized = 0;
1642 int argc = 0;
1643 a_seen = 0;
1644 b_seen = 0;
1646 argv[argc++] = "program";
1647 argv[argc++] = "donald";
1648 argv[argc++] = "-p";
1649 argv[argc++] = "billy";
1650 argv[argc++] = "duck";
1651 argv[argc++] = "-a";
1652 argv[argc++] = "--";
1653 argv[argc++] = "-b";
1654 argv[argc++] = "foo";
1655 argv[argc++] = "-q";
1656 argv[argc++] = "johnny";
1657 argv[argc++] = "bar";
1658 argv[argc] = NULL;
1659 optind = start;
1660 getopt_long_loop (argc, argv, "+abp:q:", long_options_required,
1661 &p_value, &q_value,
1662 &non_options_count, non_options, &unrecognized);
1663 ASSERT (strcmp (argv[0], "program") == 0);
1664 ASSERT (strcmp (argv[1], "donald") == 0);
1665 ASSERT (strcmp (argv[2], "-p") == 0);
1666 ASSERT (strcmp (argv[3], "billy") == 0);
1667 ASSERT (strcmp (argv[4], "duck") == 0);
1668 ASSERT (strcmp (argv[5], "-a") == 0);
1669 ASSERT (strcmp (argv[6], "--") == 0);
1670 ASSERT (strcmp (argv[7], "-b") == 0);
1671 ASSERT (strcmp (argv[8], "foo") == 0);
1672 ASSERT (strcmp (argv[9], "-q") == 0);
1673 ASSERT (strcmp (argv[10], "johnny") == 0);
1674 ASSERT (strcmp (argv[11], "bar") == 0);
1675 ASSERT (argv[12] == NULL);
1676 ASSERT (a_seen == 0);
1677 ASSERT (b_seen == 0);
1678 ASSERT (p_value == NULL);
1679 ASSERT (q_value == NULL);
1680 ASSERT (non_options_count == 0);
1681 ASSERT (unrecognized == 0);
1682 ASSERT (optind == 1);
1685 /* Check that the '+' flag has to come first. */
1686 for (start = 0; start <= 1; start++)
1688 const char *p_value = NULL;
1689 const char *q_value = NULL;
1690 int non_options_count = 0;
1691 const char *non_options[10];
1692 int unrecognized = 0;
1693 int argc = 0;
1694 a_seen = 0;
1695 b_seen = 0;
1697 argv[argc++] = "program";
1698 argv[argc++] = "donald";
1699 argv[argc++] = "-p";
1700 argv[argc++] = "billy";
1701 argv[argc++] = "duck";
1702 argv[argc++] = "-a";
1703 argv[argc++] = "bar";
1704 argv[argc] = NULL;
1705 optind = start;
1706 getopt_long_loop (argc, argv, "abp:q:+", long_options_required,
1707 &p_value, &q_value,
1708 &non_options_count, non_options, &unrecognized);
1709 ASSERT (strcmp (argv[0], "program") == 0);
1710 ASSERT (strcmp (argv[1], "-p") == 0);
1711 ASSERT (strcmp (argv[2], "billy") == 0);
1712 ASSERT (strcmp (argv[3], "-a") == 0);
1713 ASSERT (strcmp (argv[4], "donald") == 0);
1714 ASSERT (strcmp (argv[5], "duck") == 0);
1715 ASSERT (strcmp (argv[6], "bar") == 0);
1716 ASSERT (argv[7] == NULL);
1717 ASSERT (a_seen == 1);
1718 ASSERT (b_seen == 0);
1719 ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
1720 ASSERT (q_value == NULL);
1721 ASSERT (non_options_count == 0);
1722 ASSERT (unrecognized == 0);
1723 ASSERT (optind == 4);
1727 /* Test behavior of getopt_long when POSIXLY_CORRECT is set in the
1728 environment. Options with optional arguments should not change
1729 behavior just because of an environment variable.
1730 https://lists.gnu.org/r/bug-m4/2006-09/msg00028.html */
1731 static void
1732 test_getopt_long_posix (void)
1734 const char *argv[20];
1735 int c;
1736 int start;
1738 /* Check that POSIXLY_CORRECT stops parsing the same as leading '+'. */
1739 for (start = 0; start <= 1; start++)
1741 const char *p_value = NULL;
1742 const char *q_value = NULL;
1743 int non_options_count = 0;
1744 const char *non_options[10];
1745 int unrecognized = 0;
1746 int argc = 0;
1747 a_seen = 0;
1748 b_seen = 0;
1750 argv[argc++] = "program";
1751 argv[argc++] = "donald";
1752 argv[argc++] = "-p";
1753 argv[argc++] = "billy";
1754 argv[argc++] = "duck";
1755 argv[argc++] = "-a";
1756 argv[argc++] = "bar";
1757 argv[argc] = NULL;
1758 optind = start;
1759 getopt_long_loop (argc, argv, "abp:q:", long_options_required,
1760 &p_value, &q_value,
1761 &non_options_count, non_options, &unrecognized);
1762 ASSERT (strcmp (argv[0], "program") == 0);
1763 ASSERT (strcmp (argv[1], "donald") == 0);
1764 ASSERT (strcmp (argv[2], "-p") == 0);
1765 ASSERT (strcmp (argv[3], "billy") == 0);
1766 ASSERT (strcmp (argv[4], "duck") == 0);
1767 ASSERT (strcmp (argv[5], "-a") == 0);
1768 ASSERT (strcmp (argv[6], "bar") == 0);
1769 ASSERT (argv[7] == NULL);
1770 ASSERT (a_seen == 0);
1771 ASSERT (b_seen == 0);
1772 ASSERT (p_value == NULL);
1773 ASSERT (q_value == NULL);
1774 ASSERT (non_options_count == 0);
1775 ASSERT (unrecognized == 0);
1776 ASSERT (optind == 1);
1779 /* Check that POSIXLY_CORRECT doesn't change optional arguments. */
1780 for (start = 0; start <= 1; start++)
1782 const char *p_value = NULL;
1783 const char *q_value = NULL;
1784 int non_options_count = 0;
1785 const char *non_options[10];
1786 int unrecognized = 0;
1787 int argc = 0;
1788 a_seen = 0;
1789 b_seen = 0;
1791 argv[argc++] = "program";
1792 argv[argc++] = "-p";
1793 argv[argc++] = "billy";
1794 argv[argc] = NULL;
1795 optind = start;
1796 getopt_long_loop (argc, argv, "p::", long_options_required,
1797 &p_value, &q_value,
1798 &non_options_count, non_options, &unrecognized);
1799 ASSERT (a_seen == 0);
1800 ASSERT (b_seen == 0);
1801 ASSERT (p_value == NULL);
1802 ASSERT (q_value == NULL);
1803 ASSERT (non_options_count == 0);
1804 ASSERT (unrecognized == 0);
1805 ASSERT (optind == 2);
1808 /* Check that leading - still sees options after non-options. */
1809 for (start = 0; start <= 1; start++)
1811 const char *p_value = NULL;
1812 const char *q_value = NULL;
1813 int non_options_count = 0;
1814 const char *non_options[10];
1815 int unrecognized = 0;
1816 int argc = 0;
1817 a_seen = 0;
1818 b_seen = 0;
1820 argv[argc++] = "program";
1821 argv[argc++] = "-a";
1822 argv[argc++] = "billy";
1823 argv[argc++] = "-b";
1824 argv[argc] = NULL;
1825 optind = start;
1826 getopt_long_loop (argc, argv, "-ab", long_options_required,
1827 &p_value, &q_value,
1828 &non_options_count, non_options, &unrecognized);
1829 ASSERT (a_seen == 1);
1830 ASSERT (b_seen == 1);
1831 ASSERT (p_value == NULL);
1832 ASSERT (q_value == NULL);
1833 ASSERT (non_options_count == 1);
1834 ASSERT (strcmp (non_options[0], "billy") == 0);
1835 ASSERT (unrecognized == 0);
1836 ASSERT (optind == 4);
1840 /* Reduce casting, so we can use string literals elsewhere.
1841 getopt_long_only takes an array of char*, but luckily does not
1842 modify those elements, so we can pass const char*. */
1843 static int
1844 do_getopt_long_only (int argc, const char **argv, const char *shortopts,
1845 const struct option *longopts, int *longind)
1847 optind = 1, opterr = 0;
1848 return getopt_long_only (argc, (char **) argv, shortopts, longopts, longind);
1851 static void
1852 test_getopt_long_only (void)
1854 const char *argv[20];
1855 int c;
1856 /* Test disambiguation of options. */
1858 int argc = 0;
1859 int option_index;
1861 argv[argc++] = "program";
1862 argv[argc++] = "-x";
1863 argv[argc] = NULL;
1864 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
1865 &option_index);
1866 ASSERT (c == '?');
1867 ASSERT (optopt == 0);
1870 int argc = 0;
1871 int option_index;
1873 argv[argc++] = "program";
1874 argv[argc++] = "-x";
1875 argv[argc] = NULL;
1876 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
1877 &option_index);
1878 ASSERT (c == 'x');
1879 ASSERT (optopt == 0);
1882 int argc = 0;
1883 int option_index;
1885 argv[argc++] = "program";
1886 argv[argc++] = "--x";
1887 argv[argc] = NULL;
1888 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
1889 &option_index);
1890 ASSERT (c == '?');
1891 ASSERT (optopt == 0);
1894 int argc = 0;
1895 int option_index;
1897 argv[argc++] = "program";
1898 argv[argc++] = "-b";
1899 argv[argc] = NULL;
1900 b_seen = 0;
1901 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
1902 &option_index);
1903 ASSERT (c == 'b');
1904 ASSERT (b_seen == 0);
1907 int argc = 0;
1908 int option_index;
1910 argv[argc++] = "program";
1911 argv[argc++] = "--b";
1912 argv[argc] = NULL;
1913 b_seen = 0;
1914 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
1915 &option_index);
1916 ASSERT (c == 0);
1917 ASSERT (b_seen == 1);
1920 int argc = 0;
1921 int option_index;
1923 argv[argc++] = "program";
1924 argv[argc++] = "-xt";
1925 argv[argc] = NULL;
1926 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
1927 &option_index);
1928 ASSERT (c == '?');
1929 ASSERT (optopt == 0);
1932 int argc = 0;
1933 int option_index;
1935 argv[argc++] = "program";
1936 argv[argc++] = "-xt";
1937 argv[argc] = NULL;
1938 c = do_getopt_long_only (argc, argv, "abx", long_options_required,
1939 &option_index);
1940 ASSERT (c == '?');
1941 ASSERT (optopt == 0);
1944 int argc = 0;
1945 int option_index;
1947 argv[argc++] = "program";
1948 argv[argc++] = "-xtra";
1949 argv[argc] = NULL;
1950 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
1951 &option_index);
1952 ASSERT (c == 1001);
1955 int argc = 0;
1956 int option_index;
1958 argv[argc++] = "program";
1959 argv[argc++] = "-xtreme";
1960 argv[argc] = NULL;
1961 c = do_getopt_long_only (argc, argv, "abx:", long_options_required,
1962 &option_index);
1963 ASSERT (c == 1002);
1966 int argc = 0;
1967 int option_index;
1969 argv[argc++] = "program";
1970 argv[argc++] = "-xtremel";
1971 argv[argc] = NULL;
1972 c = do_getopt_long_only (argc, argv, "ab", long_options_required,
1973 &option_index);
1974 /* glibc getopt_long_only is intentionally different from
1975 getopt_long when handling a prefix that is common to two
1976 spellings, when both spellings have the same option directives.
1977 BSD getopt_long_only treats both cases the same. */
1978 ASSERT (c == 1003 || c == '?');
1979 ASSERT (optind == 2);
1982 int argc = 0;
1983 int option_index;
1985 argv[argc++] = "program";
1986 argv[argc++] = "-xtremel";
1987 argv[argc] = NULL;
1988 c = do_getopt_long_only (argc, argv, "abx::", long_options_required,
1989 &option_index);
1990 /* glibc getopt_long_only is intentionally different from
1991 getopt_long when handling a prefix that is common to two
1992 spellings, when both spellings have the same option directives.
1993 BSD getopt_long_only treats both cases the same. */
1994 ASSERT (c == 1003 || c == '?');
1995 ASSERT (optind == 2);
1996 ASSERT (optarg == NULL);
1999 int argc = 0;
2000 int option_index;
2002 argv[argc++] = "program";
2003 argv[argc++] = "-xtras";
2004 argv[argc] = NULL;
2005 c = do_getopt_long_only (argc, argv, "abx::", long_options_required,
2006 &option_index);
2007 ASSERT (c == 'x');
2008 ASSERT (strcmp (optarg, "tras") == 0);