Wednesday, September 15, 2010

Processing input arguments in C program

Parsing Sort options:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void usage (const char *programName)
{
  printf ("Usage: %s [-kldeh]\n"
          "          -k <val_k>   Value for the the option -k\n"
          "          -l <val_l>   Value for the the option -l\n"
          "          -d           Does not have any value\n"
          "          -e           Does not have any value\n"
          "          -h           Print this message\n\n",
          programName);

  exit (EXIT_SUCCESS);
}

int main (int argc, char **argv)
{
  char *val_k = NULL;
  int index = 0;
  int c = 0;
  
  if (1 == argc)
  {
    printf ("This tool needs at lease one input arguments.\n");
    usage (argv[0]);
  }

  /* Check the input arguments/options */
  while (-1 != (c = getopt (argc, argv, "k:l:deh")))
  {
    switch (c)
    {
      case 'k':
        /* Assign the optarg pointer to a local variable for further use. */
        val_k = optarg;
        printf ("value for the option [k] is \"%s\"\n", val_k);
        break;
      case 'l':
        /* We can use the variable optarg directly too. */
        printf ("value for the option [l] is \"%s\"\n", optarg);
        break;
      case 'd':
        printf ("option [d] is given.\n");
        break;
      case 'e':
        printf ("option [e] is given.\n");
        break;
      case 'h':
        usage (argv[0]);
      case '?':
        if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\n",
                   optopt);
        usage (argv[0]);
      default:
        abort ();
    }
  }

  for (index = optind; index < argc; index++)
  {
    printf ("Extra arguments [%d] --> %s\n", index, argv[index]);
  }

  return 0;
}


Parsing Long options:

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

static struct option options[] = {
  /* These options set a flag.   */
  {"option_k", 1, 0, 'k'},
  {"option_l", 1, 0, 'l'},
  {"option_d", 0, 0, 'd'},
  {"option_e", 0, 0, 'e'},
  {"help", 0, 0, 'h'},
  {0, 0, 0, 0}
};

void usage (const char *programName)
{
  printf ("Usage: %s [-kldeh]\n"
          "          -k or --option_k <val_k>   Value for the the option -k\n"
          "          -l or --option_l <val_l>   Value for the the option -l\n"
          "          -d or --option_d           Does not have any value\n"
          "          -e or --option_e           Does not have any value\n"
          "          -h or --help               Print this message\n\n",
          programName);

  exit (EXIT_SUCCESS);
}

int main (int argc, char **argv)
{
  char *val_k = NULL;
  int index = 0;
  int c = 0;
  int option_index = 0;

  if (1 == argc)
  {
    printf ("This tool needs at lease one input arguments.\n");
    usage (argv[0]);
  }

  /* Check the input arguments/options */
  while (-1 != (c = getopt_long (argc, argv, "k:l:deh",
                                 options, &option_index)))
  {
    switch (c)
    {
      case 'k':
        /* Assign the optarg pointer to a local variable for further use. */
        val_k = optarg;
        printf ("value for the option [k] is \"%s\"\n", val_k);
        break;
      case 'l':
        /* We can use the variable optarg directly too. */
        printf ("value for the option [l] is \"%s\"\n", optarg);
        break;
      case 'd':
        printf ("option [d] is given.\n");
        break;
      case 'e':
        printf ("option [e] is given.\n");
        break;
      case 'h':
        usage (argv[0]);
      case '?':
        if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\n",
                   optopt);
        usage (argv[0]);
      default:
        abort ();
    }
  }

  for (index = optind; index < argc; index++)
  {
    printf ("Extra arguments [%d] --> %s\n", index, argv[index]);
  }

  return 0;
}

Ref: http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_22.html

awk - One of the best tools in linux

I was searching for a tool/command in linux to remove all the duplicate lines from a file without sort the lines. And i came across this link (see the link below)...Its really amazing. We can do lots and lots of things using awk. Its one of the mostpowerful tools for file/string processing. I hope it will be useful for most of the software developers and testers. One more good thing is that awk is available for windows too.

Link: http://kaneda.bohater.net/faq/awk1line.txt.
The latest version of this file is here.

Processing input arguments in bash shell scripting

 
# Function to print the usage of this script
Usage ()
{
    cat <<EOF
Usage: `basename $0` options [-kld]
          -k <val_k>   Value for the the option -k
          -l <val_l>   Value for the the option -l
          -d           Does not have any value
          -e           Does not have any value
          -h           Print this message
EOF
exit $E_OPTERROR
}
 
# Check for input arguments. If no arguments given, print the usage.
if [ $# -eq 0 ]; then
    Usage
fi
 
while getopts ":k:l:de" Option
do
    case $Option in
        k ) echo "value for the option [k] is \"$OPTARG\""
            ;;
        l ) echo "value for the option [l] is \"$OPTARG\""
            ;;
        d ) echo "option [d] is given."
            ;;
        e ) echo "option [e] is given."
            ;;
        h ) Usage
            ;;
        * ) echo "Invalid input option"
            Usage
            ;;
    esac
done

# To get the arguments other than the option given in the usage
shift $(($OPTIND - 1))
 
for i in $*
do
    echo "Extra inputs given -> \"$i\""
done

exit 0