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. */
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 },
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 },
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
,
57 int option_index
= -1;
62 while ((c
= getopt_long (argc
, (char **) argv
, options
, long_options
,
69 /* An option with a non-NULL flag pointer was processed. */
86 /* Must only happen with option '-' at the beginning. */
87 ASSERT (options
[0] == '-');
88 non_options
[(*non_options_count
)++] = optarg
;
91 /* Must only happen with option ':' at the beginning. */
92 ASSERT (options
[0] == ':'
93 || ((options
[0] == '-' || options
[0] == '+')
94 && options
[1] == ':'));
97 *unrecognized
= optopt
;
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*. */
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
);
118 test_getopt_long (void)
120 const char *argv
[20];
124 /* Test disambiguation of options. */
129 argv
[argc
++] = "program";
130 argv
[argc
++] = "--x";
132 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
134 ASSERT (optopt
== 0);
140 argv
[argc
++] = "program";
141 argv
[argc
++] = "--xt";
143 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
145 ASSERT (optopt
== 0);
151 argv
[argc
++] = "program";
152 argv
[argc
++] = "--xtr";
154 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
156 ASSERT (optopt
== 0);
162 argv
[argc
++] = "program";
163 argv
[argc
++] = "--xtra";
165 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
172 argv
[argc
++] = "program";
173 argv
[argc
++] = "--xtre";
175 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
177 ASSERT (optopt
== 0);
183 argv
[argc
++] = "program";
184 argv
[argc
++] = "--xtrem";
186 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
188 ASSERT (optopt
== 0);
194 argv
[argc
++] = "program";
195 argv
[argc
++] = "--xtreme";
197 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
204 argv
[argc
++] = "program";
205 argv
[argc
++] = "--xtremel";
207 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
214 argv
[argc
++] = "program";
215 argv
[argc
++] = "--xtremely";
217 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
221 /* Check that -W handles unknown options. */
226 argv
[argc
++] = "program";
229 c
= do_getopt_long (argc
, argv
, "W;", long_options_required
, &option_index
);
231 ASSERT (optopt
== 'W');
237 argv
[argc
++] = "program";
238 argv
[argc
++] = "-Wunknown";
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. */
245 ASSERT (optopt
== 0);
246 ASSERT (optarg
== NULL
);
251 ASSERT (strcmp (optarg
, "unknown") == 0);
258 argv
[argc
++] = "program";
260 argv
[argc
++] = "unknown";
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. */
267 ASSERT (optopt
== 0);
268 ASSERT (optarg
== NULL
);
273 ASSERT (strcmp (optarg
, "unknown") == 0);
277 /* Test that 'W' does not dump core:
278 https://sourceware.org/bugzilla/show_bug.cgi?id=12922 */
283 argv
[argc
++] = "program";
285 argv
[argc
++] = "dummy";
287 c
= do_getopt_long (argc
, argv
, "W;", NULL
, &option_index
);
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;
304 argv
[argc
++] = "program";
306 argv
[argc
++] = "foo";
307 argv
[argc
++] = "bar";
310 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
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;
332 argv
[argc
++] = "program";
335 argv
[argc
++] = "foo";
336 argv
[argc
++] = "bar";
339 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
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;
361 argv
[argc
++] = "program";
362 argv
[argc
++] = "-ba";
363 argv
[argc
++] = "foo";
364 argv
[argc
++] = "bar";
367 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
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;
389 argv
[argc
++] = "program";
390 argv
[argc
++] = "-ab";
392 argv
[argc
++] = "foo";
393 argv
[argc
++] = "bar";
396 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
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;
420 argv
[argc
++] = "program";
421 argv
[argc
++] = "--alpha";
422 argv
[argc
++] = "foo";
423 argv
[argc
++] = "bar";
426 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
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;
448 argv
[argc
++] = "program";
449 argv
[argc
++] = "--beta";
450 argv
[argc
++] = "--alpha";
451 argv
[argc
++] = "foo";
452 argv
[argc
++] = "bar";
455 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
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;
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";
486 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
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;
510 argv
[argc
++] = "program";
511 argv
[argc
++] = "-Walpha";
512 argv
[argc
++] = "foo";
513 argv
[argc
++] = "bar";
516 getopt_long_loop (argc
, argv
, "abW;", long_options_required
,
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;
538 argv
[argc
++] = "program";
540 argv
[argc
++] = "beta";
542 argv
[argc
++] = "alpha";
543 argv
[argc
++] = "foo";
544 argv
[argc
++] = "bar";
547 getopt_long_loop (argc
, argv
, "aW;b", long_options_required
,
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;
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";
578 getopt_long_loop (argc
, argv
, "W;ab", long_options_required
,
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;
602 argv
[argc
++] = "program";
603 argv
[argc
++] = "-pfoo";
604 argv
[argc
++] = "bar";
607 getopt_long_loop (argc
, argv
, "p:q:", long_options_required
,
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;
629 argv
[argc
++] = "program";
631 argv
[argc
++] = "foo";
632 argv
[argc
++] = "bar";
635 getopt_long_loop (argc
, argv
, "p:q:", long_options_required
,
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;
657 argv
[argc
++] = "program";
658 argv
[argc
++] = "-ab";
660 argv
[argc
++] = "baz";
661 argv
[argc
++] = "-pfoo";
662 argv
[argc
++] = "bar";
665 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
689 argv
[argc
++] = "program";
690 argv
[argc
++] = "--p=foo";
691 argv
[argc
++] = "bar";
694 getopt_long_loop (argc
, argv
, "p:q:", long_options_required
,
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;
716 argv
[argc
++] = "program";
717 argv
[argc
++] = "--p";
718 argv
[argc
++] = "foo";
719 argv
[argc
++] = "bar";
722 getopt_long_loop (argc
, argv
, "p:q:", long_options_required
,
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;
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";
752 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
776 argv
[argc
++] = "program";
777 argv
[argc
++] = "-Wp=foo";
778 argv
[argc
++] = "bar";
781 getopt_long_loop (argc
, argv
, "p:q:W;", long_options_required
,
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;
803 argv
[argc
++] = "program";
806 argv
[argc
++] = "foo";
807 argv
[argc
++] = "bar";
810 getopt_long_loop (argc
, argv
, "p:W;q:", long_options_required
,
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;
832 argv
[argc
++] = "program";
833 argv
[argc
++] = "-ab";
834 argv
[argc
++] = "-Wq";
835 argv
[argc
++] = "baz";
837 argv
[argc
++] = "p=foo";
838 argv
[argc
++] = "bar";
841 getopt_long_loop (argc
, argv
, "W;abp:q:", long_options_required
,
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;
865 argv
[argc
++] = "program";
866 argv
[argc
++] = "-pfoo";
867 argv
[argc
++] = "bar";
870 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
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;
892 argv
[argc
++] = "program";
894 argv
[argc
++] = "foo";
895 argv
[argc
++] = "bar";
898 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
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;
920 argv
[argc
++] = "program";
923 argv
[argc
++] = "bar";
926 getopt_long_loop (argc
, argv
, "abp::q::", long_options_optional
,
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;
950 argv
[argc
++] = "program";
951 argv
[argc
++] = "--p=foo";
952 argv
[argc
++] = "bar";
955 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
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;
977 argv
[argc
++] = "program";
978 argv
[argc
++] = "--p";
979 argv
[argc
++] = "foo";
980 argv
[argc
++] = "bar";
983 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
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;
1005 argv
[argc
++] = "program";
1006 argv
[argc
++] = "--p=";
1007 argv
[argc
++] = "foo";
1008 argv
[argc
++] = "bar";
1011 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
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;
1033 argv
[argc
++] = "program";
1034 argv
[argc
++] = "--p";
1035 argv
[argc
++] = "-a";
1036 argv
[argc
++] = "bar";
1039 getopt_long_loop (argc
, argv
, "abp::q::", long_options_optional
,
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;
1063 argv
[argc
++] = "program";
1064 argv
[argc
++] = "-Wp=foo";
1065 argv
[argc
++] = "bar";
1068 getopt_long_loop (argc
, argv
, "p::q::W;", long_options_optional
,
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;
1090 argv
[argc
++] = "program";
1091 argv
[argc
++] = "-Wp";
1092 argv
[argc
++] = "foo";
1093 argv
[argc
++] = "bar";
1096 getopt_long_loop (argc
, argv
, "p::q::W;", long_options_optional
,
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;
1118 argv
[argc
++] = "program";
1119 argv
[argc
++] = "-Wp=";
1120 argv
[argc
++] = "foo";
1121 argv
[argc
++] = "bar";
1124 getopt_long_loop (argc
, argv
, "W;p::q::", long_options_optional
,
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;
1146 argv
[argc
++] = "program";
1147 argv
[argc
++] = "-W";
1148 argv
[argc
++] = "p=";
1149 argv
[argc
++] = "foo";
1150 argv
[argc
++] = "bar";
1153 getopt_long_loop (argc
, argv
, "W;p::q::", long_options_optional
,
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;
1175 argv
[argc
++] = "program";
1176 argv
[argc
++] = "-W";
1178 argv
[argc
++] = "-a";
1179 argv
[argc
++] = "bar";
1182 getopt_long_loop (argc
, argv
, "W;abp::q::", long_options_optional
,
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;
1206 argv
[argc
++] = "program";
1207 argv
[argc
++] = "-p";
1208 argv
[argc
++] = "foo";
1209 argv
[argc
++] = "-x";
1210 argv
[argc
++] = "-a";
1211 argv
[argc
++] = "bar";
1214 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
1236 argv
[argc
++] = "program";
1237 argv
[argc
++] = "-p";
1238 argv
[argc
++] = "foo";
1239 argv
[argc
++] = "-:";
1240 argv
[argc
++] = "-a";
1241 argv
[argc
++] = "bar";
1244 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
1268 argv
[argc
++] = "program";
1269 argv
[argc
++] = "-p";
1270 argv
[argc
++] = "foo";
1271 argv
[argc
++] = "--a=";
1272 argv
[argc
++] = "bar";
1275 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
1297 argv
[argc
++] = "program";
1298 argv
[argc
++] = "-p";
1299 argv
[argc
++] = "foo";
1300 argv
[argc
++] = "--b=";
1301 argv
[argc
++] = "bar";
1304 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
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";
1339 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
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";
1385 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
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";
1431 getopt_long_loop (argc
, argv
, "-abp:q:", long_options_required
,
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;
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";
1480 getopt_long_loop (argc
, argv
, "-abp:q:", long_options_required
,
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);
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;
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";
1546 getopt_long_loop (argc
, argv
, "abp:q:-", long_options_required
,
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
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;
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";
1588 getopt_long_loop (argc
, argv
, "+abp:q:", long_options_required
,
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;
1618 argv
[argc
++] = "program";
1619 argv
[argc
++] = "-+";
1622 getopt_long_loop (argc
, argv
, "+abp:q:", long_options_required
,
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;
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";
1660 getopt_long_loop (argc
, argv
, "+abp:q:", long_options_required
,
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;
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";
1706 getopt_long_loop (argc
, argv
, "abp:q:+", long_options_required
,
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 */
1732 test_getopt_long_posix (void)
1734 const char *argv
[20];
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;
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";
1759 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
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;
1791 argv
[argc
++] = "program";
1792 argv
[argc
++] = "-p";
1793 argv
[argc
++] = "billy";
1796 getopt_long_loop (argc
, argv
, "p::", long_options_required
,
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;
1820 argv
[argc
++] = "program";
1821 argv
[argc
++] = "-a";
1822 argv
[argc
++] = "billy";
1823 argv
[argc
++] = "-b";
1826 getopt_long_loop (argc
, argv
, "-ab", long_options_required
,
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*. */
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
);
1852 test_getopt_long_only (void)
1854 const char *argv
[20];
1856 /* Test disambiguation of options. */
1861 argv
[argc
++] = "program";
1862 argv
[argc
++] = "-x";
1864 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
1867 ASSERT (optopt
== 0);
1873 argv
[argc
++] = "program";
1874 argv
[argc
++] = "-x";
1876 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
1879 ASSERT (optopt
== 0);
1885 argv
[argc
++] = "program";
1886 argv
[argc
++] = "--x";
1888 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
1891 ASSERT (optopt
== 0);
1897 argv
[argc
++] = "program";
1898 argv
[argc
++] = "-b";
1901 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
1904 ASSERT (b_seen
== 0);
1910 argv
[argc
++] = "program";
1911 argv
[argc
++] = "--b";
1914 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
1917 ASSERT (b_seen
== 1);
1923 argv
[argc
++] = "program";
1924 argv
[argc
++] = "-xt";
1926 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
1929 ASSERT (optopt
== 0);
1935 argv
[argc
++] = "program";
1936 argv
[argc
++] = "-xt";
1938 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
1941 ASSERT (optopt
== 0);
1947 argv
[argc
++] = "program";
1948 argv
[argc
++] = "-xtra";
1950 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
1958 argv
[argc
++] = "program";
1959 argv
[argc
++] = "-xtreme";
1961 c
= do_getopt_long_only (argc
, argv
, "abx:", long_options_required
,
1969 argv
[argc
++] = "program";
1970 argv
[argc
++] = "-xtremel";
1972 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
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);
1985 argv
[argc
++] = "program";
1986 argv
[argc
++] = "-xtremel";
1988 c
= do_getopt_long_only (argc
, argv
, "abx::", long_options_required
,
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
);
2002 argv
[argc
++] = "program";
2003 argv
[argc
++] = "-xtras";
2005 c
= do_getopt_long_only (argc
, argv
, "abx::", long_options_required
,
2008 ASSERT (strcmp (optarg
, "tras") == 0);