1 /* Test of command line argument processing.
2 Copyright (C) 2009-2017 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
23 static const struct option long_options_required
[] =
25 { "alpha", no_argument
, NULL
, 'a' },
26 { "beta", no_argument
, &b_seen
, 1 },
27 { "prune", required_argument
, NULL
, 'p' },
28 { "quetsche", required_argument
, &q_seen
, 1 },
29 { "xtremely-",no_argument
, NULL
, 1003 },
30 { "xtra", no_argument
, NULL
, 1001 },
31 { "xtreme", no_argument
, NULL
, 1002 },
32 { "xtremely", no_argument
, NULL
, 1003 },
36 static const struct option long_options_optional
[] =
38 { "alpha", no_argument
, NULL
, 'a' },
39 { "beta", no_argument
, &b_seen
, 1 },
40 { "prune", optional_argument
, NULL
, 'p' },
41 { "quetsche", optional_argument
, &q_seen
, 1 },
46 getopt_long_loop (int argc
, const char **argv
,
47 const char *options
, const struct option
*long_options
,
48 const char **p_value
, const char **q_value
,
49 int *non_options_count
, const char **non_options
,
52 int option_index
= -1;
57 while ((c
= getopt_long (argc
, (char **) argv
, options
, long_options
,
64 /* An option with a non-NULL flag pointer was processed. */
81 /* Must only happen with option '-' at the beginning. */
82 ASSERT (options
[0] == '-');
83 non_options
[(*non_options_count
)++] = optarg
;
86 /* Must only happen with option ':' at the beginning. */
87 ASSERT (options
[0] == ':'
88 || ((options
[0] == '-' || options
[0] == '+')
89 && options
[1] == ':'));
92 *unrecognized
= optopt
;
101 /* Reduce casting, so we can use string literals elsewhere.
102 getopt_long takes an array of char*, but luckily does not modify
103 those elements, so we can pass const char*. */
105 do_getopt_long (int argc
, const char **argv
, const char *shortopts
,
106 const struct option
*longopts
, int *longind
)
108 return getopt_long (argc
, (char **) argv
, shortopts
, longopts
, longind
);
112 test_getopt_long (void)
116 /* Test disambiguation of options. */
119 const char *argv
[10];
123 argv
[argc
++] = "program";
124 argv
[argc
++] = "--x";
128 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
130 ASSERT (optopt
== 0);
134 const char *argv
[10];
138 argv
[argc
++] = "program";
139 argv
[argc
++] = "--xt";
143 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
145 ASSERT (optopt
== 0);
149 const char *argv
[10];
153 argv
[argc
++] = "program";
154 argv
[argc
++] = "--xtr";
158 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
160 ASSERT (optopt
== 0);
164 const char *argv
[10];
168 argv
[argc
++] = "program";
169 argv
[argc
++] = "--xtra";
173 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
178 const char *argv
[10];
182 argv
[argc
++] = "program";
183 argv
[argc
++] = "--xtre";
187 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
189 ASSERT (optopt
== 0);
193 const char *argv
[10];
197 argv
[argc
++] = "program";
198 argv
[argc
++] = "--xtrem";
202 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
204 ASSERT (optopt
== 0);
208 const char *argv
[10];
212 argv
[argc
++] = "program";
213 argv
[argc
++] = "--xtreme";
217 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
222 const char *argv
[10];
226 argv
[argc
++] = "program";
227 argv
[argc
++] = "--xtremel";
231 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
236 const char *argv
[10];
240 argv
[argc
++] = "program";
241 argv
[argc
++] = "--xtremely";
245 c
= do_getopt_long (argc
, argv
, "ab", long_options_required
, &option_index
);
249 /* Check that -W handles unknown options. */
252 const char *argv
[10];
256 argv
[argc
++] = "program";
261 c
= do_getopt_long (argc
, argv
, "W;", long_options_required
, &option_index
);
263 ASSERT (optopt
== 'W');
267 const char *argv
[10];
271 argv
[argc
++] = "program";
272 argv
[argc
++] = "-Wunknown";
276 c
= do_getopt_long (argc
, argv
, "W;", long_options_required
, &option_index
);
277 /* glibc and BSD behave differently here, but for now, we allow
278 both behaviors since W support is not frequently used. */
281 ASSERT (optopt
== 0);
282 ASSERT (optarg
== NULL
);
287 ASSERT (strcmp (optarg
, "unknown") == 0);
292 const char *argv
[10];
296 argv
[argc
++] = "program";
298 argv
[argc
++] = "unknown";
302 c
= do_getopt_long (argc
, argv
, "W;", long_options_required
, &option_index
);
303 /* glibc and BSD behave differently here, but for now, we allow
304 both behaviors since W support is not frequently used. */
307 ASSERT (optopt
== 0);
308 ASSERT (optarg
== NULL
);
313 ASSERT (strcmp (optarg
, "unknown") == 0);
317 /* Test that 'W' does not dump core:
318 http://sourceware.org/bugzilla/show_bug.cgi?id=12922 */
321 const char *argv
[10];
325 argv
[argc
++] = "program";
327 argv
[argc
++] = "dummy";
331 c
= do_getopt_long (argc
, argv
, "W;", NULL
, &option_index
);
333 ASSERT (optind
== 2);
336 /* Test processing of boolean short options. */
337 for (start
= 0; start
<= 1; start
++)
339 const char *p_value
= NULL
;
340 const char *q_value
= NULL
;
341 int non_options_count
= 0;
342 const char *non_options
[10];
343 int unrecognized
= 0;
345 const char *argv
[10];
349 argv
[argc
++] = "program";
351 argv
[argc
++] = "foo";
352 argv
[argc
++] = "bar";
355 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
357 &non_options_count
, non_options
, &unrecognized
);
358 ASSERT (a_seen
== 1);
359 ASSERT (b_seen
== 0);
360 ASSERT (p_value
== NULL
);
361 ASSERT (q_value
== NULL
);
362 ASSERT (non_options_count
== 0);
363 ASSERT (unrecognized
== 0);
364 ASSERT (optind
== 2);
366 for (start
= 0; start
<= 1; start
++)
368 const char *p_value
= NULL
;
369 const char *q_value
= NULL
;
370 int non_options_count
= 0;
371 const char *non_options
[10];
372 int unrecognized
= 0;
374 const char *argv
[10];
378 argv
[argc
++] = "program";
381 argv
[argc
++] = "foo";
382 argv
[argc
++] = "bar";
385 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
387 &non_options_count
, non_options
, &unrecognized
);
388 ASSERT (a_seen
== 1);
389 ASSERT (b_seen
== 1);
390 ASSERT (p_value
== NULL
);
391 ASSERT (q_value
== NULL
);
392 ASSERT (non_options_count
== 0);
393 ASSERT (unrecognized
== 0);
394 ASSERT (optind
== 3);
396 for (start
= 0; start
<= 1; start
++)
398 const char *p_value
= NULL
;
399 const char *q_value
= NULL
;
400 int non_options_count
= 0;
401 const char *non_options
[10];
402 int unrecognized
= 0;
404 const char *argv
[10];
408 argv
[argc
++] = "program";
409 argv
[argc
++] = "-ba";
410 argv
[argc
++] = "foo";
411 argv
[argc
++] = "bar";
414 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
416 &non_options_count
, non_options
, &unrecognized
);
417 ASSERT (a_seen
== 1);
418 ASSERT (b_seen
== 1);
419 ASSERT (p_value
== NULL
);
420 ASSERT (q_value
== NULL
);
421 ASSERT (non_options_count
== 0);
422 ASSERT (unrecognized
== 0);
423 ASSERT (optind
== 2);
425 for (start
= 0; start
<= 1; start
++)
427 const char *p_value
= NULL
;
428 const char *q_value
= NULL
;
429 int non_options_count
= 0;
430 const char *non_options
[10];
431 int unrecognized
= 0;
433 const char *argv
[10];
437 argv
[argc
++] = "program";
438 argv
[argc
++] = "-ab";
440 argv
[argc
++] = "foo";
441 argv
[argc
++] = "bar";
444 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
446 &non_options_count
, non_options
, &unrecognized
);
447 ASSERT (a_seen
== 2);
448 ASSERT (b_seen
== 1);
449 ASSERT (p_value
== NULL
);
450 ASSERT (q_value
== NULL
);
451 ASSERT (non_options_count
== 0);
452 ASSERT (unrecognized
== 0);
453 ASSERT (optind
== 3);
456 /* Test processing of boolean long options. */
457 for (start
= 0; start
<= 1; start
++)
459 const char *p_value
= NULL
;
460 const char *q_value
= NULL
;
461 int non_options_count
= 0;
462 const char *non_options
[10];
463 int unrecognized
= 0;
465 const char *argv
[10];
469 argv
[argc
++] = "program";
470 argv
[argc
++] = "--alpha";
471 argv
[argc
++] = "foo";
472 argv
[argc
++] = "bar";
475 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
477 &non_options_count
, non_options
, &unrecognized
);
478 ASSERT (a_seen
== 1);
479 ASSERT (b_seen
== 0);
480 ASSERT (p_value
== NULL
);
481 ASSERT (q_value
== NULL
);
482 ASSERT (non_options_count
== 0);
483 ASSERT (unrecognized
== 0);
484 ASSERT (optind
== 2);
486 for (start
= 0; start
<= 1; start
++)
488 const char *p_value
= NULL
;
489 const char *q_value
= NULL
;
490 int non_options_count
= 0;
491 const char *non_options
[10];
492 int unrecognized
= 0;
494 const char *argv
[10];
498 argv
[argc
++] = "program";
499 argv
[argc
++] = "--beta";
500 argv
[argc
++] = "--alpha";
501 argv
[argc
++] = "foo";
502 argv
[argc
++] = "bar";
505 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
507 &non_options_count
, non_options
, &unrecognized
);
508 ASSERT (a_seen
== 1);
509 ASSERT (b_seen
== 1);
510 ASSERT (p_value
== NULL
);
511 ASSERT (q_value
== NULL
);
512 ASSERT (non_options_count
== 0);
513 ASSERT (unrecognized
== 0);
514 ASSERT (optind
== 3);
516 for (start
= 0; start
<= 1; start
++)
518 const char *p_value
= NULL
;
519 const char *q_value
= NULL
;
520 int non_options_count
= 0;
521 const char *non_options
[10];
522 int unrecognized
= 0;
524 const char *argv
[10];
528 argv
[argc
++] = "program";
529 argv
[argc
++] = "--alpha";
530 argv
[argc
++] = "--beta";
531 argv
[argc
++] = "--alpha";
532 argv
[argc
++] = "--beta";
533 argv
[argc
++] = "foo";
534 argv
[argc
++] = "bar";
537 getopt_long_loop (argc
, argv
, "ab", long_options_required
,
539 &non_options_count
, non_options
, &unrecognized
);
540 ASSERT (a_seen
== 2);
541 ASSERT (b_seen
== 1);
542 ASSERT (p_value
== NULL
);
543 ASSERT (q_value
== NULL
);
544 ASSERT (non_options_count
== 0);
545 ASSERT (unrecognized
== 0);
546 ASSERT (optind
== 5);
549 /* Test processing of boolean long options via -W. */
550 for (start
= 0; start
<= 1; start
++)
552 const char *p_value
= NULL
;
553 const char *q_value
= NULL
;
554 int non_options_count
= 0;
555 const char *non_options
[10];
556 int unrecognized
= 0;
558 const char *argv
[10];
562 argv
[argc
++] = "program";
563 argv
[argc
++] = "-Walpha";
564 argv
[argc
++] = "foo";
565 argv
[argc
++] = "bar";
568 getopt_long_loop (argc
, argv
, "abW;", long_options_required
,
570 &non_options_count
, non_options
, &unrecognized
);
571 ASSERT (a_seen
== 1);
572 ASSERT (b_seen
== 0);
573 ASSERT (p_value
== NULL
);
574 ASSERT (q_value
== NULL
);
575 ASSERT (non_options_count
== 0);
576 ASSERT (unrecognized
== 0);
577 ASSERT (optind
== 2);
579 for (start
= 0; start
<= 1; start
++)
581 const char *p_value
= NULL
;
582 const char *q_value
= NULL
;
583 int non_options_count
= 0;
584 const char *non_options
[10];
585 int unrecognized
= 0;
587 const char *argv
[10];
591 argv
[argc
++] = "program";
593 argv
[argc
++] = "beta";
595 argv
[argc
++] = "alpha";
596 argv
[argc
++] = "foo";
597 argv
[argc
++] = "bar";
600 getopt_long_loop (argc
, argv
, "aW;b", long_options_required
,
602 &non_options_count
, non_options
, &unrecognized
);
603 ASSERT (a_seen
== 1);
604 ASSERT (b_seen
== 1);
605 ASSERT (p_value
== NULL
);
606 ASSERT (q_value
== NULL
);
607 ASSERT (non_options_count
== 0);
608 ASSERT (unrecognized
== 0);
609 ASSERT (optind
== 5);
611 for (start
= 0; start
<= 1; start
++)
613 const char *p_value
= NULL
;
614 const char *q_value
= NULL
;
615 int non_options_count
= 0;
616 const char *non_options
[10];
617 int unrecognized
= 0;
619 const char *argv
[10];
623 argv
[argc
++] = "program";
624 argv
[argc
++] = "-Walpha";
625 argv
[argc
++] = "-Wbeta";
626 argv
[argc
++] = "-Walpha";
627 argv
[argc
++] = "-Wbeta";
628 argv
[argc
++] = "foo";
629 argv
[argc
++] = "bar";
632 getopt_long_loop (argc
, argv
, "W;ab", long_options_required
,
634 &non_options_count
, non_options
, &unrecognized
);
635 ASSERT (a_seen
== 2);
636 ASSERT (b_seen
== 1);
637 ASSERT (p_value
== NULL
);
638 ASSERT (q_value
== NULL
);
639 ASSERT (non_options_count
== 0);
640 ASSERT (unrecognized
== 0);
641 ASSERT (optind
== 5);
644 /* Test processing of short options with arguments. */
645 for (start
= 0; start
<= 1; start
++)
647 const char *p_value
= NULL
;
648 const char *q_value
= NULL
;
649 int non_options_count
= 0;
650 const char *non_options
[10];
651 int unrecognized
= 0;
653 const char *argv
[10];
657 argv
[argc
++] = "program";
658 argv
[argc
++] = "-pfoo";
659 argv
[argc
++] = "bar";
662 getopt_long_loop (argc
, argv
, "p:q:", long_options_required
,
664 &non_options_count
, non_options
, &unrecognized
);
665 ASSERT (a_seen
== 0);
666 ASSERT (b_seen
== 0);
667 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
668 ASSERT (q_value
== NULL
);
669 ASSERT (non_options_count
== 0);
670 ASSERT (unrecognized
== 0);
671 ASSERT (optind
== 2);
673 for (start
= 0; start
<= 1; start
++)
675 const char *p_value
= NULL
;
676 const char *q_value
= NULL
;
677 int non_options_count
= 0;
678 const char *non_options
[10];
679 int unrecognized
= 0;
681 const char *argv
[10];
685 argv
[argc
++] = "program";
687 argv
[argc
++] = "foo";
688 argv
[argc
++] = "bar";
691 getopt_long_loop (argc
, argv
, "p:q:", long_options_required
,
693 &non_options_count
, non_options
, &unrecognized
);
694 ASSERT (a_seen
== 0);
695 ASSERT (b_seen
== 0);
696 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
697 ASSERT (q_value
== NULL
);
698 ASSERT (non_options_count
== 0);
699 ASSERT (unrecognized
== 0);
700 ASSERT (optind
== 3);
702 for (start
= 0; start
<= 1; start
++)
704 const char *p_value
= NULL
;
705 const char *q_value
= NULL
;
706 int non_options_count
= 0;
707 const char *non_options
[10];
708 int unrecognized
= 0;
710 const char *argv
[10];
714 argv
[argc
++] = "program";
715 argv
[argc
++] = "-ab";
717 argv
[argc
++] = "baz";
718 argv
[argc
++] = "-pfoo";
719 argv
[argc
++] = "bar";
722 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
724 &non_options_count
, non_options
, &unrecognized
);
725 ASSERT (a_seen
== 1);
726 ASSERT (b_seen
== 1);
727 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
728 ASSERT (q_value
!= NULL
&& strcmp (q_value
, "baz") == 0);
729 ASSERT (non_options_count
== 0);
730 ASSERT (unrecognized
== 0);
731 ASSERT (optind
== 5);
734 /* Test processing of long options with arguments. */
735 for (start
= 0; start
<= 1; start
++)
737 const char *p_value
= NULL
;
738 const char *q_value
= NULL
;
739 int non_options_count
= 0;
740 const char *non_options
[10];
741 int unrecognized
= 0;
743 const char *argv
[10];
747 argv
[argc
++] = "program";
748 argv
[argc
++] = "--p=foo";
749 argv
[argc
++] = "bar";
752 getopt_long_loop (argc
, argv
, "p:q:", long_options_required
,
754 &non_options_count
, non_options
, &unrecognized
);
755 ASSERT (a_seen
== 0);
756 ASSERT (b_seen
== 0);
757 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
758 ASSERT (q_value
== NULL
);
759 ASSERT (non_options_count
== 0);
760 ASSERT (unrecognized
== 0);
761 ASSERT (optind
== 2);
763 for (start
= 0; start
<= 1; start
++)
765 const char *p_value
= NULL
;
766 const char *q_value
= NULL
;
767 int non_options_count
= 0;
768 const char *non_options
[10];
769 int unrecognized
= 0;
771 const char *argv
[10];
775 argv
[argc
++] = "program";
776 argv
[argc
++] = "--p";
777 argv
[argc
++] = "foo";
778 argv
[argc
++] = "bar";
781 getopt_long_loop (argc
, argv
, "p:q:", 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
== 3);
792 for (start
= 0; start
<= 1; start
++)
794 const char *p_value
= NULL
;
795 const char *q_value
= NULL
;
796 int non_options_count
= 0;
797 const char *non_options
[10];
798 int unrecognized
= 0;
800 const char *argv
[10];
804 argv
[argc
++] = "program";
805 argv
[argc
++] = "-ab";
806 argv
[argc
++] = "--q";
807 argv
[argc
++] = "baz";
808 argv
[argc
++] = "--p=foo";
809 argv
[argc
++] = "bar";
812 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
814 &non_options_count
, non_options
, &unrecognized
);
815 ASSERT (a_seen
== 1);
816 ASSERT (b_seen
== 1);
817 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
818 ASSERT (q_value
!= NULL
&& strcmp (q_value
, "baz") == 0);
819 ASSERT (non_options_count
== 0);
820 ASSERT (unrecognized
== 0);
821 ASSERT (optind
== 5);
824 /* Test processing of long options with arguments via -W. */
825 for (start
= 0; start
<= 1; start
++)
827 const char *p_value
= NULL
;
828 const char *q_value
= NULL
;
829 int non_options_count
= 0;
830 const char *non_options
[10];
831 int unrecognized
= 0;
833 const char *argv
[10];
837 argv
[argc
++] = "program";
838 argv
[argc
++] = "-Wp=foo";
839 argv
[argc
++] = "bar";
842 getopt_long_loop (argc
, argv
, "p:q:W;", long_options_required
,
844 &non_options_count
, non_options
, &unrecognized
);
845 ASSERT (a_seen
== 0);
846 ASSERT (b_seen
== 0);
847 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
848 ASSERT (q_value
== NULL
);
849 ASSERT (non_options_count
== 0);
850 ASSERT (unrecognized
== 0);
851 ASSERT (optind
== 2);
853 for (start
= 0; start
<= 1; start
++)
855 const char *p_value
= NULL
;
856 const char *q_value
= NULL
;
857 int non_options_count
= 0;
858 const char *non_options
[10];
859 int unrecognized
= 0;
861 const char *argv
[10];
865 argv
[argc
++] = "program";
868 argv
[argc
++] = "foo";
869 argv
[argc
++] = "bar";
872 getopt_long_loop (argc
, argv
, "p:W;q:", long_options_required
,
874 &non_options_count
, non_options
, &unrecognized
);
875 ASSERT (a_seen
== 0);
876 ASSERT (b_seen
== 0);
877 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
878 ASSERT (q_value
== NULL
);
879 ASSERT (non_options_count
== 0);
880 ASSERT (unrecognized
== 0);
881 ASSERT (optind
== 4);
883 for (start
= 0; start
<= 1; start
++)
885 const char *p_value
= NULL
;
886 const char *q_value
= NULL
;
887 int non_options_count
= 0;
888 const char *non_options
[10];
889 int unrecognized
= 0;
891 const char *argv
[10];
895 argv
[argc
++] = "program";
896 argv
[argc
++] = "-ab";
897 argv
[argc
++] = "-Wq";
898 argv
[argc
++] = "baz";
900 argv
[argc
++] = "p=foo";
901 argv
[argc
++] = "bar";
904 getopt_long_loop (argc
, argv
, "W;abp:q:", long_options_required
,
906 &non_options_count
, non_options
, &unrecognized
);
907 ASSERT (a_seen
== 1);
908 ASSERT (b_seen
== 1);
909 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
910 ASSERT (q_value
!= NULL
&& strcmp (q_value
, "baz") == 0);
911 ASSERT (non_options_count
== 0);
912 ASSERT (unrecognized
== 0);
913 ASSERT (optind
== 6);
916 /* Test processing of short options with optional arguments. */
917 for (start
= 0; start
<= 1; start
++)
919 const char *p_value
= NULL
;
920 const char *q_value
= NULL
;
921 int non_options_count
= 0;
922 const char *non_options
[10];
923 int unrecognized
= 0;
925 const char *argv
[10];
929 argv
[argc
++] = "program";
930 argv
[argc
++] = "-pfoo";
931 argv
[argc
++] = "bar";
934 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
936 &non_options_count
, non_options
, &unrecognized
);
937 ASSERT (a_seen
== 0);
938 ASSERT (b_seen
== 0);
939 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
940 ASSERT (q_value
== NULL
);
941 ASSERT (non_options_count
== 0);
942 ASSERT (unrecognized
== 0);
943 ASSERT (optind
== 2);
945 for (start
= 0; start
<= 1; start
++)
947 const char *p_value
= NULL
;
948 const char *q_value
= NULL
;
949 int non_options_count
= 0;
950 const char *non_options
[10];
951 int unrecognized
= 0;
953 const char *argv
[10];
957 argv
[argc
++] = "program";
959 argv
[argc
++] = "foo";
960 argv
[argc
++] = "bar";
963 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
965 &non_options_count
, non_options
, &unrecognized
);
966 ASSERT (a_seen
== 0);
967 ASSERT (b_seen
== 0);
968 ASSERT (p_value
== NULL
);
969 ASSERT (q_value
== NULL
);
970 ASSERT (non_options_count
== 0);
971 ASSERT (unrecognized
== 0);
972 ASSERT (optind
== 2);
974 for (start
= 0; start
<= 1; start
++)
976 const char *p_value
= NULL
;
977 const char *q_value
= NULL
;
978 int non_options_count
= 0;
979 const char *non_options
[10];
980 int unrecognized
= 0;
982 const char *argv
[10];
986 argv
[argc
++] = "program";
989 argv
[argc
++] = "bar";
992 getopt_long_loop (argc
, argv
, "abp::q::", long_options_optional
,
994 &non_options_count
, non_options
, &unrecognized
);
995 ASSERT (a_seen
== 1);
996 ASSERT (b_seen
== 0);
997 ASSERT (p_value
== NULL
);
998 ASSERT (q_value
== NULL
);
999 ASSERT (non_options_count
== 0);
1000 ASSERT (unrecognized
== 0);
1001 ASSERT (optind
== 3);
1004 /* Test processing of long options with optional arguments. */
1005 for (start
= 0; start
<= 1; start
++)
1007 const char *p_value
= NULL
;
1008 const char *q_value
= NULL
;
1009 int non_options_count
= 0;
1010 const char *non_options
[10];
1011 int unrecognized
= 0;
1013 const char *argv
[10];
1017 argv
[argc
++] = "program";
1018 argv
[argc
++] = "--p=foo";
1019 argv
[argc
++] = "bar";
1022 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
1024 &non_options_count
, non_options
, &unrecognized
);
1025 ASSERT (a_seen
== 0);
1026 ASSERT (b_seen
== 0);
1027 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
1028 ASSERT (q_value
== NULL
);
1029 ASSERT (non_options_count
== 0);
1030 ASSERT (unrecognized
== 0);
1031 ASSERT (optind
== 2);
1033 for (start
= 0; start
<= 1; start
++)
1035 const char *p_value
= NULL
;
1036 const char *q_value
= NULL
;
1037 int non_options_count
= 0;
1038 const char *non_options
[10];
1039 int unrecognized
= 0;
1041 const char *argv
[10];
1045 argv
[argc
++] = "program";
1046 argv
[argc
++] = "--p";
1047 argv
[argc
++] = "foo";
1048 argv
[argc
++] = "bar";
1051 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
1053 &non_options_count
, non_options
, &unrecognized
);
1054 ASSERT (a_seen
== 0);
1055 ASSERT (b_seen
== 0);
1056 ASSERT (p_value
== NULL
);
1057 ASSERT (q_value
== NULL
);
1058 ASSERT (non_options_count
== 0);
1059 ASSERT (unrecognized
== 0);
1060 ASSERT (optind
== 2);
1062 for (start
= 0; start
<= 1; start
++)
1064 const char *p_value
= NULL
;
1065 const char *q_value
= NULL
;
1066 int non_options_count
= 0;
1067 const char *non_options
[10];
1068 int unrecognized
= 0;
1070 const char *argv
[10];
1074 argv
[argc
++] = "program";
1075 argv
[argc
++] = "--p=";
1076 argv
[argc
++] = "foo";
1077 argv
[argc
++] = "bar";
1080 getopt_long_loop (argc
, argv
, "p::q::", long_options_optional
,
1082 &non_options_count
, non_options
, &unrecognized
);
1083 ASSERT (a_seen
== 0);
1084 ASSERT (b_seen
== 0);
1085 ASSERT (p_value
!= NULL
&& *p_value
== '\0');
1086 ASSERT (q_value
== NULL
);
1087 ASSERT (non_options_count
== 0);
1088 ASSERT (unrecognized
== 0);
1089 ASSERT (optind
== 2);
1091 for (start
= 0; start
<= 1; start
++)
1093 const char *p_value
= NULL
;
1094 const char *q_value
= NULL
;
1095 int non_options_count
= 0;
1096 const char *non_options
[10];
1097 int unrecognized
= 0;
1099 const char *argv
[10];
1103 argv
[argc
++] = "program";
1104 argv
[argc
++] = "--p";
1105 argv
[argc
++] = "-a";
1106 argv
[argc
++] = "bar";
1109 getopt_long_loop (argc
, argv
, "abp::q::", long_options_optional
,
1111 &non_options_count
, non_options
, &unrecognized
);
1112 ASSERT (a_seen
== 1);
1113 ASSERT (b_seen
== 0);
1114 ASSERT (p_value
== NULL
);
1115 ASSERT (q_value
== NULL
);
1116 ASSERT (non_options_count
== 0);
1117 ASSERT (unrecognized
== 0);
1118 ASSERT (optind
== 3);
1121 /* Test processing of long options with optional arguments via -W. */
1122 for (start
= 0; start
<= 1; start
++)
1124 const char *p_value
= NULL
;
1125 const char *q_value
= NULL
;
1126 int non_options_count
= 0;
1127 const char *non_options
[10];
1128 int unrecognized
= 0;
1130 const char *argv
[10];
1134 argv
[argc
++] = "program";
1135 argv
[argc
++] = "-Wp=foo";
1136 argv
[argc
++] = "bar";
1139 getopt_long_loop (argc
, argv
, "p::q::W;", long_options_optional
,
1141 &non_options_count
, non_options
, &unrecognized
);
1142 ASSERT (a_seen
== 0);
1143 ASSERT (b_seen
== 0);
1144 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
1145 ASSERT (q_value
== NULL
);
1146 ASSERT (non_options_count
== 0);
1147 ASSERT (unrecognized
== 0);
1148 ASSERT (optind
== 2);
1150 for (start
= 0; start
<= 1; start
++)
1152 const char *p_value
= NULL
;
1153 const char *q_value
= NULL
;
1154 int non_options_count
= 0;
1155 const char *non_options
[10];
1156 int unrecognized
= 0;
1158 const char *argv
[10];
1162 argv
[argc
++] = "program";
1163 argv
[argc
++] = "-Wp";
1164 argv
[argc
++] = "foo";
1165 argv
[argc
++] = "bar";
1168 getopt_long_loop (argc
, argv
, "p::q::W;", long_options_optional
,
1170 &non_options_count
, non_options
, &unrecognized
);
1171 ASSERT (a_seen
== 0);
1172 ASSERT (b_seen
== 0);
1173 ASSERT (p_value
== NULL
);
1174 ASSERT (q_value
== NULL
);
1175 ASSERT (non_options_count
== 0);
1176 ASSERT (unrecognized
== 0);
1177 ASSERT (optind
== 2);
1179 for (start
= 0; start
<= 1; start
++)
1181 const char *p_value
= NULL
;
1182 const char *q_value
= NULL
;
1183 int non_options_count
= 0;
1184 const char *non_options
[10];
1185 int unrecognized
= 0;
1187 const char *argv
[10];
1191 argv
[argc
++] = "program";
1192 argv
[argc
++] = "-Wp=";
1193 argv
[argc
++] = "foo";
1194 argv
[argc
++] = "bar";
1197 getopt_long_loop (argc
, argv
, "W;p::q::", long_options_optional
,
1199 &non_options_count
, non_options
, &unrecognized
);
1200 ASSERT (a_seen
== 0);
1201 ASSERT (b_seen
== 0);
1202 ASSERT (p_value
!= NULL
&& *p_value
== '\0');
1203 ASSERT (q_value
== NULL
);
1204 ASSERT (non_options_count
== 0);
1205 ASSERT (unrecognized
== 0);
1206 ASSERT (optind
== 2);
1208 for (start
= 0; start
<= 1; start
++)
1210 const char *p_value
= NULL
;
1211 const char *q_value
= NULL
;
1212 int non_options_count
= 0;
1213 const char *non_options
[10];
1214 int unrecognized
= 0;
1216 const char *argv
[10];
1220 argv
[argc
++] = "program";
1221 argv
[argc
++] = "-W";
1222 argv
[argc
++] = "p=";
1223 argv
[argc
++] = "foo";
1224 argv
[argc
++] = "bar";
1227 getopt_long_loop (argc
, argv
, "W;p::q::", long_options_optional
,
1229 &non_options_count
, non_options
, &unrecognized
);
1230 ASSERT (a_seen
== 0);
1231 ASSERT (b_seen
== 0);
1232 ASSERT (p_value
!= NULL
&& *p_value
== '\0');
1233 ASSERT (q_value
== NULL
);
1234 ASSERT (non_options_count
== 0);
1235 ASSERT (unrecognized
== 0);
1236 ASSERT (optind
== 3);
1238 for (start
= 0; start
<= 1; start
++)
1240 const char *p_value
= NULL
;
1241 const char *q_value
= NULL
;
1242 int non_options_count
= 0;
1243 const char *non_options
[10];
1244 int unrecognized
= 0;
1246 const char *argv
[10];
1250 argv
[argc
++] = "program";
1251 argv
[argc
++] = "-W";
1253 argv
[argc
++] = "-a";
1254 argv
[argc
++] = "bar";
1257 getopt_long_loop (argc
, argv
, "W;abp::q::", long_options_optional
,
1259 &non_options_count
, non_options
, &unrecognized
);
1260 ASSERT (a_seen
== 1);
1261 ASSERT (b_seen
== 0);
1262 /* ASSERT (p_value == NULL); */
1263 ASSERT (q_value
== NULL
);
1264 ASSERT (non_options_count
== 0);
1265 ASSERT (unrecognized
== 0);
1266 ASSERT (optind
== 4);
1269 /* Check that invalid options are recognized. */
1270 for (start
= 0; start
<= 1; start
++)
1272 const char *p_value
= NULL
;
1273 const char *q_value
= NULL
;
1274 int non_options_count
= 0;
1275 const char *non_options
[10];
1276 int unrecognized
= 0;
1278 const char *argv
[10];
1282 argv
[argc
++] = "program";
1283 argv
[argc
++] = "-p";
1284 argv
[argc
++] = "foo";
1285 argv
[argc
++] = "-x";
1286 argv
[argc
++] = "-a";
1287 argv
[argc
++] = "bar";
1290 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
1292 &non_options_count
, non_options
, &unrecognized
);
1293 ASSERT (a_seen
== 1);
1294 ASSERT (b_seen
== 0);
1295 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
1296 ASSERT (q_value
== NULL
);
1297 ASSERT (non_options_count
== 0);
1298 ASSERT (unrecognized
== 'x');
1299 ASSERT (optind
== 5);
1301 for (start
= 0; start
<= 1; start
++)
1303 const char *p_value
= NULL
;
1304 const char *q_value
= NULL
;
1305 int non_options_count
= 0;
1306 const char *non_options
[10];
1307 int unrecognized
= 0;
1309 const char *argv
[10];
1313 argv
[argc
++] = "program";
1314 argv
[argc
++] = "-p";
1315 argv
[argc
++] = "foo";
1316 argv
[argc
++] = "-:";
1317 argv
[argc
++] = "-a";
1318 argv
[argc
++] = "bar";
1321 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
1323 &non_options_count
, non_options
, &unrecognized
);
1324 ASSERT (a_seen
== 1);
1325 ASSERT (b_seen
== 0);
1326 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
1327 ASSERT (q_value
== NULL
);
1328 ASSERT (non_options_count
== 0);
1329 ASSERT (unrecognized
== ':');
1330 ASSERT (optind
== 5);
1333 /* Check that unexpected arguments are recognized. */
1334 for (start
= 0; start
<= 1; start
++)
1336 const char *p_value
= NULL
;
1337 const char *q_value
= NULL
;
1338 int non_options_count
= 0;
1339 const char *non_options
[10];
1340 int unrecognized
= 0;
1342 const char *argv
[10];
1346 argv
[argc
++] = "program";
1347 argv
[argc
++] = "-p";
1348 argv
[argc
++] = "foo";
1349 argv
[argc
++] = "--a=";
1350 argv
[argc
++] = "bar";
1353 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
1355 &non_options_count
, non_options
, &unrecognized
);
1356 ASSERT (a_seen
== 0);
1357 ASSERT (b_seen
== 0);
1358 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
1359 ASSERT (q_value
== NULL
);
1360 ASSERT (non_options_count
== 0);
1361 ASSERT (unrecognized
== 'a');
1362 ASSERT (optind
== 4);
1364 for (start
= 0; start
<= 1; start
++)
1366 const char *p_value
= NULL
;
1367 const char *q_value
= NULL
;
1368 int non_options_count
= 0;
1369 const char *non_options
[10];
1370 int unrecognized
= 0;
1372 const char *argv
[10];
1376 argv
[argc
++] = "program";
1377 argv
[argc
++] = "-p";
1378 argv
[argc
++] = "foo";
1379 argv
[argc
++] = "--b=";
1380 argv
[argc
++] = "bar";
1383 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
1385 &non_options_count
, non_options
, &unrecognized
);
1386 ASSERT (a_seen
== 0);
1387 ASSERT (b_seen
== 0);
1388 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
1389 ASSERT (q_value
== NULL
);
1390 ASSERT (non_options_count
== 0);
1391 /* When flag is non-zero, glibc sets optopt anyway, but BSD
1392 leaves optopt unchanged. */
1393 ASSERT (unrecognized
== 1 || unrecognized
== 0);
1394 ASSERT (optind
== 4);
1397 /* Check that by default, non-options arguments are moved to the end. */
1398 for (start
= 0; start
<= 1; start
++)
1400 const char *p_value
= NULL
;
1401 const char *q_value
= NULL
;
1402 int non_options_count
= 0;
1403 const char *non_options
[10];
1404 int unrecognized
= 0;
1406 const char *argv
[10];
1410 argv
[argc
++] = "program";
1411 argv
[argc
++] = "donald";
1412 argv
[argc
++] = "-p";
1413 argv
[argc
++] = "billy";
1414 argv
[argc
++] = "duck";
1415 argv
[argc
++] = "-a";
1416 argv
[argc
++] = "bar";
1419 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
1421 &non_options_count
, non_options
, &unrecognized
);
1422 ASSERT (strcmp (argv
[0], "program") == 0);
1423 ASSERT (strcmp (argv
[1], "-p") == 0);
1424 ASSERT (strcmp (argv
[2], "billy") == 0);
1425 ASSERT (strcmp (argv
[3], "-a") == 0);
1426 ASSERT (strcmp (argv
[4], "donald") == 0);
1427 ASSERT (strcmp (argv
[5], "duck") == 0);
1428 ASSERT (strcmp (argv
[6], "bar") == 0);
1429 ASSERT (argv
[7] == NULL
);
1430 ASSERT (a_seen
== 1);
1431 ASSERT (b_seen
== 0);
1432 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1433 ASSERT (q_value
== NULL
);
1434 ASSERT (non_options_count
== 0);
1435 ASSERT (unrecognized
== 0);
1436 ASSERT (optind
== 4);
1439 /* Check that '--' ends the argument processing. */
1440 for (start
= 0; start
<= 1; start
++)
1442 const char *p_value
= NULL
;
1443 const char *q_value
= NULL
;
1444 int non_options_count
= 0;
1445 const char *non_options
[10];
1446 int unrecognized
= 0;
1448 const char *argv
[20];
1452 argv
[argc
++] = "program";
1453 argv
[argc
++] = "donald";
1454 argv
[argc
++] = "-p";
1455 argv
[argc
++] = "billy";
1456 argv
[argc
++] = "duck";
1457 argv
[argc
++] = "-a";
1458 argv
[argc
++] = "--";
1459 argv
[argc
++] = "-b";
1460 argv
[argc
++] = "foo";
1461 argv
[argc
++] = "-q";
1462 argv
[argc
++] = "johnny";
1463 argv
[argc
++] = "bar";
1466 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
1468 &non_options_count
, non_options
, &unrecognized
);
1469 ASSERT (strcmp (argv
[0], "program") == 0);
1470 ASSERT (strcmp (argv
[1], "-p") == 0);
1471 ASSERT (strcmp (argv
[2], "billy") == 0);
1472 ASSERT (strcmp (argv
[3], "-a") == 0);
1473 ASSERT (strcmp (argv
[4], "--") == 0);
1474 ASSERT (strcmp (argv
[5], "donald") == 0);
1475 ASSERT (strcmp (argv
[6], "duck") == 0);
1476 ASSERT (strcmp (argv
[7], "-b") == 0);
1477 ASSERT (strcmp (argv
[8], "foo") == 0);
1478 ASSERT (strcmp (argv
[9], "-q") == 0);
1479 ASSERT (strcmp (argv
[10], "johnny") == 0);
1480 ASSERT (strcmp (argv
[11], "bar") == 0);
1481 ASSERT (argv
[12] == NULL
);
1482 ASSERT (a_seen
== 1);
1483 ASSERT (b_seen
== 0);
1484 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1485 ASSERT (q_value
== NULL
);
1486 ASSERT (non_options_count
== 0);
1487 ASSERT (unrecognized
== 0);
1488 ASSERT (optind
== 5);
1491 /* Check that the '-' flag causes non-options to be returned in order. */
1492 for (start
= 0; start
<= 1; start
++)
1494 const char *p_value
= NULL
;
1495 const char *q_value
= NULL
;
1496 int non_options_count
= 0;
1497 const char *non_options
[10];
1498 int unrecognized
= 0;
1500 const char *argv
[10];
1504 argv
[argc
++] = "program";
1505 argv
[argc
++] = "donald";
1506 argv
[argc
++] = "-p";
1507 argv
[argc
++] = "billy";
1508 argv
[argc
++] = "duck";
1509 argv
[argc
++] = "-a";
1510 argv
[argc
++] = "bar";
1513 getopt_long_loop (argc
, argv
, "-abp:q:", long_options_required
,
1515 &non_options_count
, non_options
, &unrecognized
);
1516 ASSERT (strcmp (argv
[0], "program") == 0);
1517 ASSERT (strcmp (argv
[1], "donald") == 0);
1518 ASSERT (strcmp (argv
[2], "-p") == 0);
1519 ASSERT (strcmp (argv
[3], "billy") == 0);
1520 ASSERT (strcmp (argv
[4], "duck") == 0);
1521 ASSERT (strcmp (argv
[5], "-a") == 0);
1522 ASSERT (strcmp (argv
[6], "bar") == 0);
1523 ASSERT (argv
[7] == NULL
);
1524 ASSERT (a_seen
== 1);
1525 ASSERT (b_seen
== 0);
1526 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1527 ASSERT (q_value
== NULL
);
1528 ASSERT (non_options_count
== 3);
1529 ASSERT (strcmp (non_options
[0], "donald") == 0);
1530 ASSERT (strcmp (non_options
[1], "duck") == 0);
1531 ASSERT (strcmp (non_options
[2], "bar") == 0);
1532 ASSERT (unrecognized
== 0);
1533 ASSERT (optind
== 7);
1536 /* Check that '--' ends the argument processing. */
1537 for (start
= 0; start
<= 1; start
++)
1539 const char *p_value
= NULL
;
1540 const char *q_value
= NULL
;
1541 int non_options_count
= 0;
1542 const char *non_options
[10];
1543 int unrecognized
= 0;
1545 const char *argv
[20];
1549 argv
[argc
++] = "program";
1550 argv
[argc
++] = "donald";
1551 argv
[argc
++] = "-p";
1552 argv
[argc
++] = "billy";
1553 argv
[argc
++] = "duck";
1554 argv
[argc
++] = "-a";
1555 argv
[argc
++] = "--";
1556 argv
[argc
++] = "-b";
1557 argv
[argc
++] = "foo";
1558 argv
[argc
++] = "-q";
1559 argv
[argc
++] = "johnny";
1560 argv
[argc
++] = "bar";
1563 getopt_long_loop (argc
, argv
, "-abp:q:", long_options_required
,
1565 &non_options_count
, non_options
, &unrecognized
);
1566 ASSERT (strcmp (argv
[0], "program") == 0);
1567 ASSERT (strcmp (argv
[1], "donald") == 0);
1568 ASSERT (strcmp (argv
[2], "-p") == 0);
1569 ASSERT (strcmp (argv
[3], "billy") == 0);
1570 ASSERT (strcmp (argv
[4], "duck") == 0);
1571 ASSERT (strcmp (argv
[5], "-a") == 0);
1572 ASSERT (strcmp (argv
[6], "--") == 0);
1573 ASSERT (strcmp (argv
[7], "-b") == 0);
1574 ASSERT (strcmp (argv
[8], "foo") == 0);
1575 ASSERT (strcmp (argv
[9], "-q") == 0);
1576 ASSERT (strcmp (argv
[10], "johnny") == 0);
1577 ASSERT (strcmp (argv
[11], "bar") == 0);
1578 ASSERT (argv
[12] == NULL
);
1579 ASSERT (a_seen
== 1);
1580 ASSERT (b_seen
== 0);
1581 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1582 ASSERT (q_value
== NULL
);
1583 if (non_options_count
== 2)
1585 /* glibc behaviour. */
1586 ASSERT (non_options_count
== 2);
1587 ASSERT (strcmp (non_options
[0], "donald") == 0);
1588 ASSERT (strcmp (non_options
[1], "duck") == 0);
1589 ASSERT (unrecognized
== 0);
1590 ASSERT (optind
== 7);
1594 /* Another valid behaviour. */
1595 ASSERT (non_options_count
== 7);
1596 ASSERT (strcmp (non_options
[0], "donald") == 0);
1597 ASSERT (strcmp (non_options
[1], "duck") == 0);
1598 ASSERT (strcmp (non_options
[2], "-b") == 0);
1599 ASSERT (strcmp (non_options
[3], "foo") == 0);
1600 ASSERT (strcmp (non_options
[4], "-q") == 0);
1601 ASSERT (strcmp (non_options
[5], "johnny") == 0);
1602 ASSERT (strcmp (non_options
[6], "bar") == 0);
1603 ASSERT (unrecognized
== 0);
1604 ASSERT (optind
== 12);
1608 /* Check that the '-' flag has to come first. */
1609 for (start
= 0; start
<= 1; start
++)
1611 const char *p_value
= NULL
;
1612 const char *q_value
= NULL
;
1613 int non_options_count
= 0;
1614 const char *non_options
[10];
1615 int unrecognized
= 0;
1617 const char *argv
[10];
1621 argv
[argc
++] = "program";
1622 argv
[argc
++] = "donald";
1623 argv
[argc
++] = "-p";
1624 argv
[argc
++] = "billy";
1625 argv
[argc
++] = "duck";
1626 argv
[argc
++] = "-a";
1627 argv
[argc
++] = "bar";
1630 getopt_long_loop (argc
, argv
, "abp:q:-", long_options_required
,
1632 &non_options_count
, non_options
, &unrecognized
);
1633 ASSERT (strcmp (argv
[0], "program") == 0);
1634 ASSERT (strcmp (argv
[1], "-p") == 0);
1635 ASSERT (strcmp (argv
[2], "billy") == 0);
1636 ASSERT (strcmp (argv
[3], "-a") == 0);
1637 ASSERT (strcmp (argv
[4], "donald") == 0);
1638 ASSERT (strcmp (argv
[5], "duck") == 0);
1639 ASSERT (strcmp (argv
[6], "bar") == 0);
1640 ASSERT (argv
[7] == NULL
);
1641 ASSERT (a_seen
== 1);
1642 ASSERT (b_seen
== 0);
1643 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1644 ASSERT (q_value
== NULL
);
1645 ASSERT (non_options_count
== 0);
1646 ASSERT (unrecognized
== 0);
1647 ASSERT (optind
== 4);
1650 /* Check that the '+' flag causes the first non-option to terminate the
1652 for (start
= 0; start
<= 1; start
++)
1654 const char *p_value
= NULL
;
1655 const char *q_value
= NULL
;
1656 int non_options_count
= 0;
1657 const char *non_options
[10];
1658 int unrecognized
= 0;
1660 const char *argv
[10];
1664 argv
[argc
++] = "program";
1665 argv
[argc
++] = "donald";
1666 argv
[argc
++] = "-p";
1667 argv
[argc
++] = "billy";
1668 argv
[argc
++] = "duck";
1669 argv
[argc
++] = "-a";
1670 argv
[argc
++] = "bar";
1673 getopt_long_loop (argc
, argv
, "+abp:q:", long_options_required
,
1675 &non_options_count
, non_options
, &unrecognized
);
1676 ASSERT (strcmp (argv
[0], "program") == 0);
1677 ASSERT (strcmp (argv
[1], "donald") == 0);
1678 ASSERT (strcmp (argv
[2], "-p") == 0);
1679 ASSERT (strcmp (argv
[3], "billy") == 0);
1680 ASSERT (strcmp (argv
[4], "duck") == 0);
1681 ASSERT (strcmp (argv
[5], "-a") == 0);
1682 ASSERT (strcmp (argv
[6], "bar") == 0);
1683 ASSERT (argv
[7] == NULL
);
1684 ASSERT (a_seen
== 0);
1685 ASSERT (b_seen
== 0);
1686 ASSERT (p_value
== NULL
);
1687 ASSERT (q_value
== NULL
);
1688 ASSERT (non_options_count
== 0);
1689 ASSERT (unrecognized
== 0);
1690 ASSERT (optind
== 1);
1692 for (start
= 0; start
<= 1; start
++)
1694 const char *p_value
= NULL
;
1695 const char *q_value
= NULL
;
1696 int non_options_count
= 0;
1697 const char *non_options
[10];
1698 int unrecognized
= 0;
1700 const char *argv
[10];
1704 argv
[argc
++] = "program";
1705 argv
[argc
++] = "-+";
1708 getopt_long_loop (argc
, argv
, "+abp:q:", long_options_required
,
1710 &non_options_count
, non_options
, &unrecognized
);
1711 ASSERT (a_seen
== 0);
1712 ASSERT (b_seen
== 0);
1713 ASSERT (p_value
== NULL
);
1714 ASSERT (q_value
== NULL
);
1715 ASSERT (non_options_count
== 0);
1716 ASSERT (unrecognized
== '+');
1717 ASSERT (optind
== 2);
1720 /* Check that '--' ends the argument processing. */
1721 for (start
= 0; start
<= 1; start
++)
1723 const char *p_value
= NULL
;
1724 const char *q_value
= NULL
;
1725 int non_options_count
= 0;
1726 const char *non_options
[10];
1727 int unrecognized
= 0;
1729 const char *argv
[20];
1733 argv
[argc
++] = "program";
1734 argv
[argc
++] = "donald";
1735 argv
[argc
++] = "-p";
1736 argv
[argc
++] = "billy";
1737 argv
[argc
++] = "duck";
1738 argv
[argc
++] = "-a";
1739 argv
[argc
++] = "--";
1740 argv
[argc
++] = "-b";
1741 argv
[argc
++] = "foo";
1742 argv
[argc
++] = "-q";
1743 argv
[argc
++] = "johnny";
1744 argv
[argc
++] = "bar";
1747 getopt_long_loop (argc
, argv
, "+abp:q:", long_options_required
,
1749 &non_options_count
, non_options
, &unrecognized
);
1750 ASSERT (strcmp (argv
[0], "program") == 0);
1751 ASSERT (strcmp (argv
[1], "donald") == 0);
1752 ASSERT (strcmp (argv
[2], "-p") == 0);
1753 ASSERT (strcmp (argv
[3], "billy") == 0);
1754 ASSERT (strcmp (argv
[4], "duck") == 0);
1755 ASSERT (strcmp (argv
[5], "-a") == 0);
1756 ASSERT (strcmp (argv
[6], "--") == 0);
1757 ASSERT (strcmp (argv
[7], "-b") == 0);
1758 ASSERT (strcmp (argv
[8], "foo") == 0);
1759 ASSERT (strcmp (argv
[9], "-q") == 0);
1760 ASSERT (strcmp (argv
[10], "johnny") == 0);
1761 ASSERT (strcmp (argv
[11], "bar") == 0);
1762 ASSERT (argv
[12] == NULL
);
1763 ASSERT (a_seen
== 0);
1764 ASSERT (b_seen
== 0);
1765 ASSERT (p_value
== NULL
);
1766 ASSERT (q_value
== NULL
);
1767 ASSERT (non_options_count
== 0);
1768 ASSERT (unrecognized
== 0);
1769 ASSERT (optind
== 1);
1772 /* Check that the '+' flag has to come first. */
1773 for (start
= 0; start
<= 1; start
++)
1775 const char *p_value
= NULL
;
1776 const char *q_value
= NULL
;
1777 int non_options_count
= 0;
1778 const char *non_options
[10];
1779 int unrecognized
= 0;
1781 const char *argv
[10];
1785 argv
[argc
++] = "program";
1786 argv
[argc
++] = "donald";
1787 argv
[argc
++] = "-p";
1788 argv
[argc
++] = "billy";
1789 argv
[argc
++] = "duck";
1790 argv
[argc
++] = "-a";
1791 argv
[argc
++] = "bar";
1794 getopt_long_loop (argc
, argv
, "abp:q:+", long_options_required
,
1796 &non_options_count
, non_options
, &unrecognized
);
1797 ASSERT (strcmp (argv
[0], "program") == 0);
1798 ASSERT (strcmp (argv
[1], "-p") == 0);
1799 ASSERT (strcmp (argv
[2], "billy") == 0);
1800 ASSERT (strcmp (argv
[3], "-a") == 0);
1801 ASSERT (strcmp (argv
[4], "donald") == 0);
1802 ASSERT (strcmp (argv
[5], "duck") == 0);
1803 ASSERT (strcmp (argv
[6], "bar") == 0);
1804 ASSERT (argv
[7] == NULL
);
1805 ASSERT (a_seen
== 1);
1806 ASSERT (b_seen
== 0);
1807 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1808 ASSERT (q_value
== NULL
);
1809 ASSERT (non_options_count
== 0);
1810 ASSERT (unrecognized
== 0);
1811 ASSERT (optind
== 4);
1815 /* Test behavior of getopt_long when POSIXLY_CORRECT is set in the
1816 environment. Options with optional arguments should not change
1817 behavior just because of an environment variable.
1818 http://lists.gnu.org/archive/html/bug-m4/2006-09/msg00028.html */
1820 test_getopt_long_posix (void)
1824 /* Check that POSIXLY_CORRECT stops parsing the same as leading '+'. */
1825 for (start
= 0; start
<= 1; start
++)
1827 const char *p_value
= NULL
;
1828 const char *q_value
= NULL
;
1829 int non_options_count
= 0;
1830 const char *non_options
[10];
1831 int unrecognized
= 0;
1833 const char *argv
[10];
1837 argv
[argc
++] = "program";
1838 argv
[argc
++] = "donald";
1839 argv
[argc
++] = "-p";
1840 argv
[argc
++] = "billy";
1841 argv
[argc
++] = "duck";
1842 argv
[argc
++] = "-a";
1843 argv
[argc
++] = "bar";
1846 getopt_long_loop (argc
, argv
, "abp:q:", long_options_required
,
1848 &non_options_count
, non_options
, &unrecognized
);
1849 ASSERT (strcmp (argv
[0], "program") == 0);
1850 ASSERT (strcmp (argv
[1], "donald") == 0);
1851 ASSERT (strcmp (argv
[2], "-p") == 0);
1852 ASSERT (strcmp (argv
[3], "billy") == 0);
1853 ASSERT (strcmp (argv
[4], "duck") == 0);
1854 ASSERT (strcmp (argv
[5], "-a") == 0);
1855 ASSERT (strcmp (argv
[6], "bar") == 0);
1856 ASSERT (argv
[7] == NULL
);
1857 ASSERT (a_seen
== 0);
1858 ASSERT (b_seen
== 0);
1859 ASSERT (p_value
== NULL
);
1860 ASSERT (q_value
== NULL
);
1861 ASSERT (non_options_count
== 0);
1862 ASSERT (unrecognized
== 0);
1863 ASSERT (optind
== 1);
1866 /* Check that POSIXLY_CORRECT doesn't change optional arguments. */
1867 for (start
= 0; start
<= 1; start
++)
1869 const char *p_value
= NULL
;
1870 const char *q_value
= NULL
;
1871 int non_options_count
= 0;
1872 const char *non_options
[10];
1873 int unrecognized
= 0;
1875 const char *argv
[10];
1879 argv
[argc
++] = "program";
1880 argv
[argc
++] = "-p";
1881 argv
[argc
++] = "billy";
1884 getopt_long_loop (argc
, argv
, "p::", long_options_required
,
1886 &non_options_count
, non_options
, &unrecognized
);
1887 ASSERT (a_seen
== 0);
1888 ASSERT (b_seen
== 0);
1889 ASSERT (p_value
== NULL
);
1890 ASSERT (q_value
== NULL
);
1891 ASSERT (non_options_count
== 0);
1892 ASSERT (unrecognized
== 0);
1893 ASSERT (optind
== 2);
1896 /* Check that leading - still sees options after non-options. */
1897 for (start
= 0; start
<= 1; start
++)
1899 const char *p_value
= NULL
;
1900 const char *q_value
= NULL
;
1901 int non_options_count
= 0;
1902 const char *non_options
[10];
1903 int unrecognized
= 0;
1905 const char *argv
[10];
1909 argv
[argc
++] = "program";
1910 argv
[argc
++] = "-a";
1911 argv
[argc
++] = "billy";
1912 argv
[argc
++] = "-b";
1915 getopt_long_loop (argc
, argv
, "-ab", long_options_required
,
1917 &non_options_count
, non_options
, &unrecognized
);
1918 ASSERT (a_seen
== 1);
1919 ASSERT (b_seen
== 1);
1920 ASSERT (p_value
== NULL
);
1921 ASSERT (q_value
== NULL
);
1922 ASSERT (non_options_count
== 1);
1923 ASSERT (strcmp (non_options
[0], "billy") == 0);
1924 ASSERT (unrecognized
== 0);
1925 ASSERT (optind
== 4);
1929 /* Reduce casting, so we can use string literals elsewhere.
1930 getopt_long_only takes an array of char*, but luckily does not
1931 modify those elements, so we can pass const char*. */
1933 do_getopt_long_only (int argc
, const char **argv
, const char *shortopts
,
1934 const struct option
*longopts
, int *longind
)
1936 return getopt_long_only (argc
, (char **) argv
, shortopts
, longopts
, longind
);
1940 test_getopt_long_only (void)
1942 /* Test disambiguation of options. */
1945 const char *argv
[10];
1949 argv
[argc
++] = "program";
1950 argv
[argc
++] = "-x";
1954 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
1957 ASSERT (optopt
== 0);
1961 const char *argv
[10];
1965 argv
[argc
++] = "program";
1966 argv
[argc
++] = "-x";
1970 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
1973 ASSERT (optopt
== 0);
1977 const char *argv
[10];
1981 argv
[argc
++] = "program";
1982 argv
[argc
++] = "--x";
1986 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
1989 ASSERT (optopt
== 0);
1993 const char *argv
[10];
1997 argv
[argc
++] = "program";
1998 argv
[argc
++] = "-b";
2003 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
2006 ASSERT (b_seen
== 0);
2010 const char *argv
[10];
2014 argv
[argc
++] = "program";
2015 argv
[argc
++] = "--b";
2020 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
2023 ASSERT (b_seen
== 1);
2027 const char *argv
[10];
2031 argv
[argc
++] = "program";
2032 argv
[argc
++] = "-xt";
2036 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
2039 ASSERT (optopt
== 0);
2043 const char *argv
[10];
2047 argv
[argc
++] = "program";
2048 argv
[argc
++] = "-xt";
2052 c
= do_getopt_long_only (argc
, argv
, "abx", long_options_required
,
2055 ASSERT (optopt
== 0);
2059 const char *argv
[10];
2063 argv
[argc
++] = "program";
2064 argv
[argc
++] = "-xtra";
2068 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
2074 const char *argv
[10];
2078 argv
[argc
++] = "program";
2079 argv
[argc
++] = "-xtreme";
2083 c
= do_getopt_long_only (argc
, argv
, "abx:", long_options_required
,
2089 const char *argv
[10];
2093 argv
[argc
++] = "program";
2094 argv
[argc
++] = "-xtremel";
2098 c
= do_getopt_long_only (argc
, argv
, "ab", long_options_required
,
2100 /* glibc getopt_long_only is intentionally different from
2101 getopt_long when handling a prefix that is common to two
2102 spellings, when both spellings have the same option directives.
2103 BSD getopt_long_only treats both cases the same. */
2104 ASSERT (c
== 1003 || c
== '?');
2105 ASSERT (optind
== 2);
2109 const char *argv
[10];
2113 argv
[argc
++] = "program";
2114 argv
[argc
++] = "-xtremel";
2118 c
= do_getopt_long_only (argc
, argv
, "abx::", long_options_required
,
2120 /* glibc getopt_long_only is intentionally different from
2121 getopt_long when handling a prefix that is common to two
2122 spellings, when both spellings have the same option directives.
2123 BSD getopt_long_only treats both cases the same. */
2124 ASSERT (c
== 1003 || c
== '?');
2125 ASSERT (optind
== 2);
2126 ASSERT (optarg
== NULL
);
2130 const char *argv
[10];
2134 argv
[argc
++] = "program";
2135 argv
[argc
++] = "-xtras";
2139 c
= do_getopt_long_only (argc
, argv
, "abx::", long_options_required
,
2142 ASSERT (strcmp (optarg
, "tras") == 0);