785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
|
/*
** Look for a command-line option. If present, return a pointer.
** Return NULL if missing.
**
** hasArg==0 means the option is a flag. It is either present or not.
** hasArg==1 means the option has an argument. Return a pointer to the
** argument.
**
** Note that this function REMOVES any found entry from the args list,
** so calling this twice for the same var will cause NULL to be returned
** after the first time.
**
** zLong may not be NULL but zShort may be.
**
** Options are accepted in these forms, depending on the value of hasArg:
**
** hasArg=true:
** -long VALUE
** -long=VALUE
** -short VALUE
** -short=VALUE
*/
const char *find_option(const char *zLong, const char *zShort, int hasArg){
int i;
int nLong, nShort;
const char *zReturn = 0;
assert( hasArg==0 || hasArg==1 );
nLong = strlen(zLong);
|
|
|
>
>
>
|
|
|
>
>
|
|
>
|
|
|
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
|
/*
** Look for a command-line option. If present, return a pointer.
** Return NULL if missing.
**
** hasArg==0 means the option is a flag. It is either present or not.
** hasArg==1 means the option has an argument. Return a pointer to
** the argument. If hasArg is 0 and the argument is found then a
** pointer to an empty string is returned (to distinguish from
** NULL). If hasArg==1 and the option lies at the end of the argument
** list, it is treated as if it had not been found.
**
** Note that this function REMOVES any found entry from the args list,
** so calling this twice for the same var will cause NULL to be
** returned after the first time.
**
** zLong may not be NULL but zShort may be.
**
** Options are accepted in these forms, depending on the value of
** hasArg:
**
** hasArg=true:
** -long (hasArg==0)
** -long VALUE (hasArg==1)
** -long=VALUE (hasArg==1)
** -short (hasArg==0)
** -short VALUE (hasArg==1)
** -short=VALUE (hasArg==1)
*/
const char *find_option(const char *zLong, const char *zShort, int hasArg){
int i;
int nLong, nShort;
const char *zReturn = 0;
assert( hasArg==0 || hasArg==1 );
nLong = strlen(zLong);
|
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
|
}
if( strncmp(z,zLong,nLong)==0 ){
if( hasArg && z[nLong]=='=' ){
zReturn = &z[nLong+1];
remove_from_argv(i, 1);
break;
}else if( z[nLong]==0 ){
if (i+hasArg >= g.argc) break;
zReturn = g.argv[i+hasArg];
remove_from_argv(i, 1+hasArg);
break;
}
}else if( strncmp(z,zShort,nShort)==0 ){
if( hasArg && z[nShort]=='=' ){
zReturn = &z[nShort+1];
remove_from_argv(i, 1);
break;
}else if( z[nShort]==0 ){
if (i+hasArg >= g.argc) break;
zReturn = g.argv[i+hasArg];
remove_from_argv(i, 1+hasArg);
break;
}
}
}
return zReturn;
|
>
|
|
>
>
>
>
|
|
>
>
>
|
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
|
}
if( strncmp(z,zLong,nLong)==0 ){
if( hasArg && z[nLong]=='=' ){
zReturn = &z[nLong+1];
remove_from_argv(i, 1);
break;
}else if( z[nLong]==0 ){
if( hasArg ){
if (i+hasArg >= g.argc) break;
zReturn = g.argv[i+hasArg];
}else{
zReturn = "";
}
remove_from_argv(i, 1+hasArg);
break;
}
}else if( strncmp(z,zShort,nShort)==0 ){
if( hasArg && z[nShort]=='=' ){
zReturn = &z[nShort+1];
remove_from_argv(i, 1);
break;
}else if( z[nShort]==0 ){
if( hasArg ){
if (i+hasArg >= g.argc) break;
zReturn = g.argv[i+hasArg];
}else{
zReturn = "";
}
remove_from_argv(i, 1+hasArg);
break;
}
}
}
return zReturn;
|