Home| base |src.lib|task|option| OptionAdd Index

OptionAdd

Syntax
int OptionAdd(struct OptionData *opt,char *name,char type,void *data);
Header
base/option.h
Library
opt
Description

The OptionAdd function add a command line option to a table of options.

The table of command line options is given by the argument opt which is a pointer to a structure. The command line option, without the leading "-" is given by the argument name. The type of the option is given by the argument type. If the command line option takes an argument, the decoded value of that argument is written to the location given by data.

The possible type codes are:

'x'Flag
'i'Option takes an integer argument of type int.
's'Option takes an integer argument of type short.
'l'Option takes an integer argument of type long.
'f'Option takes a floating point argument of type float.
'd'Option takes a floating point argument of type double.
't'Option takes a text string as an argument.
'a'Option can takes a text string as an argument and occur multiple times.

If the option type code is 't', then data should point to a pointer of type char *. The value of the pointer should be initialized to NULL. When the command line options are parsed, memory will be allocated to store the zero-terminated text string and its location will be stored in this pointer. The memory allocated should be freed when the text string is no longer required.

If the option type code is 'a', then data should point to a pointer of type struct OptionText *. When the command line options are parsed, memory will be allocated for this structure and it will be populated. The memory should be freed when the structure is no longer required by calling the function OptionFreeText.

Returns
Returns zero on success. On error, (-1) is returned.
Errors
On error, (-1) is returned.
Example

Source Code: OptionAdd.c

/* OptionAdd.c
   ===========
   Author: R.J.Barnes
 Copyright (c) 2012 The Johns Hopkins University/Applied Physics Laboratory

This file is part of the Radar Software Toolkit (RST).

RST is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

Modifications:



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


struct OptionData opt;
int arg=0;

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

  unsigned char flag=0;
  char *text=NULL;

  OptionAdd(&opt,"flag",'x',&flag); 
  OptionAdd(&opt,"text",'t',&text);

  arg=OptionProcess(1,argc,argv,&opt,NULL);

  OptionFree(&opt);

  if (flag) fprintf(stdout,"flag set'n");
  else fprintf(stdout,"flag not set'n");
  if (text !=NULL) fprintf(stdout,"%s'n",text);
  
  return 0;
}