The C language provides the function getopt to parse command-line options. The getopt_long function is a GNU extension that parses command-line options that offer more functionality than getopt such as multi-character options parsing. You can find documentation here : http://linux.die.net/man/3/getopt_long or simply a man getopt_long.
Let me show an example. Let's say you have a program that have 3 options (-h for help message, -i for displaying an integer and -s for displaying a String). First, you must declare a struct options. This structure will contains all options that your program needs and is defined like this :
struct option {
const char *name; // the option name
int has_arg; // if your option has an argument - no_argument (equivalent to 0) if no option and required_argument otherwise (1)
int *flag; // specifies how results are returned for a long option. If flag is NULL, then getopt_long() returns val
int val; // is the value to return, or to load into the variable pointed to by flag.
};
As your program as many options, you must declare an array of struct options :
struct option options[] = {
{"help", no_argument, NULL, 'h'}, // for the help msg
{"int", 1, required_argument, 'i'}, // display an int
{"string", required_argument, NULL, 's'}, // displays a string
{NULL, 0, NULL, 0}};
And you read your options as follow :
int main(int argc, char* argv[]){
char opt;
while ((opt = getopt_long (argc, argv, "his:", options, NULL)) != -1) {
switch(opt){
case 'h': printf("Help message \n"); exit(0);
case 'i': printf("Int options = %d\n", optarg);break;
case 's': printf("String option = %s\n", optarg); break;
default: printf("Help message\n");
}
}
Don't forget to include "getopt.h"
Good Luck