[alt.sources] snagging command line arguments

slehar@park.bu.edu (Steve Lehar) (01/04/91)

Here's  a little set of simple  routines that  help you retrieve  your
command  line   arguments  painlessly.  They  may    not  be the  most
efficient, but    they  are easy  to   use,  and will  save   you more
programming time  (if you use  them as often  as  I do) than all their
executable time put together.   All you do is tack   them  on to  your
source code file (or #include them, if you like) and  declare  them in
your  main program and you're ready  to  go.   Say you're expecting an
integer command line  argument flagged by "-n",  for instance "-n  5".
You  can  capture that  value   and store  it in   variable int  i  as
follows...

  --------------------------------------------------------------------
 |main(argc,argv)
 |int argc;
 |char *argv[];
 |{
 |  int   intval(), is_arg(), i;
 |  float floatval();
 |  char  *stringval();
 |
 |  i = intval("-n",argc,argv);
  --------------------------------------------------------------------

If you're not sure it's gonna be there, you can check for it like this...

  --------------------------------------------------------------------
 |  if(is_arg("-n",argc,argv)) i = intval("-n",argc,argv);
 |  else i = 0;
  --------------------------------------------------------------------

Similarly, you can snag float and string values...

  --------------------------------------------------------------------
 |  x = floatval("-f",argc,argv);
 |  fp = fopen(stringval("-infile",argc,argv),"r");
 |
  --------------------------------------------------------------------

You can put any  string you  like in  the flag argument, and it always
converts the next  command line  argument following the  flag into the
appropriate data type.  Especially useful when you write programs that
use a lot of command  line  arguments.  Well, take  it or leave it.  I
find them very helpful!

/*****************************************************************/
/**	getargs							**/
/** This set of routines lets you manipulate the command line	**/
/** arguments conveniently.  is_arg(str,...) returns true if	**/
/** string str exists in the argument list.  The other functions**/
/** intval(str...),floatval(str...) and stringval(str...) return**/
/** the command line entry following the string str, converted	**/
/** to the appropriate data type.  So, for example you could use**/
/** -i to flag an input file, and access the filename using	**/
/** stringval("-i",argc,argv)					**/
/** 	Steve Lehar						**/
/*****************************************************************/


/*****************************************************************/
/**	intval							**/
/** returns the int value of the argument indexed by index+1	**/
/*****************************************************************/
intval(str,argc,argv)
char str[];
int argc;
char *argv[];
{
  int i,index=0;
  for(i=1;i<argc;i++){
    if(strcmp(argv[i],str)==0) index=i;
  }
  if(index>0) sscanf(argv[index+1],"%d",&i);
  else i=0;
  return(i);
}

/*****************************************************************/
/**	is_arg							**/
/** determines wether a command line argument exists in the	**/
/** form of str.						**/
/*****************************************************************/
is_arg(str,argc,argv)
char str[];
int argc;
char *argv[];
{
  int i,found=0;
  for(i=1;i<argc;i++){
    if(strcmp(argv[i],str)==0) found=i;
  }
  return(found);
}

/*****************************************************************/
/**	floatval						**/
/** returns the float value of the argument indexed by index+1	**/
/*****************************************************************/
float floatval(str,argc,argv)
char str[];
int argc;
char *argv[];
{
  float f;
  int i,index=0;
  for(i=1;i<argc;i++){
    if(strcmp(argv[i],str)==0) index=i;
  }
  if(index>0) sscanf(argv[index+1],"%f",&f);
  else f = 0.0;
  return(f);
}

/*****************************************************************/
/**	stringval						**/
/** returns the string value of the argument indexed by index+1	**/
/*****************************************************************/
char *stringval(str,argc,argv)
char str[];
int argc;
char *argv[];
{
  int i,index=0;
  for(i=1;i<argc;i++){
    if(strcmp(argv[i],str)==0) index=i;
  }
  if(index>0) return(argv[index+1]);
  else return("nostring");
}
--
(O)((O))(((O)))((((O))))(((((O)))))(((((O)))))((((O))))(((O)))((O))(O)
(O)((O))(((               slehar@park.bu.edu               )))((O))(O)
(O)((O))(((    Steve Lehar Boston University Boston MA     )))((O))(O)
(O)((O))(((    (617) 424-7035 (H)   (617) 353-6741 (W)     )))((O))(O)
(O)((O))(((O)))((((O))))(((((O)))))(((((O)))))((((O))))(((O)))((O))(O)