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 10:17 th1-query-api
Commit 232008406fa462e3f12eb5bb2bc58738bca2777c
1 file changed +27 -13
+27 -13
--- src/main.c
+++ src/main.c
@@ -787,26 +787,32 @@
787787
/*
788788
** Look for a command-line option. If present, return a pointer.
789789
** Return NULL if missing.
790790
**
791791
** 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.
792
+** hasArg==1 means the option has an argument. Return a pointer to
793
+** the argument. If hasArg is 0 and the argument is found then a
794
+** pointer to an empty string is returned (to distinguish from
795
+** NULL). If hasArg==1 and the option lies at the end of the argument
796
+** list, it is treated as if it had not been found.
794797
**
795798
** 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.
799
+** so calling this twice for the same var will cause NULL to be
800
+** returned after the first time.
798801
**
799802
** zLong may not be NULL but zShort may be.
800803
**
801
-** Options are accepted in these forms, depending on the value of hasArg:
804
+** Options are accepted in these forms, depending on the value of
805
+** hasArg:
802806
**
803807
** hasArg=true:
804
-** -long VALUE
805
-** -long=VALUE
806
-** -short VALUE
807
-** -short=VALUE
808
+** -long (hasArg==0)
809
+** -long VALUE (hasArg==1)
810
+** -long=VALUE (hasArg==1)
811
+** -short (hasArg==0)
812
+** -short VALUE (hasArg==1)
813
+** -short=VALUE (hasArg==1)
808814
*/
809815
const char *find_option(const char *zLong, const char *zShort, int hasArg){
810816
int i;
811817
int nLong, nShort;
812818
const char *zReturn = 0;
@@ -829,23 +835,31 @@
829835
if( hasArg && z[nLong]=='=' ){
830836
zReturn = &z[nLong+1];
831837
remove_from_argv(i, 1);
832838
break;
833839
}else if( z[nLong]==0 ){
834
- if (i+hasArg >= g.argc) break;
835
- zReturn = g.argv[i+hasArg];
840
+ if( hasArg ){
841
+ if (i+hasArg >= g.argc) break;
842
+ zReturn = g.argv[i+hasArg];
843
+ }else{
844
+ zReturn = "";
845
+ }
836846
remove_from_argv(i, 1+hasArg);
837847
break;
838848
}
839849
}else if( strncmp(z,zShort,nShort)==0 ){
840850
if( hasArg && z[nShort]=='=' ){
841851
zReturn = &z[nShort+1];
842852
remove_from_argv(i, 1);
843853
break;
844854
}else if( z[nShort]==0 ){
845
- if (i+hasArg >= g.argc) break;
846
- zReturn = g.argv[i+hasArg];
855
+ if( hasArg ){
856
+ if (i+hasArg >= g.argc) break;
857
+ zReturn = g.argv[i+hasArg];
858
+ }else{
859
+ zReturn = "";
860
+ }
847861
remove_from_argv(i, 1+hasArg);
848862
break;
849863
}
850864
}
851865
}
852866
--- src/main.c
+++ src/main.c
@@ -787,26 +787,32 @@
787 /*
788 ** Look for a command-line option. If present, return a pointer.
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;
@@ -829,23 +835,31 @@
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
--- src/main.c
+++ src/main.c
@@ -787,26 +787,32 @@
787 /*
788 ** Look for a command-line option. If present, return a pointer.
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
793 ** the argument. If hasArg is 0 and the argument is found then a
794 ** pointer to an empty string is returned (to distinguish from
795 ** NULL). If hasArg==1 and the option lies at the end of the argument
796 ** list, it is treated as if it had not been found.
797 **
798 ** Note that this function REMOVES any found entry from the args list,
799 ** so calling this twice for the same var will cause NULL to be
800 ** returned after the first time.
801 **
802 ** zLong may not be NULL but zShort may be.
803 **
804 ** Options are accepted in these forms, depending on the value of
805 ** hasArg:
806 **
807 ** hasArg=true:
808 ** -long (hasArg==0)
809 ** -long VALUE (hasArg==1)
810 ** -long=VALUE (hasArg==1)
811 ** -short (hasArg==0)
812 ** -short VALUE (hasArg==1)
813 ** -short=VALUE (hasArg==1)
814 */
815 const char *find_option(const char *zLong, const char *zShort, int hasArg){
816 int i;
817 int nLong, nShort;
818 const char *zReturn = 0;
@@ -829,23 +835,31 @@
835 if( hasArg && z[nLong]=='=' ){
836 zReturn = &z[nLong+1];
837 remove_from_argv(i, 1);
838 break;
839 }else if( z[nLong]==0 ){
840 if( hasArg ){
841 if (i+hasArg >= g.argc) break;
842 zReturn = g.argv[i+hasArg];
843 }else{
844 zReturn = "";
845 }
846 remove_from_argv(i, 1+hasArg);
847 break;
848 }
849 }else if( strncmp(z,zShort,nShort)==0 ){
850 if( hasArg && z[nShort]=='=' ){
851 zReturn = &z[nShort+1];
852 remove_from_argv(i, 1);
853 break;
854 }else if( z[nShort]==0 ){
855 if( hasArg ){
856 if (i+hasArg >= g.argc) break;
857 zReturn = g.argv[i+hasArg];
858 }else{
859 zReturn = "";
860 }
861 remove_from_argv(i, 1+hasArg);
862 break;
863 }
864 }
865 }
866

Keyboard Shortcuts

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