There are many ways to do this, you can check each past argument and when a given argument is that specific just take the next parameter and then skip it to not be checked. an example would be if your program checks the age passed in the -i parameter, when the program finds the -i it takes the next argument after -i which is the age and then skips the next argument in the counter not to check the age value passed 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 pass to the program -i 10 it will display the age 10
kodo.exe -i 10
if you pass another value to it before the 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 that this allows you to pass parameters to your program without a specific order too
kodo.exe 50 -i 10 80 -i 30
another example now passing the name in the -n parameter, age in -i and date in -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 on the terminal would be
kodo.exe -d "25/06/2017" -n "kodo no kami" -i 20
the exit in the terminal would look like this
nome = kodo no kami
idade = 20
data = 25/06/2017
Take a look here and see if it solves.
– gato