Mail Archives: djgpp-workers/2000/04/24/11:14:15
The following patch adds a bit to the documentation for the `getopt'
libc function to clarify how the `?' character can be used as a valid
option as well as for flagging an unexpected option. It's something
I found useful; I hope you agree it is a worthwhile patch. I made it
against the latest CVS source (d/l'ed this morning).
jtw
*** src/libc/posix/unistd/getopt.txh.old Fri Sep 10 15:19:52 1999
--- src/libc/posix/unistd/getopt.txh Mon Apr 24 10:51:48 2000
***************
*** 22,27 ****
--- 22,30 ----
If an unexpected option is found, @code{getopt} will return @code{?},
and if @code{opterr} is nonzero, will print an error message to stderr.
+ The example below shows how @code{?} can be still be used as an option
+ (e.g., to request a summary of program usage) in addition to flagging
+ an unexpected option.
The special option @code{--} indicates that no more options follow on
the command line, and cause @code{getopt} to stop looking.
***************
*** 39,45 ****
@example
int c;
opterr = 0;
! while ((c=getopt(argc, argv, "vbf:")) != -1)
@{
switch (c)
@{
--- 42,48 ----
@example
int c;
opterr = 0;
! while ((c=getopt(argc, argv, "?vbf:")) != -1)
@{
switch (c)
@{
***************
*** 53,61 ****
output_filename = optarg;
break;
case '?':
! printf("Unknown option %c\n", optopt);
! usage();
! exit(1);
@}
@}
@end example
--- 56,69 ----
output_filename = optarg;
break;
case '?':
! if (optopt == '?') @{
! usage();
! exit(0);
! @} else @{
! printf("Unknown option %c\n", optopt);
! usage();
! exit(1);
! @}
@}
@}
@end example
- Raw text -