Reputation: 1699
I need to check the following arguments:
./centro -n CD1 –cp 100000 –i 100000 –t 30 –s 1000 –p 11111
And they can come in any order. I have the following code:
void checkParameters (int argc, char** argv, center_data* info) {
int opt = 0;
const char* const short_options = "n:i:t:s:p:a:";
static struct option long_options[] = {
{"cp", 1, NULL, 'a'},
{0, 0 , 0, 0}
};
if(argc != 13)
error("Error en numero de argumentos \n");
while(opt != -1){
opt = getopt_long_only(argc, argv, short_options, long_options, NULL);
switch (opt){
case 'n':
strcpy(info->center_name, optarg);
break;
case 'a':
printf("el optgar %s \n", optarg);
info->cap_max = atoi(optarg);
if (!(38000 <= info->cap_max && info->cap_max <= 3800000))
error("Error en la capacidad maxima del centro. \n");
break;
case 'i':
printf("el optgar %s \n", optarg);
info->stock = atoi(optarg);
break;
case 't':
printf("el optgar %s \n", optarg);
info->response_time = atoi(optarg);
if (!(0 <= info->response_time && info->response_time <= 180))
error("Error en el tiempo de respuesta \n");
break;
case 's':
printf("el optgar %s \n", optarg);
info->supply = atoi(optarg);
if(!(0 <= info->supply && info->supply <= 10000))
error("Error en la cantidad de suministro \n");
break;
case 'p':
printf("el optgar %s \n", optarg);
info->port = atoi(optarg);
if (0 <= info->port && info->port <=1023)
error("Error: se esta utilizando un puerto bien conocido");
break;
case -1:
printf("caso -1\n");
break;
default:
printf("caso default\n");
abort();
}
}
if (!(0 <= info->stock && info->stock <= info->cap_max))
error("Error en el inventario actual de la bomba \n");
}
The thing is, it enters first the n case and then the -1 and it exits. Which makes no sense. I see no error in the use of getopt_long_only
Upvotes: 0
Views: 314
Reputation: 1436
sir you are not using minus for all the arguments
try this command:
./centro -a 100 -n 100000 -i 100000 -t 30 -s 1000 -p 11111
btw can you please let us know which character it is, i could not find any such on my keyboard.
Upvotes: 0
Reputation: 4942
Note the different length of the dashes in:
./centro -n CD1 –cp 100000 –i 100000 –t 30 –s 1000 –p 11111
The first one is the minus character. The others are longer. That's the problem.
Upvotes: 3