getopt
used for parse arguments passed tomain(int argc, char**argv) function under cmdline environments.
each parameters filtered by checking if it is startwith "-" to know if it is an valid one. and the characters aside from the initial '-' are taken as option characters.
it normally returns the option charactor(maybe defined by our option string or others, just by filter rule mentioned above) and will return -1 while no more arguments.
always we call it in a loop and use a switch-case statement to parse all passed arguments we need for our programs.
to make the parse process continiously and know the current parse status, other varables are imported. whilegetopt() returned, it also updates the external variableoptind and a static variablenextchar.
The variable optind is the index of the next element to be processed inargv. The system initializes this value to 1. The caller can reset itto 1 to restart scanning of the sameargv, or when scanning a new argument vector.
just as name presented, nextchar is the next char to be scanned in the option-element in which the last option character we returned was found.
If getopt() does not recognize an option character, it prints an error message tostderr, stores the character inoptopt, and returns'?'. The calling program may prevent the error message by settingopterr to 0.
If getopt() finds an option character inargv that was not included inoptstring, or if it detects a missing option argument, it returns '?' and sets the external variableoptopt to the actual option character. If the first character (following any optional '+' or '-' describedabove) ofoptstring is a colon (':'), thengetopt() returns ':' instead of '?' to indicate a missing option argument. If an error was detected,and the first character ofoptstring is not a colon, and the external variableopterr is nonzero (which is the default),getopt() printsan error message.
optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, sogetopt() places a pointer to the following text in the sameargv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the currentargv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is returned inoptarg, otherwiseoptarg is set to zero. This is a GNU extension.a must head file needs tobe imported is "unistd.h"
here is an sample from GNU site:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
and the sample input and results also be provided
% testopt
aflag = 0, bflag = 0, cvalue = (null)
% testopt -a -b
aflag = 1, bflag = 1, cvalue = (null)
% testopt -ab
aflag = 1, bflag = 1, cvalue = (null)
% testopt -c foo
aflag = 0, bflag = 0, cvalue = foo
% testopt -cfoo
aflag = 0, bflag = 0, cvalue = foo
% testopt arg1
aflag = 0, bflag = 0, cvalue = (null)
Non-option argument arg1
% testopt -a arg1
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument arg1
% testopt -c foo arg1
aflag = 0, bflag = 0, cvalue = foo
Non-option argument arg1
% testopt -a -- -b
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -b
% testopt -a -
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -
besides getopt, other API also provided like "getopt_long" by linux man page
ref:
http://linux.die.net/man/3/getopt
http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
java port also provide:
http://www.gnu.org/software/gnuprologjava/api/gnu/getopt/Getopt.html
some other String related API always needed:
http://blog.csdn.net/eager7/article/details/8131437
http://blog.csdn.net/bg2bkk/article/details/37569555
you got it! :-D