Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

test_options.c

This file shows how to use all the function of the library libmy_option.

#include <stdio.h>
#include "option.h"




/* ------------------------------------------------------------- */
/* Define your own data structure that will be used to store the */
/* option' values.                                               */
/* ------------------------------------------------------------- */

typedef struct config_ {
                         char *temperature;
                         char *gradient;
                         char *help;
                       } configuration;

configuration config;

/* ------------------------------------------------------------- */
/* Define the command line options for your process.             */
/* ------------------------------------------------------------- */

#define NUMBER_OF_OPTIONS 3

Option option[NUMBER_OF_OPTIONS] = 
       {
         { "--temperature", 1, 10, "^[0-9]+$", &config.temperature, "temperature in Celcius" },
         { "--gradient",    1, 10, "^[0-9]+$", &config.gradient,    "gradient"               },
         { "--help",        0, -1, NULL,       &config.help,        "print the help"         }
       };

/* ------------------------------------------------------------- */
/* Print option's argument (if required)                         */
/* ------------------------------------------------------------- */

void print_opt_arg (Option_set *options, char *flag)
{
  char *arg;

  if (get_argument(options, flag, &arg))
  { fprintf (stdout, "\tUndefined option (%s), this is a programming error\n", flag); }
  else
  {
    if (arg != NULL) { 
                       fprintf (stdout, "\toption (%s) activated", flag);
                       if (is_argument_required(options, flag))
                       { fprintf (stdout, ", argument is [%s]\n", arg); }
                       else { fprintf (stdout, "\n"); }
                     }
    else { fprintf (stdout, "\toption (%s) not activated\n", flag); }
  }
    
  return;
} 







int main (int argc, char *argv[])
{
  Option_set options = { option, NUMBER_OF_OPTIONS };
  char       *err, *flag, *arg;



  /* ----------------------------------------------------------- */
  /* Initilize the options                                       */
  /* ----------------------------------------------------------- */

  if (init_options (&options))
  {
    fprintf (stderr, "ERROR: function init_options() failed\n");
    return 1;
  }

  /* ----------------------------------------------------------- */
  /* Parse command line                                          */
  /* ----------------------------------------------------------- */

  if (parse_command_line (argv, argc, &options, &err, &flag, &arg))
  {
    fprintf (stderr, "ERROR: function parse_command_line() failed - %s (%s / %s)\n", err, flag, arg);
    return 1;
  }

  /* ----------------------------------------------------------- */
  /* Print result                                                */
  /* ----------------------------------------------------------- */

  fprintf (stdout, "Last ARGV index is: %d\n\n", last_argv_index(options));
  print_opt_arg (&options, "--temperature"); 
  print_opt_arg (&options, "--gradient"); 
  print_opt_arg (&options, "--help"); 
  fprintf (stdout, "\n\n");

  return 0;
}

Generated on Thu Apr 3 16:23:41 2003 for Common_C_libraries by doxygen1.3-rc1