michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2000, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: uoptions.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2000apr17 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * This file provides a command line argument parser. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "cstring.h" michael@0: #include "uoptions.h" michael@0: michael@0: U_CAPI int U_EXPORT2 michael@0: u_parseArgs(int argc, char* argv[], michael@0: int optionCount, UOption options[]) { michael@0: char *arg; michael@0: int i=1, remaining=1; michael@0: char c, stopOptions=0; michael@0: michael@0: while(idoesOccur=1; michael@0: michael@0: if(option->hasArg!=UOPT_NO_ARG) { michael@0: /* parse the argument for the option, if any */ michael@0: if(i+1value=argv[++i]; michael@0: } else if(option->hasArg==UOPT_REQUIRES_ARG) { michael@0: /* there is no argument, but one is required: return with error */ michael@0: return -i; michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: /* process one or more short options */ michael@0: do { michael@0: /* search for the option letter */ michael@0: int j; michael@0: for(j=0; jdoesOccur=1; michael@0: michael@0: if(option->hasArg!=UOPT_NO_ARG) { michael@0: /* parse the argument for the option, if any */ michael@0: if(*arg!=0) { michael@0: /* argument following in the same argv[] */ michael@0: option->value=arg; michael@0: /* do not process the rest of this arg as option letters */ michael@0: break; michael@0: } else if(i+1value=argv[++i]; michael@0: /* this break is redundant because we know that *arg==0 */ michael@0: break; michael@0: } else if(option->hasArg==UOPT_REQUIRES_ARG) { michael@0: /* there is no argument, but one is required: return with error */ michael@0: return -i; michael@0: } michael@0: } michael@0: michael@0: /* get the next option letter */ michael@0: option=NULL; michael@0: c=*arg++; michael@0: } while(c!=0); michael@0: } michael@0: michael@0: if(option!=0 && option->optionFn!=0 && option->optionFn(option->context, option)<0) { michael@0: /* the option function was called and returned an error */ michael@0: return -i; michael@0: } michael@0: michael@0: /* go to next argv[] */ michael@0: ++i; michael@0: } else { michael@0: /* move a non-option up in argv[] */ michael@0: argv[remaining++]=arg; michael@0: ++i; michael@0: } michael@0: } michael@0: return remaining; michael@0: }