4/15/2008

getopt

/*
*@ file : getopt
*@ brief : 分析命令行参数
*/
getopt(分析命令行参数)
------------------------------
C
-------------------------------
相关函数
表头文件
#include
定义函数
int getopt(int argc,char * const argv[ ],const char * optstring);
函数说明
getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
返回值
如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
范例
#include#includeint main(int argc,char **argv){int ch;opterr = 0;while((ch = getopt(argc,argv,”a:bcde”))!= -1)switch(ch){case ‘a’:printf(“option a:’%s’\n”,optarg);break;case ‘b’:printf(“option b :b\n”);break;default:printf(“other option :%c\n”,ch);}printf(“optopt +%c\n”,optopt);}
执行
$./getopt –boption b:b$./getopt –cother option:c$./getopt –aother option :?$./getopt –a12345option a:’12345’

------------------------------
Perl
-------------------------------
Getopt::Std - Process Single-Character Options with Option Clusteringuse Getopt::Std;
getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
getopts('oif:'); # -o & -i are boolean flags, -f takes an argument.
# Sets opt_* as a side effect.
The getopt() and getopts() functions give your program simple mechanisms for processing single-character options. These options can be clustered (for example, -bdLc might be interpreted as four single-character options), and you can specify individual options that require an accompanying argument. When you invoke getopt() or getopts(), you pass along information about the kinds of options your program expects. These functions then analyze @ARGV, extract information about the options, and return this information to your program in a set of variables. The processing of @ARGV stops when an argument without a leading "-" is encountered, if that argument is not associated with a preceding option. Otherwise, @ARGV is processed to its end and left empty.
For each option in your program's invocation, both getopt() and getopts() define a variable $opt_x where x is the option name. If the option takes an argument, then the argument is read and assigned to $opt_x as its value; otherwise, a value of 1 is assigned to the variable.
Invoke getopt() with one argument, which should contain all options that require a following argument. For example:getopt('dV');
If your program is then invoked as:myscr -bfd January -V 10.4
then these variables will be set in the program:$opt_b = 1;
$opt_f = 1;
$opt_d = "January";
$opt_V = 10.4;
Space between an option and its following argument is unnecessary. The previous command line could have been given this way:myscr -bfdJanuary -V10.4
In general, your program can be invoked with options given in any order. All options not "declared" in the invocation of getopt() are assumed to be without accompanying argument.
Where getopt() allows any single-character option, getopts() allows only those options you declare explicitly. For example, this invocation:getopts('a:bc:');
legitimizes only the options -a, -b, and -c. The colon following the a and c means that these two options require an accompanying argument; b is not allowed to have an argument. Accordingly, here are some ways to invoke the program:myscr -abc # WRONG unless bc is really the argument to -a
myscr -a -bc # WRONG, with same qualification
myscr -a foo -bc bar # $opt_a = "foo"; $opt_b = 1; $opt_c = "bar"
myscr -bafoo -cbar # same as previous
getopts() returns false if it encounters errors during option processing. However, it continues to process arguments and assign values as best it can to $opt_x variables. You should always check for errors before assuming that the variables hold meaningful values.
getopt() does not return a meaningful value.
Remember that both getopt() and getopts() halt argument processing upon reading an argument (without leading "-") where none was called for. This is not considered an error.

没有评论: