There are many ways you can do this, you can check each last argument and when that argument is specific you just have to get the next paramenter and then skip it to not be checked. an example would be if your program does a check of the age passed in the param -i, when the program finds the -i it takes the next argument after -i that is the age and then skips the next argument in the counter to not check the old age value as argument
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv){
int contParam, idade = 0;
//corre cada paramatro passado
for(contParam = 0; contParam < argc; contParam++){
//se o parametro passado for -i ele executa isso
if(!strcmp(argv[contParam],"-i")){
//armazena na variavel local o valor passado
//que seria o paramatro + 1
idade = atoi(argv[contParam + 1]); //pega o proximo valor
contParam += 1; //checa outro parametro desconsiderando
//o proprio valor como parametro
}
}
printf("idade = %d \n",idade);
}
In the previous example if you move to the -i 10 program it will display age 10
kodo.exe -i 10
If you pass it another value before minus -i, the result will be the same because the program picks up the value after -i
kodo.exe 50 -i 10
The same is true if the value is passed after the value of -i
kodo.exe 50 -i 10 80
Of course if you pass another -i will overwrite the value in the variable, the good thing is that it allows you to pass parameters to your program without a specific order as well
kodo.exe 50 -i 10 80 -i 30
Another example now passing the name in the -n, age -i and date -d
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv){
int contParam, idade = 0;
char nome[100] = "", data[20] = "";
for(contParam = 0; contParam < argc; contParam++){
if(!strcmp(argv[contParam],"-i")){
idade = atoi(argv[contParam + 1]);
contParam += 1;
}
else if(!strcmp(argv[contParam],"-n")){
strncpy(nome,argv[contParam + 1],100);
contParam += 1;
}
else if(!strcmp(argv[contParam],"-d")){
strncpy(data,argv[contParam + 1],20);
contParam += 1;
}
}
printf("nome = %s \n",nome);
printf("idade = %d \n",idade);
printf("data = %s \n",data);
}
the command in the terminal would be
kodo.exe -d "25/06/2017" -n "kodo no kami" -i 20
the output on the terminal would look like this
nome = kodo no kami
idade = 20
data = 25/06/2017