hay*_*lci 43 c arguments getopt getopt-long optional-arguments
在C中,getopt_long不会解析命令行参数参数的可选参数.
当我运行程序时,无法识别可选参数,如下面的示例运行.
$ ./respond --praise John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame
You suck !
Run Code Online (Sandbox Code Playgroud)
这是测试代码.
#include <stdio.h>
#include <getopt.h>
int main(int argc, char ** argv )
{
int getopt_ret, option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'},
{"blame", optional_argument, 0, 'b'},
{0, 0, 0, 0} };
while (1) {
getopt_ret = getopt_long( argc, argv, "p:b::",
long_options, &option_index);
if (getopt_ret == -1) break;
switch(getopt_ret)
{
case 0: break;
case 'p':
printf("Kudos to %s\n", optarg); break;
case 'b':
printf("You suck ");
if (optarg)
printf (", %s!\n", optarg);
else
printf ("!\n", optarg);
break;
case '?':
printf("Unknown option\n"); break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
hay*_*lci 85
虽然在glibc文档或getopt手册页中没有提到,但长样式命令行参数的可选参数需要'等号'(=).将可选参数与参数分隔开的空格不起作用.
使用测试代码运行的示例:
Run Code Online (Sandbox Code Playgroud)$ ./respond --praise John Kudos to John $ ./respond --praise=John Kudos to John $ ./respond --blame John You suck ! $ ./respond --blame=John You suck , John!
Bri*_*erg 15
手册页肯定没有很好地记录,但源代码有点帮助.
简单地说:你应该做类似以下的事情(虽然这可能有点过于迂腐):
if( !optarg
&& optind < argc // make sure optind is valid
&& NULL != argv[optind] // make sure it's not a null string
&& '\0' != argv[optind][0] // ... or an empty string
&& '-' != argv[optind][0] // ... or another option
) {
// update optind so the next getopt_long invocation skips argv[optind]
my_optarg = argv[optind++];
}
/* ... */
Run Code Online (Sandbox Code Playgroud)
从_getopt_internal之前的注释中:
...
如果
getopt
找到另一个选项字符,它将返回该字符,进行 更新optind
,nextchar
以便下一次调用getopt
可以使用以下选项字符或ARGV-element恢复扫描.如果没有其他选项字符,则
getopt
返回-1.然后optind
是ARGV中第一个ARGV元素的索引,它不是一个选项.(ARGV元素已经被置换,因此那些不是选项的元素现在已经存在了.)<-- a note from me: if the 3rd argument to getopt_long starts with a dash, argv will not be permuted
...
如果OPTSTRING中的char后跟冒号,则意味着它需要一个arg,因此返回同一ARGV元素中的以下文本或下一个ARGV元素的文本
optarg
.两个冒号意味着需要一个可选的arg的选项; 如果当前ARGV元素中有文本,则返回optarg
,否则optarg
设置为零....
......虽然你必须在两行之间做一些阅读.以下是您想要的:
#include <stdio.h>
#include <getopt.h>
int main(int argc, char* argv[] ) {
int getopt_ret;
int option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'}
, {"blame", optional_argument, 0, 'b'}
, {0, 0, 0, 0}
};
while( -1 != ( getopt_ret = getopt_long( argc
, argv
, "p:b::"
, long_options
, &option_index) ) ) {
const char *tmp_optarg = optarg;
switch( getopt_ret ) {
case 0: break;
case 1:
// handle non-option arguments here if you put a `-`
// at the beginning of getopt_long's 3rd argument
break;
case 'p':
printf("Kudos to %s\n", optarg); break;
case 'b':
if( !optarg
&& NULL != argv[optindex]
&& '-' != argv[optindex][0] ) {
// This is what makes it work; if `optarg` isn't set
// and argv[optindex] doesn't look like another option,
// then assume it's our parameter and overtly modify optindex
// to compensate.
//
// I'm not terribly fond of how this is done in the getopt
// API, but if you look at the man page it documents the
// existence of `optarg`, `optindex`, etc, and they're
// not marked const -- implying they expect and intend you
// to modify them if needed.
tmp_optarg = argv[optindex++];
}
printf( "You suck" );
if (tmp_optarg) {
printf (", %s!\n", tmp_optarg);
} else {
printf ("!\n");
}
break;
case '?':
printf("Unknown option\n");
break;
default:
printf( "Unknown: getopt_ret == %d\n", getopt_ret );
break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26137 次 |
最近记录: |