1 /* Test of command line argument processing.
2 Copyright (C) 2009-2020 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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
21 /* The glibc/gnulib implementation of getopt supports setting optind =
22 0, but not all other implementations do. This matters for getopt.
23 But for getopt_long, we require GNU compatibility. */
24 #if defined __GETOPT_PREFIX || (__GLIBC__ >= 2 && !defined __UCLIBC__)
26 #elif HAVE_DECL_OPTRESET
27 # define OPTIND_MIN (optreset = 1)
33 getopt_loop (int argc
, const char **argv
,
35 int *a_seen
, int *b_seen
,
36 const char **p_value
, const char **q_value
,
37 int *non_options_count
, const char **non_options
,
38 int *unrecognized
, bool *message_issued
)
41 int pos
= ftell (stderr
);
43 while ((c
= getopt (argc
, (char **) argv
, options
)) != -1)
60 /* Must only happen with option '-' at the beginning. */
61 ASSERT (options
[0] == '-');
62 non_options
[(*non_options_count
)++] = optarg
;
65 /* Must only happen with option ':' at the beginning. */
66 ASSERT (options
[0] == ':'
67 || ((options
[0] == '-' || options
[0] == '+')
68 && options
[1] == ':'));
71 *unrecognized
= optopt
;
79 *message_issued
= pos
< ftell (stderr
);
86 bool posixly
= !!getenv ("POSIXLY_CORRECT");
87 /* See comment in getopt.c:
88 glibc gets a LSB-compliant getopt.
89 Standalone applications get a POSIX-compliant getopt. */
90 #if defined __GETOPT_PREFIX || !(__GLIBC__ >= 2 || defined __MINGW32__)
91 /* Using getopt from gnulib or from a non-glibc system. */
95 /* Test processing of boolean options. */
96 for (start
= OPTIND_MIN
; start
<= 1; start
++)
100 const char *p_value
= NULL
;
101 const char *q_value
= NULL
;
102 int non_options_count
= 0;
103 const char *non_options
[10];
104 int unrecognized
= 0;
107 const char *argv
[10];
109 argv
[argc
++] = "program";
111 argv
[argc
++] = "foo";
112 argv
[argc
++] = "bar";
116 getopt_loop (argc
, argv
, "ab",
117 &a_seen
, &b_seen
, &p_value
, &q_value
,
118 &non_options_count
, non_options
, &unrecognized
, &output
);
119 ASSERT (a_seen
== 1);
120 ASSERT (b_seen
== 0);
121 ASSERT (p_value
== NULL
);
122 ASSERT (q_value
== NULL
);
123 ASSERT (non_options_count
== 0);
124 ASSERT (unrecognized
== 0);
125 ASSERT (optind
== 2);
128 for (start
= OPTIND_MIN
; start
<= 1; start
++)
132 const char *p_value
= NULL
;
133 const char *q_value
= NULL
;
134 int non_options_count
= 0;
135 const char *non_options
[10];
136 int unrecognized
= 0;
139 const char *argv
[10];
141 argv
[argc
++] = "program";
144 argv
[argc
++] = "foo";
145 argv
[argc
++] = "bar";
149 getopt_loop (argc
, argv
, "ab",
150 &a_seen
, &b_seen
, &p_value
, &q_value
,
151 &non_options_count
, non_options
, &unrecognized
, &output
);
152 ASSERT (a_seen
== 1);
153 ASSERT (b_seen
== 1);
154 ASSERT (p_value
== NULL
);
155 ASSERT (q_value
== NULL
);
156 ASSERT (non_options_count
== 0);
157 ASSERT (unrecognized
== 0);
158 ASSERT (optind
== 3);
161 for (start
= OPTIND_MIN
; start
<= 1; start
++)
165 const char *p_value
= NULL
;
166 const char *q_value
= NULL
;
167 int non_options_count
= 0;
168 const char *non_options
[10];
169 int unrecognized
= 0;
172 const char *argv
[10];
174 argv
[argc
++] = "program";
175 argv
[argc
++] = "-ba";
176 argv
[argc
++] = "foo";
177 argv
[argc
++] = "bar";
181 getopt_loop (argc
, argv
, "ab",
182 &a_seen
, &b_seen
, &p_value
, &q_value
,
183 &non_options_count
, non_options
, &unrecognized
, &output
);
184 ASSERT (a_seen
== 1);
185 ASSERT (b_seen
== 1);
186 ASSERT (p_value
== NULL
);
187 ASSERT (q_value
== NULL
);
188 ASSERT (non_options_count
== 0);
189 ASSERT (unrecognized
== 0);
190 ASSERT (optind
== 2);
193 for (start
= OPTIND_MIN
; start
<= 1; start
++)
197 const char *p_value
= NULL
;
198 const char *q_value
= NULL
;
199 int non_options_count
= 0;
200 const char *non_options
[10];
201 int unrecognized
= 0;
204 const char *argv
[10];
206 argv
[argc
++] = "program";
207 argv
[argc
++] = "-ab";
209 argv
[argc
++] = "foo";
210 argv
[argc
++] = "bar";
214 getopt_loop (argc
, argv
, "ab",
215 &a_seen
, &b_seen
, &p_value
, &q_value
,
216 &non_options_count
, non_options
, &unrecognized
, &output
);
217 ASSERT (a_seen
== 2);
218 ASSERT (b_seen
== 1);
219 ASSERT (p_value
== NULL
);
220 ASSERT (q_value
== NULL
);
221 ASSERT (non_options_count
== 0);
222 ASSERT (unrecognized
== 0);
223 ASSERT (optind
== 3);
227 /* Test processing of options with arguments. */
228 for (start
= OPTIND_MIN
; start
<= 1; start
++)
232 const char *p_value
= NULL
;
233 const char *q_value
= NULL
;
234 int non_options_count
= 0;
235 const char *non_options
[10];
236 int unrecognized
= 0;
239 const char *argv
[10];
241 argv
[argc
++] = "program";
242 argv
[argc
++] = "-pfoo";
243 argv
[argc
++] = "bar";
247 getopt_loop (argc
, argv
, "p:q:",
248 &a_seen
, &b_seen
, &p_value
, &q_value
,
249 &non_options_count
, non_options
, &unrecognized
, &output
);
250 ASSERT (a_seen
== 0);
251 ASSERT (b_seen
== 0);
252 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
253 ASSERT (q_value
== NULL
);
254 ASSERT (non_options_count
== 0);
255 ASSERT (unrecognized
== 0);
256 ASSERT (optind
== 2);
259 for (start
= OPTIND_MIN
; start
<= 1; start
++)
263 const char *p_value
= NULL
;
264 const char *q_value
= NULL
;
265 int non_options_count
= 0;
266 const char *non_options
[10];
267 int unrecognized
= 0;
270 const char *argv
[10];
272 argv
[argc
++] = "program";
274 argv
[argc
++] = "foo";
275 argv
[argc
++] = "bar";
279 getopt_loop (argc
, argv
, "p:q:",
280 &a_seen
, &b_seen
, &p_value
, &q_value
,
281 &non_options_count
, non_options
, &unrecognized
, &output
);
282 ASSERT (a_seen
== 0);
283 ASSERT (b_seen
== 0);
284 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
285 ASSERT (q_value
== NULL
);
286 ASSERT (non_options_count
== 0);
287 ASSERT (unrecognized
== 0);
288 ASSERT (optind
== 3);
291 for (start
= OPTIND_MIN
; 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;
302 const char *argv
[10];
304 argv
[argc
++] = "program";
305 argv
[argc
++] = "-ab";
307 argv
[argc
++] = "baz";
308 argv
[argc
++] = "-pfoo";
309 argv
[argc
++] = "bar";
313 getopt_loop (argc
, argv
, "abp:q:",
314 &a_seen
, &b_seen
, &p_value
, &q_value
,
315 &non_options_count
, non_options
, &unrecognized
, &output
);
316 ASSERT (a_seen
== 1);
317 ASSERT (b_seen
== 1);
318 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
319 ASSERT (q_value
!= NULL
&& strcmp (q_value
, "baz") == 0);
320 ASSERT (non_options_count
== 0);
321 ASSERT (unrecognized
== 0);
322 ASSERT (optind
== 5);
326 #if GNULIB_TEST_GETOPT_GNU
327 /* Test processing of options with optional arguments. */
328 for (start
= OPTIND_MIN
; start
<= 1; start
++)
332 const char *p_value
= NULL
;
333 const char *q_value
= NULL
;
334 int non_options_count
= 0;
335 const char *non_options
[10];
336 int unrecognized
= 0;
339 const char *argv
[10];
341 argv
[argc
++] = "program";
342 argv
[argc
++] = "-pfoo";
343 argv
[argc
++] = "bar";
347 getopt_loop (argc
, argv
, "p::q::",
348 &a_seen
, &b_seen
, &p_value
, &q_value
,
349 &non_options_count
, non_options
, &unrecognized
, &output
);
350 ASSERT (a_seen
== 0);
351 ASSERT (b_seen
== 0);
352 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
353 ASSERT (q_value
== NULL
);
354 ASSERT (non_options_count
== 0);
355 ASSERT (unrecognized
== 0);
356 ASSERT (optind
== 2);
359 for (start
= OPTIND_MIN
; start
<= 1; start
++)
363 const char *p_value
= NULL
;
364 const char *q_value
= NULL
;
365 int non_options_count
= 0;
366 const char *non_options
[10];
367 int unrecognized
= 0;
370 const char *argv
[10];
372 argv
[argc
++] = "program";
374 argv
[argc
++] = "foo";
375 argv
[argc
++] = "bar";
379 getopt_loop (argc
, argv
, "p::q::",
380 &a_seen
, &b_seen
, &p_value
, &q_value
,
381 &non_options_count
, non_options
, &unrecognized
, &output
);
382 ASSERT (a_seen
== 0);
383 ASSERT (b_seen
== 0);
384 ASSERT (p_value
== NULL
);
385 ASSERT (q_value
== NULL
);
386 ASSERT (non_options_count
== 0);
387 ASSERT (unrecognized
== 0);
388 ASSERT (optind
== 2);
391 for (start
= OPTIND_MIN
; start
<= 1; start
++)
395 const char *p_value
= NULL
;
396 const char *q_value
= NULL
;
397 int non_options_count
= 0;
398 const char *non_options
[10];
399 int unrecognized
= 0;
402 const char *argv
[10];
404 argv
[argc
++] = "program";
407 argv
[argc
++] = "bar";
411 getopt_loop (argc
, argv
, "abp::q::",
412 &a_seen
, &b_seen
, &p_value
, &q_value
,
413 &non_options_count
, non_options
, &unrecognized
, &output
);
414 ASSERT (a_seen
== 1);
415 ASSERT (b_seen
== 0);
416 ASSERT (p_value
== NULL
);
417 ASSERT (q_value
== NULL
);
418 ASSERT (non_options_count
== 0);
419 ASSERT (unrecognized
== 0);
420 ASSERT (optind
== 3);
423 #endif /* GNULIB_TEST_GETOPT_GNU */
425 /* Check that invalid options are recognized; and that both opterr
426 and leading ':' can silence output. */
427 for (start
= OPTIND_MIN
; start
<= 1; start
++)
431 const char *p_value
= NULL
;
432 const char *q_value
= NULL
;
433 int non_options_count
= 0;
434 const char *non_options
[10];
435 int unrecognized
= 0;
438 const char *argv
[10];
440 argv
[argc
++] = "program";
442 argv
[argc
++] = "foo";
445 argv
[argc
++] = "bar";
449 getopt_loop (argc
, argv
, "abp:q:",
450 &a_seen
, &b_seen
, &p_value
, &q_value
,
451 &non_options_count
, non_options
, &unrecognized
, &output
);
452 ASSERT (a_seen
== 1);
453 ASSERT (b_seen
== 0);
454 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
455 ASSERT (q_value
== NULL
);
456 ASSERT (non_options_count
== 0);
457 ASSERT (unrecognized
== 'x');
458 ASSERT (optind
== 5);
461 for (start
= OPTIND_MIN
; start
<= 1; start
++)
465 const char *p_value
= NULL
;
466 const char *q_value
= NULL
;
467 int non_options_count
= 0;
468 const char *non_options
[10];
469 int unrecognized
= 0;
472 const char *argv
[10];
474 argv
[argc
++] = "program";
476 argv
[argc
++] = "foo";
479 argv
[argc
++] = "bar";
483 getopt_loop (argc
, argv
, "abp:q:",
484 &a_seen
, &b_seen
, &p_value
, &q_value
,
485 &non_options_count
, non_options
, &unrecognized
, &output
);
486 ASSERT (a_seen
== 1);
487 ASSERT (b_seen
== 0);
488 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
489 ASSERT (q_value
== NULL
);
490 ASSERT (non_options_count
== 0);
491 ASSERT (unrecognized
== 'x');
492 ASSERT (optind
== 5);
495 for (start
= OPTIND_MIN
; start
<= 1; start
++)
499 const char *p_value
= NULL
;
500 const char *q_value
= NULL
;
501 int non_options_count
= 0;
502 const char *non_options
[10];
503 int unrecognized
= 0;
506 const char *argv
[10];
508 argv
[argc
++] = "program";
510 argv
[argc
++] = "foo";
513 argv
[argc
++] = "bar";
517 getopt_loop (argc
, argv
, ":abp:q:",
518 &a_seen
, &b_seen
, &p_value
, &q_value
,
519 &non_options_count
, non_options
, &unrecognized
, &output
);
520 ASSERT (a_seen
== 1);
521 ASSERT (b_seen
== 0);
522 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
523 ASSERT (q_value
== NULL
);
524 ASSERT (non_options_count
== 0);
525 ASSERT (unrecognized
== 'x');
526 ASSERT (optind
== 5);
529 for (start
= OPTIND_MIN
; start
<= 1; start
++)
533 const char *p_value
= NULL
;
534 const char *q_value
= NULL
;
535 int non_options_count
= 0;
536 const char *non_options
[10];
537 int unrecognized
= 0;
540 const char *argv
[10];
542 argv
[argc
++] = "program";
544 argv
[argc
++] = "foo";
547 argv
[argc
++] = "bar";
551 getopt_loop (argc
, argv
, "abp:q:",
552 &a_seen
, &b_seen
, &p_value
, &q_value
,
553 &non_options_count
, non_options
, &unrecognized
, &output
);
554 ASSERT (a_seen
== 1);
555 ASSERT (b_seen
== 0);
556 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
557 ASSERT (q_value
== NULL
);
558 ASSERT (non_options_count
== 0);
559 ASSERT (unrecognized
== ':');
560 ASSERT (optind
== 5);
563 for (start
= OPTIND_MIN
; start
<= 1; start
++)
567 const char *p_value
= NULL
;
568 const char *q_value
= NULL
;
569 int non_options_count
= 0;
570 const char *non_options
[10];
571 int unrecognized
= 0;
574 const char *argv
[10];
576 argv
[argc
++] = "program";
578 argv
[argc
++] = "foo";
581 argv
[argc
++] = "bar";
585 getopt_loop (argc
, argv
, "abp:q:",
586 &a_seen
, &b_seen
, &p_value
, &q_value
,
587 &non_options_count
, non_options
, &unrecognized
, &output
);
588 ASSERT (a_seen
== 1);
589 ASSERT (b_seen
== 0);
590 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
591 ASSERT (q_value
== NULL
);
592 ASSERT (non_options_count
== 0);
593 ASSERT (unrecognized
== ':');
594 ASSERT (optind
== 5);
597 for (start
= OPTIND_MIN
; start
<= 1; start
++)
601 const char *p_value
= NULL
;
602 const char *q_value
= NULL
;
603 int non_options_count
= 0;
604 const char *non_options
[10];
605 int unrecognized
= 0;
608 const char *argv
[10];
610 argv
[argc
++] = "program";
612 argv
[argc
++] = "foo";
615 argv
[argc
++] = "bar";
619 getopt_loop (argc
, argv
, ":abp:q:",
620 &a_seen
, &b_seen
, &p_value
, &q_value
,
621 &non_options_count
, non_options
, &unrecognized
, &output
);
622 ASSERT (a_seen
== 1);
623 ASSERT (b_seen
== 0);
624 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "foo") == 0);
625 ASSERT (q_value
== NULL
);
626 ASSERT (non_options_count
== 0);
627 ASSERT (unrecognized
== ':');
628 ASSERT (optind
== 5);
632 /* Check for missing argument behavior. */
633 for (start
= OPTIND_MIN
; start
<= 1; start
++)
637 const char *p_value
= NULL
;
638 const char *q_value
= NULL
;
639 int non_options_count
= 0;
640 const char *non_options
[10];
641 int unrecognized
= 0;
644 const char *argv
[10];
646 argv
[argc
++] = "program";
647 argv
[argc
++] = "-ap";
651 getopt_loop (argc
, argv
, "abp:q:",
652 &a_seen
, &b_seen
, &p_value
, &q_value
,
653 &non_options_count
, non_options
, &unrecognized
, &output
);
654 ASSERT (a_seen
== 1);
655 ASSERT (b_seen
== 0);
656 ASSERT (p_value
== NULL
);
657 ASSERT (q_value
== NULL
);
658 ASSERT (non_options_count
== 0);
659 ASSERT (unrecognized
== 'p');
660 ASSERT (optind
== 2);
663 for (start
= OPTIND_MIN
; start
<= 1; start
++)
667 const char *p_value
= NULL
;
668 const char *q_value
= NULL
;
669 int non_options_count
= 0;
670 const char *non_options
[10];
671 int unrecognized
= 0;
674 const char *argv
[10];
676 argv
[argc
++] = "program";
677 argv
[argc
++] = "-ap";
681 getopt_loop (argc
, argv
, "abp:q:",
682 &a_seen
, &b_seen
, &p_value
, &q_value
,
683 &non_options_count
, non_options
, &unrecognized
, &output
);
684 ASSERT (a_seen
== 1);
685 ASSERT (b_seen
== 0);
686 ASSERT (p_value
== NULL
);
687 ASSERT (q_value
== NULL
);
688 ASSERT (non_options_count
== 0);
689 ASSERT (unrecognized
== 'p');
690 ASSERT (optind
== 2);
693 for (start
= OPTIND_MIN
; start
<= 1; start
++)
697 const char *p_value
= NULL
;
698 const char *q_value
= NULL
;
699 int non_options_count
= 0;
700 const char *non_options
[10];
701 int unrecognized
= 0;
704 const char *argv
[10];
706 argv
[argc
++] = "program";
707 argv
[argc
++] = "-ap";
711 getopt_loop (argc
, argv
, ":abp:q:",
712 &a_seen
, &b_seen
, &p_value
, &q_value
,
713 &non_options_count
, non_options
, &unrecognized
, &output
);
714 ASSERT (a_seen
== 1);
715 ASSERT (b_seen
== 0);
716 ASSERT (p_value
== NULL
);
717 ASSERT (q_value
== NULL
);
718 ASSERT (non_options_count
== 0);
719 ASSERT (unrecognized
== 'p');
720 ASSERT (optind
== 2);
724 /* Check that by default, non-options arguments are moved to the end. */
725 for (start
= OPTIND_MIN
; start
<= 1; start
++)
729 const char *p_value
= NULL
;
730 const char *q_value
= NULL
;
731 int non_options_count
= 0;
732 const char *non_options
[10];
733 int unrecognized
= 0;
736 const char *argv
[10];
738 argv
[argc
++] = "program";
739 argv
[argc
++] = "donald";
741 argv
[argc
++] = "billy";
742 argv
[argc
++] = "duck";
744 argv
[argc
++] = "bar";
748 getopt_loop (argc
, argv
, "abp:q:",
749 &a_seen
, &b_seen
, &p_value
, &q_value
,
750 &non_options_count
, non_options
, &unrecognized
, &output
);
753 ASSERT (strcmp (argv
[0], "program") == 0);
754 ASSERT (strcmp (argv
[1], "donald") == 0);
755 ASSERT (strcmp (argv
[2], "-p") == 0);
756 ASSERT (strcmp (argv
[3], "billy") == 0);
757 ASSERT (strcmp (argv
[4], "duck") == 0);
758 ASSERT (strcmp (argv
[5], "-a") == 0);
759 ASSERT (strcmp (argv
[6], "bar") == 0);
760 ASSERT (argv
[7] == NULL
);
761 ASSERT (a_seen
== 0);
762 ASSERT (b_seen
== 0);
763 ASSERT (p_value
== NULL
);
764 ASSERT (q_value
== NULL
);
765 ASSERT (non_options_count
== 0);
766 ASSERT (unrecognized
== 0);
767 ASSERT (optind
== 1);
772 ASSERT (strcmp (argv
[0], "program") == 0);
773 ASSERT (strcmp (argv
[1], "-p") == 0);
774 ASSERT (strcmp (argv
[2], "billy") == 0);
775 ASSERT (strcmp (argv
[3], "-a") == 0);
776 ASSERT (strcmp (argv
[4], "donald") == 0);
777 ASSERT (strcmp (argv
[5], "duck") == 0);
778 ASSERT (strcmp (argv
[6], "bar") == 0);
779 ASSERT (argv
[7] == NULL
);
780 ASSERT (a_seen
== 1);
781 ASSERT (b_seen
== 0);
782 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
783 ASSERT (q_value
== NULL
);
784 ASSERT (non_options_count
== 0);
785 ASSERT (unrecognized
== 0);
786 ASSERT (optind
== 4);
791 /* Check that '--' ends the argument processing. */
792 for (start
= OPTIND_MIN
; start
<= 1; start
++)
796 const char *p_value
= NULL
;
797 const char *q_value
= NULL
;
798 int non_options_count
= 0;
799 const char *non_options
[10];
800 int unrecognized
= 0;
803 const char *argv
[20];
805 argv
[argc
++] = "program";
806 argv
[argc
++] = "donald";
808 argv
[argc
++] = "billy";
809 argv
[argc
++] = "duck";
813 argv
[argc
++] = "foo";
815 argv
[argc
++] = "johnny";
816 argv
[argc
++] = "bar";
820 getopt_loop (argc
, argv
, "abp:q:",
821 &a_seen
, &b_seen
, &p_value
, &q_value
,
822 &non_options_count
, non_options
, &unrecognized
, &output
);
825 ASSERT (strcmp (argv
[0], "program") == 0);
826 ASSERT (strcmp (argv
[1], "donald") == 0);
827 ASSERT (strcmp (argv
[2], "-p") == 0);
828 ASSERT (strcmp (argv
[3], "billy") == 0);
829 ASSERT (strcmp (argv
[4], "duck") == 0);
830 ASSERT (strcmp (argv
[5], "-a") == 0);
831 ASSERT (strcmp (argv
[6], "--") == 0);
832 ASSERT (strcmp (argv
[7], "-b") == 0);
833 ASSERT (strcmp (argv
[8], "foo") == 0);
834 ASSERT (strcmp (argv
[9], "-q") == 0);
835 ASSERT (strcmp (argv
[10], "johnny") == 0);
836 ASSERT (strcmp (argv
[11], "bar") == 0);
837 ASSERT (argv
[12] == NULL
);
838 ASSERT (a_seen
== 0);
839 ASSERT (b_seen
== 0);
840 ASSERT (p_value
== NULL
);
841 ASSERT (q_value
== NULL
);
842 ASSERT (non_options_count
== 0);
843 ASSERT (unrecognized
== 0);
844 ASSERT (optind
== 1);
849 ASSERT (strcmp (argv
[0], "program") == 0);
850 ASSERT (strcmp (argv
[1], "-p") == 0);
851 ASSERT (strcmp (argv
[2], "billy") == 0);
852 ASSERT (strcmp (argv
[3], "-a") == 0);
853 ASSERT (strcmp (argv
[4], "--") == 0);
854 ASSERT (strcmp (argv
[5], "donald") == 0);
855 ASSERT (strcmp (argv
[6], "duck") == 0);
856 ASSERT (strcmp (argv
[7], "-b") == 0);
857 ASSERT (strcmp (argv
[8], "foo") == 0);
858 ASSERT (strcmp (argv
[9], "-q") == 0);
859 ASSERT (strcmp (argv
[10], "johnny") == 0);
860 ASSERT (strcmp (argv
[11], "bar") == 0);
861 ASSERT (argv
[12] == NULL
);
862 ASSERT (a_seen
== 1);
863 ASSERT (b_seen
== 0);
864 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
865 ASSERT (q_value
== NULL
);
866 ASSERT (non_options_count
== 0);
867 ASSERT (unrecognized
== 0);
868 ASSERT (optind
== 5);
873 #if GNULIB_TEST_GETOPT_GNU
874 /* Check that the '-' flag causes non-options to be returned in order. */
875 for (start
= OPTIND_MIN
; start
<= 1; start
++)
879 const char *p_value
= NULL
;
880 const char *q_value
= NULL
;
881 int non_options_count
= 0;
882 const char *non_options
[10];
883 int unrecognized
= 0;
886 const char *argv
[10];
888 argv
[argc
++] = "program";
889 argv
[argc
++] = "donald";
891 argv
[argc
++] = "billy";
892 argv
[argc
++] = "duck";
894 argv
[argc
++] = "bar";
898 getopt_loop (argc
, argv
, "-abp:q:",
899 &a_seen
, &b_seen
, &p_value
, &q_value
,
900 &non_options_count
, non_options
, &unrecognized
, &output
);
901 ASSERT (strcmp (argv
[0], "program") == 0);
902 ASSERT (strcmp (argv
[1], "donald") == 0);
903 ASSERT (strcmp (argv
[2], "-p") == 0);
904 ASSERT (strcmp (argv
[3], "billy") == 0);
905 ASSERT (strcmp (argv
[4], "duck") == 0);
906 ASSERT (strcmp (argv
[5], "-a") == 0);
907 ASSERT (strcmp (argv
[6], "bar") == 0);
908 ASSERT (argv
[7] == NULL
);
909 ASSERT (a_seen
== 1);
910 ASSERT (b_seen
== 0);
911 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
912 ASSERT (q_value
== NULL
);
913 ASSERT (non_options_count
== 3);
914 ASSERT (strcmp (non_options
[0], "donald") == 0);
915 ASSERT (strcmp (non_options
[1], "duck") == 0);
916 ASSERT (strcmp (non_options
[2], "bar") == 0);
917 ASSERT (unrecognized
== 0);
918 ASSERT (optind
== 7);
922 /* Check that '--' ends the argument processing. */
923 for (start
= OPTIND_MIN
; start
<= 1; start
++)
927 const char *p_value
= NULL
;
928 const char *q_value
= NULL
;
929 int non_options_count
= 0;
930 const char *non_options
[10];
931 int unrecognized
= 0;
934 const char *argv
[20];
936 argv
[argc
++] = "program";
937 argv
[argc
++] = "donald";
939 argv
[argc
++] = "billy";
940 argv
[argc
++] = "duck";
944 argv
[argc
++] = "foo";
946 argv
[argc
++] = "johnny";
947 argv
[argc
++] = "bar";
951 getopt_loop (argc
, argv
, "-abp:q:",
952 &a_seen
, &b_seen
, &p_value
, &q_value
,
953 &non_options_count
, non_options
, &unrecognized
, &output
);
954 ASSERT (strcmp (argv
[0], "program") == 0);
955 ASSERT (strcmp (argv
[1], "donald") == 0);
956 ASSERT (strcmp (argv
[2], "-p") == 0);
957 ASSERT (strcmp (argv
[3], "billy") == 0);
958 ASSERT (strcmp (argv
[4], "duck") == 0);
959 ASSERT (strcmp (argv
[5], "-a") == 0);
960 ASSERT (strcmp (argv
[6], "--") == 0);
961 ASSERT (strcmp (argv
[7], "-b") == 0);
962 ASSERT (strcmp (argv
[8], "foo") == 0);
963 ASSERT (strcmp (argv
[9], "-q") == 0);
964 ASSERT (strcmp (argv
[10], "johnny") == 0);
965 ASSERT (strcmp (argv
[11], "bar") == 0);
966 ASSERT (argv
[12] == NULL
);
967 ASSERT (a_seen
== 1);
968 ASSERT (b_seen
== 0);
969 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
970 ASSERT (q_value
== NULL
);
972 if (non_options_count
== 2)
974 /* glibc behaviour. */
975 ASSERT (non_options_count
== 2);
976 ASSERT (strcmp (non_options
[0], "donald") == 0);
977 ASSERT (strcmp (non_options
[1], "duck") == 0);
978 ASSERT (unrecognized
== 0);
979 ASSERT (optind
== 7);
983 /* Another valid behaviour. */
984 ASSERT (non_options_count
== 7);
985 ASSERT (strcmp (non_options
[0], "donald") == 0);
986 ASSERT (strcmp (non_options
[1], "duck") == 0);
987 ASSERT (strcmp (non_options
[2], "-b") == 0);
988 ASSERT (strcmp (non_options
[3], "foo") == 0);
989 ASSERT (strcmp (non_options
[4], "-q") == 0);
990 ASSERT (strcmp (non_options
[5], "johnny") == 0);
991 ASSERT (strcmp (non_options
[6], "bar") == 0);
992 ASSERT (unrecognized
== 0);
993 ASSERT (optind
== 12);
997 /* Check that the '-' flag has to come first. */
998 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1002 const char *p_value
= NULL
;
1003 const char *q_value
= NULL
;
1004 int non_options_count
= 0;
1005 const char *non_options
[10];
1006 int unrecognized
= 0;
1009 const char *argv
[10];
1011 argv
[argc
++] = "program";
1012 argv
[argc
++] = "donald";
1013 argv
[argc
++] = "-p";
1014 argv
[argc
++] = "billy";
1015 argv
[argc
++] = "duck";
1016 argv
[argc
++] = "-a";
1017 argv
[argc
++] = "bar";
1021 getopt_loop (argc
, argv
, "abp:q:-",
1022 &a_seen
, &b_seen
, &p_value
, &q_value
,
1023 &non_options_count
, non_options
, &unrecognized
, &output
);
1026 ASSERT (strcmp (argv
[0], "program") == 0);
1027 ASSERT (strcmp (argv
[1], "donald") == 0);
1028 ASSERT (strcmp (argv
[2], "-p") == 0);
1029 ASSERT (strcmp (argv
[3], "billy") == 0);
1030 ASSERT (strcmp (argv
[4], "duck") == 0);
1031 ASSERT (strcmp (argv
[5], "-a") == 0);
1032 ASSERT (strcmp (argv
[6], "bar") == 0);
1033 ASSERT (argv
[7] == NULL
);
1034 ASSERT (a_seen
== 0);
1035 ASSERT (b_seen
== 0);
1036 ASSERT (p_value
== NULL
);
1037 ASSERT (q_value
== NULL
);
1038 ASSERT (non_options_count
== 0);
1039 ASSERT (unrecognized
== 0);
1040 ASSERT (optind
== 1);
1045 ASSERT (strcmp (argv
[0], "program") == 0);
1046 ASSERT (strcmp (argv
[1], "-p") == 0);
1047 ASSERT (strcmp (argv
[2], "billy") == 0);
1048 ASSERT (strcmp (argv
[3], "-a") == 0);
1049 ASSERT (strcmp (argv
[4], "donald") == 0);
1050 ASSERT (strcmp (argv
[5], "duck") == 0);
1051 ASSERT (strcmp (argv
[6], "bar") == 0);
1052 ASSERT (argv
[7] == NULL
);
1053 ASSERT (a_seen
== 1);
1054 ASSERT (b_seen
== 0);
1055 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1056 ASSERT (q_value
== NULL
);
1057 ASSERT (non_options_count
== 0);
1058 ASSERT (unrecognized
== 0);
1059 ASSERT (optind
== 4);
1064 /* Check that the '+' flag causes the first non-option to terminate the
1066 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1070 const char *p_value
= NULL
;
1071 const char *q_value
= NULL
;
1072 int non_options_count
= 0;
1073 const char *non_options
[10];
1074 int unrecognized
= 0;
1077 const char *argv
[10];
1079 argv
[argc
++] = "program";
1080 argv
[argc
++] = "donald";
1081 argv
[argc
++] = "-p";
1082 argv
[argc
++] = "billy";
1083 argv
[argc
++] = "duck";
1084 argv
[argc
++] = "-a";
1085 argv
[argc
++] = "bar";
1089 getopt_loop (argc
, argv
, "+abp:q:",
1090 &a_seen
, &b_seen
, &p_value
, &q_value
,
1091 &non_options_count
, non_options
, &unrecognized
, &output
);
1092 ASSERT (strcmp (argv
[0], "program") == 0);
1093 ASSERT (strcmp (argv
[1], "donald") == 0);
1094 ASSERT (strcmp (argv
[2], "-p") == 0);
1095 ASSERT (strcmp (argv
[3], "billy") == 0);
1096 ASSERT (strcmp (argv
[4], "duck") == 0);
1097 ASSERT (strcmp (argv
[5], "-a") == 0);
1098 ASSERT (strcmp (argv
[6], "bar") == 0);
1099 ASSERT (argv
[7] == NULL
);
1100 ASSERT (a_seen
== 0);
1101 ASSERT (b_seen
== 0);
1102 ASSERT (p_value
== NULL
);
1103 ASSERT (q_value
== NULL
);
1104 ASSERT (non_options_count
== 0);
1105 ASSERT (unrecognized
== 0);
1106 ASSERT (optind
== 1);
1109 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1113 const char *p_value
= NULL
;
1114 const char *q_value
= NULL
;
1115 int non_options_count
= 0;
1116 const char *non_options
[10];
1117 int unrecognized
= 0;
1120 const char *argv
[10];
1122 argv
[argc
++] = "program";
1123 argv
[argc
++] = "-+";
1126 getopt_loop (argc
, argv
, "+abp:q:",
1127 &a_seen
, &b_seen
, &p_value
, &q_value
,
1128 &non_options_count
, non_options
, &unrecognized
, &output
);
1129 ASSERT (a_seen
== 0);
1130 ASSERT (b_seen
== 0);
1131 ASSERT (p_value
== NULL
);
1132 ASSERT (q_value
== NULL
);
1133 ASSERT (non_options_count
== 0);
1134 ASSERT (unrecognized
== '+');
1135 ASSERT (optind
== 2);
1139 /* Check that '--' ends the argument processing. */
1140 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1144 const char *p_value
= NULL
;
1145 const char *q_value
= NULL
;
1146 int non_options_count
= 0;
1147 const char *non_options
[10];
1148 int unrecognized
= 0;
1151 const char *argv
[20];
1153 argv
[argc
++] = "program";
1154 argv
[argc
++] = "donald";
1155 argv
[argc
++] = "-p";
1156 argv
[argc
++] = "billy";
1157 argv
[argc
++] = "duck";
1158 argv
[argc
++] = "-a";
1159 argv
[argc
++] = "--";
1160 argv
[argc
++] = "-b";
1161 argv
[argc
++] = "foo";
1162 argv
[argc
++] = "-q";
1163 argv
[argc
++] = "johnny";
1164 argv
[argc
++] = "bar";
1168 getopt_loop (argc
, argv
, "+abp:q:",
1169 &a_seen
, &b_seen
, &p_value
, &q_value
,
1170 &non_options_count
, non_options
, &unrecognized
, &output
);
1171 ASSERT (strcmp (argv
[0], "program") == 0);
1172 ASSERT (strcmp (argv
[1], "donald") == 0);
1173 ASSERT (strcmp (argv
[2], "-p") == 0);
1174 ASSERT (strcmp (argv
[3], "billy") == 0);
1175 ASSERT (strcmp (argv
[4], "duck") == 0);
1176 ASSERT (strcmp (argv
[5], "-a") == 0);
1177 ASSERT (strcmp (argv
[6], "--") == 0);
1178 ASSERT (strcmp (argv
[7], "-b") == 0);
1179 ASSERT (strcmp (argv
[8], "foo") == 0);
1180 ASSERT (strcmp (argv
[9], "-q") == 0);
1181 ASSERT (strcmp (argv
[10], "johnny") == 0);
1182 ASSERT (strcmp (argv
[11], "bar") == 0);
1183 ASSERT (argv
[12] == NULL
);
1184 ASSERT (a_seen
== 0);
1185 ASSERT (b_seen
== 0);
1186 ASSERT (p_value
== NULL
);
1187 ASSERT (q_value
== NULL
);
1188 ASSERT (non_options_count
== 0);
1189 ASSERT (unrecognized
== 0);
1190 ASSERT (optind
== 1);
1193 #endif /* GNULIB_TEST_GETOPT_GNU */
1195 /* Check that the '+' flag has to come first. */
1196 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1200 const char *p_value
= NULL
;
1201 const char *q_value
= NULL
;
1202 int non_options_count
= 0;
1203 const char *non_options
[10];
1204 int unrecognized
= 0;
1207 const char *argv
[10];
1209 argv
[argc
++] = "program";
1210 argv
[argc
++] = "donald";
1211 argv
[argc
++] = "-p";
1212 argv
[argc
++] = "billy";
1213 argv
[argc
++] = "duck";
1214 argv
[argc
++] = "-a";
1215 argv
[argc
++] = "bar";
1219 getopt_loop (argc
, argv
, "abp:q:+",
1220 &a_seen
, &b_seen
, &p_value
, &q_value
,
1221 &non_options_count
, non_options
, &unrecognized
, &output
);
1224 ASSERT (strcmp (argv
[0], "program") == 0);
1225 ASSERT (strcmp (argv
[1], "donald") == 0);
1226 ASSERT (strcmp (argv
[2], "-p") == 0);
1227 ASSERT (strcmp (argv
[3], "billy") == 0);
1228 ASSERT (strcmp (argv
[4], "duck") == 0);
1229 ASSERT (strcmp (argv
[5], "-a") == 0);
1230 ASSERT (strcmp (argv
[6], "bar") == 0);
1231 ASSERT (argv
[7] == NULL
);
1232 ASSERT (a_seen
== 0);
1233 ASSERT (b_seen
== 0);
1234 ASSERT (p_value
== NULL
);
1235 ASSERT (q_value
== NULL
);
1236 ASSERT (non_options_count
== 0);
1237 ASSERT (unrecognized
== 0);
1238 ASSERT (optind
== 1);
1243 ASSERT (strcmp (argv
[0], "program") == 0);
1244 ASSERT (strcmp (argv
[1], "-p") == 0);
1245 ASSERT (strcmp (argv
[2], "billy") == 0);
1246 ASSERT (strcmp (argv
[3], "-a") == 0);
1247 ASSERT (strcmp (argv
[4], "donald") == 0);
1248 ASSERT (strcmp (argv
[5], "duck") == 0);
1249 ASSERT (strcmp (argv
[6], "bar") == 0);
1250 ASSERT (argv
[7] == NULL
);
1251 ASSERT (a_seen
== 1);
1252 ASSERT (b_seen
== 0);
1253 ASSERT (p_value
!= NULL
&& strcmp (p_value
, "billy") == 0);
1254 ASSERT (q_value
== NULL
);
1255 ASSERT (non_options_count
== 0);
1256 ASSERT (unrecognized
== 0);
1257 ASSERT (optind
== 4);
1262 #if GNULIB_TEST_GETOPT_GNU
1263 /* If GNU extensions are supported, require compliance with POSIX
1264 interpretation on leading '+' behavior.
1265 http://austingroupbugs.net/view.php?id=191 */
1266 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1270 const char *p_value
= NULL
;
1271 const char *q_value
= NULL
;
1272 int non_options_count
= 0;
1273 const char *non_options
[10];
1274 int unrecognized
= 0;
1277 const char *argv
[10];
1279 argv
[argc
++] = "program";
1280 argv
[argc
++] = "donald";
1281 argv
[argc
++] = "-p";
1282 argv
[argc
++] = "billy";
1283 argv
[argc
++] = "duck";
1284 argv
[argc
++] = "-a";
1285 argv
[argc
++] = "bar";
1289 getopt_loop (argc
, argv
, "+:abp:q:",
1290 &a_seen
, &b_seen
, &p_value
, &q_value
,
1291 &non_options_count
, non_options
, &unrecognized
, &output
);
1292 ASSERT (strcmp (argv
[0], "program") == 0);
1293 ASSERT (strcmp (argv
[1], "donald") == 0);
1294 ASSERT (strcmp (argv
[2], "-p") == 0);
1295 ASSERT (strcmp (argv
[3], "billy") == 0);
1296 ASSERT (strcmp (argv
[4], "duck") == 0);
1297 ASSERT (strcmp (argv
[5], "-a") == 0);
1298 ASSERT (strcmp (argv
[6], "bar") == 0);
1299 ASSERT (argv
[7] == NULL
);
1300 ASSERT (a_seen
== 0);
1301 ASSERT (b_seen
== 0);
1302 ASSERT (p_value
== NULL
);
1303 ASSERT (q_value
== NULL
);
1304 ASSERT (non_options_count
== 0);
1305 ASSERT (unrecognized
== 0);
1306 ASSERT (optind
== 1);
1309 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1313 const char *p_value
= NULL
;
1314 const char *q_value
= NULL
;
1315 int non_options_count
= 0;
1316 const char *non_options
[10];
1317 int unrecognized
= 0;
1320 const char *argv
[10];
1322 argv
[argc
++] = "program";
1323 argv
[argc
++] = "-p";
1326 getopt_loop (argc
, argv
, "+:abp:q:",
1327 &a_seen
, &b_seen
, &p_value
, &q_value
,
1328 &non_options_count
, non_options
, &unrecognized
, &output
);
1329 ASSERT (a_seen
== 0);
1330 ASSERT (b_seen
== 0);
1331 ASSERT (p_value
== NULL
);
1332 ASSERT (q_value
== NULL
);
1333 ASSERT (non_options_count
== 0);
1334 ASSERT (unrecognized
== 'p');
1335 ASSERT (optind
== 2);
1338 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1342 const char *p_value
= NULL
;
1343 const char *q_value
= NULL
;
1344 int non_options_count
= 0;
1345 const char *non_options
[10];
1346 int unrecognized
= 0;
1349 const char *argv
[10];
1351 argv
[argc
++] = "program";
1352 argv
[argc
++] = "-b";
1353 argv
[argc
++] = "-p";
1356 getopt_loop (argc
, argv
, "+:abp:q:",
1357 &a_seen
, &b_seen
, &p_value
, &q_value
,
1358 &non_options_count
, non_options
, &unrecognized
, &output
);
1359 ASSERT (a_seen
== 0);
1360 ASSERT (b_seen
== 1);
1361 ASSERT (p_value
== NULL
);
1362 ASSERT (q_value
== NULL
);
1363 ASSERT (non_options_count
== 0);
1364 ASSERT (unrecognized
== 'p');
1365 ASSERT (optind
== 3);
1369 /* Check that 'W' does not dump core:
1370 https://sourceware.org/bugzilla/show_bug.cgi?id=12922
1371 Technically, POSIX says the presence of ';' in the opt-string
1372 gives unspecified behavior, so we only test this when GNU compliance
1374 for (start
= OPTIND_MIN
; start
<= 1; start
++)
1377 const char *argv
[10];
1378 int pos
= ftell (stderr
);
1380 argv
[argc
++] = "program";
1381 argv
[argc
++] = "-W";
1382 argv
[argc
++] = "dummy";
1386 ASSERT (getopt (argc
, (char **) argv
, "W;") == 'W');
1387 ASSERT (ftell (stderr
) == pos
);
1388 ASSERT (optind
== 2);
1390 #endif /* GNULIB_TEST_GETOPT_GNU */