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

OptionProcess

Syntax
int OptionProcess(int offset,int argc,char *argv[], struct OptionData *opt,int (*opterr)(char *));
Header
base/option.h
Library
opt
Description

The OptionProcess function decoded a sequence of command line options from the command line arguments.

The argument offset is the offset into the array of command line arguments at which to start processing. The number of arguments is given by argc and the array of NULL terminated strings pointed to by argv contains the arguments. The table of options to be passed is pointed to by opt. The final argument is a pointer to a user-defined function of the form:

int opterr(char *txt);

This function is called in the event that an error is detected in parsing the command line options. This function is passed the argument txt which is the text of the argument that caused the error. The function should return with a zero if the error can be ignored or a non-zero number to halt further processing of the command line.

If there is no user-defined error function, then the final argument should be set to NULL and errors will be ignored.

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

Source Code: OptionProcess.c

/* OptionProcess.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 opterr(char *txt) {
  fprintf(stderr,"Could not recognize option:%s'n",txt);
  return 0;
}

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

  unsigned char flag=0;
  int ival=0;
  float fval=0.0;
  char *text=NULL;


  OptionAdd(&opt,"flag",'x',&flag); 
  OptionAdd(&opt,"ival",'i',&ival);
  OptionAdd(&opt,"fval",'f',&fval);
  OptionAdd(&opt,"text",'t',&text);
  

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

  OptionFree(&opt);

  if (flag) fprintf(stdout,"flag set'n");
  else fprintf(stdout,"flag not set'n");
  fprintf(stdout,"ival=%d'n",ival);
  fprintf(stdout,"fval=%g'n",fval);
  if (text !=NULL) fprintf(stdout,"text=%s'n",text);

  if (arg !=argc) fprintf(stdout,"final argument:%s'n",argv[arg]);

 
  return 0;
}