Rajiv
Rajiv

Reputation: 565

CLI arguments in C

We are developing a program which is supposed to take in multiple inputs of the same argument. For eg.

program1 -A arg1 -A arg2 -A arg3 -B arg4 -B arg5 -B arg6

Further to complicate it, -A and -B arguments have a 1:1 relation. So arg1 is mapped to arg4 and so one.

So Is the above example the best way multiple same argument list can be provided? Or is this the accepted way?

Upvotes: 1

Views: 428

Answers (3)

Dimitri
Dimitri

Reputation: 8290

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

Upvotes: 5

Mark Wilkins
Mark Wilkins

Reputation: 41252

Without more information, it is a bit vague. My opinion, though, would be to match them up like this

cmd -pair1 argA1 argB1 -pair2 argA2 argB2 ...

or maybe something like the following. After a bit of thought, I would prefer this because it is the simplest way of matching parameters without having to type lots of parameter "types", which is not a requirement.

cmd argA1 argB1 argA2 argB2 ...

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 994471

If you're starting from scratch, you might consider something like:

program1 arg1,arg4 arg2,arg4 arg3,arg6

This pairs the arguments in the way you describe, and separates each pair by some character (a comma in this example). You may choose an appropriate separator character that works for you.

There is no requirement to use the -X option convention if you don't need to.

Upvotes: 1

Related Questions