I want to put arguments to launch the program
For exemple : ./program -i inputfile -e -b
.
I have done the code bellow but I have a problem the argument needs an option for exemple it works when I put -i inputfile
but if I put -e -b
it will take the -b as an option for the -e
.
I haven't found a solution to this as it asks me to enter an option for each argument
Thank you if you can help me
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#define OPTSTR "h:b:c:d:e:f:i:l:g:o:r:x:s:Z:z"
int main(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, OPTSTR)) != EOF) {
switch(opt) {
case 'h':
printf("AIDE POUR LE PROGRAMME");
break;
case 'b':
printf("-b ENTERED");
break;
case 'c':
printf("-c ENTERED");
break;
case 'd':
printf("-d ENTERED");
break;
case 'e':
printf("-e ENTERED");
break;
case 'f':
printf("-f ENTERED");
break;
case 'i':
printf("-i ENTERED");
break;
case 'l':
printf("-l ENTERED");
break;
case 'g':
printf("-g ENTERED");
break;
case 'o':
printf("-o ENTERED");
break;
case 'r':
printf("-r ENTERED");
break;
case 'x':
printf("-x ENTERED");
break;
case 's':
printf("-s ENTERED");
break;
case 'Z':
printf("-Z ENTERED");
break;
case 'z':
printf("-z ENTERED");
break;
default:
printf("Mauvais argument entré ! \n");
break;
}
}
return 0;
}
CodePudding user response:
In your option string, a letter followed by a :
means that option expects an argument. If an option does not take an argument, don't put :
after it.
#define OPTSTR "h:b:c:d:ef:i:l:g:o:r:x:s:Z:z"
// ^-- no colon after "e"