Fossil SCM

find_option() now accepts --long=VAL and --short=VAL forms, in addition to the conventional --long VAL and -short VAL. Long-form has had this feature a while (apparently) but it has not been documented AFAIK.

stephan 2012-07-14 09:14 th1-query-api
Commit aa3ea63c58f0a33e9e1e4f91173cb48e4bcbd9bd
1 file changed +29 -6
+29 -6
--- src/main.c
+++ src/main.c
@@ -789,20 +789,34 @@
789789
** Return NULL if missing.
790790
**
791791
** hasArg==0 means the option is a flag. It is either present or not.
792792
** hasArg==1 means the option has an argument. Return a pointer to the
793793
** argument.
794
+**
795
+** Note that this function REMOVES any found entry from the args list,
796
+** so calling this twice for the same var will cause NULL to be returned
797
+** after the first time.
798
+**
799
+** zLong may not be NULL but zShort may be.
800
+**
801
+** Options are accepted in these forms, depending on the value of hasArg:
802
+**
803
+** hasArg=true:
804
+** -long VALUE
805
+** -long=VALUE
806
+** -short VALUE
807
+** -short=VALUE
794808
*/
795809
const char *find_option(const char *zLong, const char *zShort, int hasArg){
796810
int i;
797
- int nLong;
811
+ int nLong, nShort;
798812
const char *zReturn = 0;
799813
assert( hasArg==0 || hasArg==1 );
800814
nLong = strlen(zLong);
815
+ nShort = zShort ? strlen(zShort) : 0;
801816
for(i=1; i<g.argc; i++){
802817
char *z;
803
- if (i+hasArg >= g.argc) break;
804818
z = g.argv[i];
805819
if( z[0]!='-' ) continue;
806820
z++;
807821
if( z[0]=='-' ){
808822
if( z[1]==0 ){
@@ -815,20 +829,29 @@
815829
if( hasArg && z[nLong]=='=' ){
816830
zReturn = &z[nLong+1];
817831
remove_from_argv(i, 1);
818832
break;
819833
}else if( z[nLong]==0 ){
834
+ if (i+hasArg >= g.argc) break;
835
+ zReturn = g.argv[i+hasArg];
836
+ remove_from_argv(i, 1+hasArg);
837
+ break;
838
+ }
839
+ }else if( strncmp(z,zShort,nShort)==0 ){
840
+ if( hasArg && z[nShort]=='=' ){
841
+ zReturn = &z[nShort+1];
842
+ remove_from_argv(i, 1);
843
+ break;
844
+ }else if( z[nShort]==0 ){
845
+ if (i+hasArg >= g.argc) break;
820846
zReturn = g.argv[i+hasArg];
821847
remove_from_argv(i, 1+hasArg);
822848
break;
823849
}
824
- }else if( fossil_strcmp(z,zShort)==0 ){
825
- zReturn = g.argv[i+hasArg];
826
- remove_from_argv(i, 1+hasArg);
827
- break;
828850
}
829851
}
852
+
830853
return zReturn;
831854
}
832855
833856
/*
834857
** Verify that there are no unprocessed command-line options. If
835858
--- src/main.c
+++ src/main.c
@@ -789,20 +789,34 @@
789 ** Return NULL if missing.
790 **
791 ** hasArg==0 means the option is a flag. It is either present or not.
792 ** hasArg==1 means the option has an argument. Return a pointer to the
793 ** argument.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
794 */
795 const char *find_option(const char *zLong, const char *zShort, int hasArg){
796 int i;
797 int nLong;
798 const char *zReturn = 0;
799 assert( hasArg==0 || hasArg==1 );
800 nLong = strlen(zLong);
 
801 for(i=1; i<g.argc; i++){
802 char *z;
803 if (i+hasArg >= g.argc) break;
804 z = g.argv[i];
805 if( z[0]!='-' ) continue;
806 z++;
807 if( z[0]=='-' ){
808 if( z[1]==0 ){
@@ -815,20 +829,29 @@
815 if( hasArg && z[nLong]=='=' ){
816 zReturn = &z[nLong+1];
817 remove_from_argv(i, 1);
818 break;
819 }else if( z[nLong]==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
820 zReturn = g.argv[i+hasArg];
821 remove_from_argv(i, 1+hasArg);
822 break;
823 }
824 }else if( fossil_strcmp(z,zShort)==0 ){
825 zReturn = g.argv[i+hasArg];
826 remove_from_argv(i, 1+hasArg);
827 break;
828 }
829 }
 
830 return zReturn;
831 }
832
833 /*
834 ** Verify that there are no unprocessed command-line options. If
835
--- src/main.c
+++ src/main.c
@@ -789,20 +789,34 @@
789 ** Return NULL if missing.
790 **
791 ** hasArg==0 means the option is a flag. It is either present or not.
792 ** hasArg==1 means the option has an argument. Return a pointer to the
793 ** argument.
794 **
795 ** Note that this function REMOVES any found entry from the args list,
796 ** so calling this twice for the same var will cause NULL to be returned
797 ** after the first time.
798 **
799 ** zLong may not be NULL but zShort may be.
800 **
801 ** Options are accepted in these forms, depending on the value of hasArg:
802 **
803 ** hasArg=true:
804 ** -long VALUE
805 ** -long=VALUE
806 ** -short VALUE
807 ** -short=VALUE
808 */
809 const char *find_option(const char *zLong, const char *zShort, int hasArg){
810 int i;
811 int nLong, nShort;
812 const char *zReturn = 0;
813 assert( hasArg==0 || hasArg==1 );
814 nLong = strlen(zLong);
815 nShort = zShort ? strlen(zShort) : 0;
816 for(i=1; i<g.argc; i++){
817 char *z;
 
818 z = g.argv[i];
819 if( z[0]!='-' ) continue;
820 z++;
821 if( z[0]=='-' ){
822 if( z[1]==0 ){
@@ -815,20 +829,29 @@
829 if( hasArg && z[nLong]=='=' ){
830 zReturn = &z[nLong+1];
831 remove_from_argv(i, 1);
832 break;
833 }else if( z[nLong]==0 ){
834 if (i+hasArg >= g.argc) break;
835 zReturn = g.argv[i+hasArg];
836 remove_from_argv(i, 1+hasArg);
837 break;
838 }
839 }else if( strncmp(z,zShort,nShort)==0 ){
840 if( hasArg && z[nShort]=='=' ){
841 zReturn = &z[nShort+1];
842 remove_from_argv(i, 1);
843 break;
844 }else if( z[nShort]==0 ){
845 if (i+hasArg >= g.argc) break;
846 zReturn = g.argv[i+hasArg];
847 remove_from_argv(i, 1+hasArg);
848 break;
849 }
 
 
 
 
850 }
851 }
852
853 return zReturn;
854 }
855
856 /*
857 ** Verify that there are no unprocessed command-line options. If
858

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button