C: getopt_long_only Example: Accessing command line arguments

getopt_long is useful to work with the command line arguments. But while working with getopt_long, — is used for long options and – for short options. getopt_long_only accepts both — and – for long options

  1. rectangle -a -l 12 -b 34: will calculate the area of the rectangle
  2. square -p -l 12 -b 34: will calculate the perimeter of the rectangle
  3. rectangle -a -p -l 12 -b 34: will calculate the area and perimeter of the rectangle
  4. rectangle –area –length 12 –breadth 34: will calculate the area of the rectangle
  5. square -perimeter –length 12 –breadth 34: will calculate the perimeter of the rectangle
  6. rectangle -area -perimeter –length 12 –breadth 34: will calculate the area and perimeter of the rectangle

The program is much like getopt_long.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

/** Program to calculate the area and perimeter of 
 * a rectangle using command line arguments
 */
void print_usage() {
    printf("Usage: rectangle [ap] -l num -b num\n");
}

int main(int argc, char *argv[]) {
    int opt= 0;
    int area = -1, perimeter = -1, breadth = -1, length =-1;

    //Specifying the expected options
    //The two options l and b expect numbers as argument
    static struct option long_options[] = {
        {"area",      no_argument,       0,  'a' },
        {"perimeter", no_argument,       0,  'p' },
        {"length",    required_argument, 0,  'l' },
        {"breadth",   required_argument, 0,  'b' },
        {0,           0,                 0,  0   }
    };

    int long_index =0;
    while ((opt = getopt_long_only(argc, argv,"", 
                   long_options, &long_index )) != -1) {
        switch (opt) {
             case 'a' : area = 0;
                 break;
             case 'p' : perimeter = 0;
                 break;
             case 'l' : length = atoi(optarg); 
                 break;
             case 'b' : breadth = atoi(optarg);
                 break;
             default: print_usage(); 
                 exit(EXIT_FAILURE);
        }
    }
    if (length == -1 || breadth ==-1) {
        print_usage();
        exit(EXIT_FAILURE);
    }

    // Calculate the area
    if (area == 0) {
        area = length * breadth;
        printf("Area: %d\n",area);
    }

    // Calculate the perimeter
    if (perimeter == 0) {
        perimeter = 2 * (length + breadth);
        printf("Perimeter: %d\n",perimeter);
    }
    return 0;
}
Advertisements

C: getopt_long example: Accessing command line arguments

A command can have both long and short options. getopt is useful only for short options, that are nothing but options of one char (character) long. To support both short and long options like

  1. rectangle -a -l 12 -b 34: will calculate the area of the rectangle
  2. square -p -l 12 -b 34: will calculate the perimeter of the rectangle
  3. rectangle -a -p -l 12 -b 34: will calculate the area and perimeter of the rectangle
  4. rectangle –area –length 12 –breadth 34: will calculate the area of the rectangle
  5. square –perimeter –length 12 –breadth 34: will calculate the perimeter of the rectangle
  6. rectangle –area –perimeter –length 12 –breadth 34: will calculate the area and perimeter of the rectangle

In the following program, the use of getopt_long is shown

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

/** Program to calculate the area and perimeter of 
 * a rectangle using command line arguments
 */
void print_usage() {
    printf("Usage: rectangle [ap] -l num -b num\n");
}

int main(int argc, char *argv[]) {
    int opt= 0;
    int area = -1, perimeter = -1, breadth = -1, length =-1;

    //Specifying the expected options
    //The two options l and b expect numbers as argument
    static struct option long_options[] = {
        {"area",      no_argument,       0,  'a' },
        {"perimeter", no_argument,       0,  'p' },
        {"length",    required_argument, 0,  'l' },
        {"breadth",   required_argument, 0,  'b' },
        {0,           0,                 0,  0   }
    };

    int long_index =0;
    while ((opt = getopt_long(argc, argv,"apl:b:", 
                   long_options, &long_index )) != -1) {
        switch (opt) {
             case 'a' : area = 0;
                 break;
             case 'p' : perimeter = 0;
                 break;
             case 'l' : length = atoi(optarg); 
                 break;
             case 'b' : breadth = atoi(optarg);
                 break;
             default: print_usage(); 
                 exit(EXIT_FAILURE);
        }
    }
    if (length == -1 || breadth ==-1) {
        print_usage();
        exit(EXIT_FAILURE);
    }

    // Calculate the area
    if (area == 0) {
        area = length * breadth;
        printf("Area: %d\n",area);
    }

    // Calculate the perimeter
    if (perimeter == 0) {
        perimeter = 2 * (length + breadth);
        printf("Perimeter: %d\n",perimeter);
    }
    return 0;
}

C:Working with command line arguments

Almost all of the commands in Linux/Unix have options. An option for a commmand is a mechanism by which you provide additional parameters to the command to change its behavior. Take for example, the command ls is used to list the files in a directory. But to obtain a detailed listing of the files, the option -l is used. Similarly the -a option with ls allows to see all the hidden files (file names starting with .a). Commands with option removes the need for creating multiple commands to achieve a purpose.

To access the command line parameters, make sure that the main function() looks something like this

int main(int argc, char *argv[]) {
}

Now you can write a program which access every parameter that you pass using argv[0], argv[1],…. And the number of command line arguments passed by the user can be obtained from argc

Working with command line arguments in this manner is tedious. Linux/Unix provides the following functions to easily work with the command line arguments

  1. getopt()
  2. getopt_long()
  3. getopt_long_only()

Before delving deep into this topic, let’s take a look what is meant by long arguments. Most of the options in a command have both a long and a short form. For example, to list all the hidden files, one can write ls -a or ls –all. Here -a is a short argument and –a is a long argument. As mentioned earlier, some options may not have a long option. -l option of ls doesn’t have a long option. With this in mind, let’s continue to look at the various functions

  1. getopt()
  2. getopt_long()
  3. getopt_long_only()

C: getopt Example: Accessing command line arguments

The simplest way to work with command line arguments is to use the getopt() function. To understand more about it, first let’s see a command which can calculate the area and perimeter of a rectangle

  1. rectangle a -l 12 -b 34: will calculate the area of the rectangle
  2. square p -l 12 -b 34: will calculate the perimeter of the rectangle
  3. rectangle ap -l 12 -b 34: will calculate the area and perimeter of the rectangle

As we can see, some options take arguments and some do not. Here a and p do not take any argument. But -l and -b take the arguments (number) for length and breadth respectively.

So to distinguish them, getopt provides a mechanism. All the options that require argument will be preceded by a : (colon).

The following program shows this

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

/** Program to calculate the area and perimeter of 
 * a rectangle using command line arguments
 */
void print_usage() {
    printf("Usage: rectangle [ap] -l num -b num\n");
}

int main(int argc, char *argv[]) {
    int option = 0;
    int area = -1, perimeter = -1, breadth = -1, length =-1;

    //Specifying the expected options
    //The two options l and b expect numbers as argument
    while ((option = getopt(argc, argv,"apl:b:")) != -1) {
        switch (option) {
             case 'a' : area = 0;
                 break;
             case 'p' : perimeter = 0;
                 break;
             case 'l' : length = atoi(optarg); 
                 break;
             case 'b' : breadth = atoi(optarg);
                 break;
             default: print_usage(); 
                 exit(EXIT_FAILURE);
        }
    }
    if (length == -1 || breadth ==-1) {
        print_usage();
        exit(EXIT_FAILURE);
    }

    // Calculate the area
    if (area == 0) {
        area = length * breadth;
        printf("Area: %d\n",area);
    }

    // Calculate the perimeter
    if (perimeter == 0) {
        perimeter = 2 * (length + breadth);
        printf("Perimeter: %d\n",perimeter);
    }
    return 0;
}