我正在开发一个 C 程序,它使用 getopt 读取选项,然后遍历 argc 的其余部分以读取非可选参数。其中一些非可选参数是数字,其中一些是负数。常见问题,我知道,并且我理解修复只是在选项的结尾和非可选参数的开头之间放置一个“--”终止符。但是,这似乎不适用于我当前的设置。
这是读取可选和非可选参数的代码片段:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <math.h>
static const struct option longOpts[] = {
{"inputconstheight", required_argument, 0, 'c'},
{"inputheightfile", required_argument, 0, 'f'},
{"alongtrackfile", required_argument, 0, 'g'},
{"crosstrackfile", required_argument, 0, 'h'},
{"lensconstant", required_argument, 0, 'm'},
{"nonlinearoptics", required_argument, 0, 'n'},
{"verbose", required_argument, 0, 'v'},
{"rolloffset", required_argument, 0, 'x'},
{"pitchoffset", required_argument, 0, 'y'},
{"yawoffset", required_argument, 0, 'z'},
{0, 0, 0, 0}
};
int main(int argv, char **argc)
{
//skipping variable declaration for brevity
while((opt = getopt_long(argv, argc, "cef:", longOpts, &longIndex))!=-1)
{
switch(opt)
{
case 'c': //constheight
constantheightflag=1;
constantheight=atof(optarg);
break;
case 'f': //inputheightfile
constantheightflag=0;
inputheightfilename=optarg;
break;
case 'g': //alongtrackfile
inputalongtrackanglefilename=optarg;
break;
case 'h': //crosstrackfile
inputcrosstrackanglefilename=optarg;
break;
case 'm': //lensconstant
lensconstantflag=1;
lensconstant=atof(optarg);
break;
case 'n': //nonlinearopticsflag
nonlinearoptics=atoi(optarg);
break;
case 'v': //verbose
verboseflag=1;
break;
case 'x': //rolloffset
rolloffset=atof(optarg);
break;
case 'y': //pitchoffset
pitchoffset=atof(optarg);
break;
case 'z': //yawoffset
yawoffset=atof(optarg);
break;
case '?':
opterror=1;
break;
}
}
/*get non-optional arguments*/
if((opterror==1)||((argv-optind)!=14)) {
printf("Usage: geolocateplane -[cfghmnvxyz] -- outputlatfile outputlonfile imagexsize imageysize detectorxsize detectorysize fov_x fov_y lat lon alt roll pitch yaw\n");
return 1;}
else {
outputlatfilename=argc[optind+0];
outputlonfilename=argc[optind+1];
imagexsize=atoi(argc[optind+2]);
imageysize=atoi(argc[optind+3]);
detectorxsize=atof(argc[optind+4]);
detectorysize=atof(argc[optind+5]);
fov_x=atof(argc[optind+6]);
fov_y=atof(argc[optind+7]);
cameralat=atof(argc[optind+8]);
cameralon=atof(argc[optind+9]);
cameraalt=atof(argc[optind+10]);
cameraroll=atof(argc[optind+11]);
camerapitch=atof(argc[optind+12]);
camerayaw=atof(argc[optind+13]);
}
这是函数调用和输出:
XXXXXX:~/XXXX$ /XXX/geolocateplane --inputconstheight=0 --nonlinearoptics=0 --lensconstant=5.9982225412 --rolloffset=-1.0 --pitchoffset=-0.7 --yawoffset=0.0 --verbose -- /XXX/PACS.20130122.red.020756.lat.dat /XXX/PACS.20130122.red.020756.lon.dat 1024 1024 18.0 18.0 110 110 36.2927288472 -119.6177822504 19875.8 -0.3 0.7 -26.9
/XXX/geolocateplane: invalid option -- '1'
/XXX/geolocateplane: invalid option -- '1'
/XXX/geolocateplane: invalid option -- '9'
//etc...etc...
知道为什么会发生这种情况,或者解决方法是什么?