Fossil SCM

Merged in double-dash-flag2 branch, which adds conventional -- handling to the vast majority of commands (the exception being those few which don't call verify_all_arguments()).

stephan 2019-10-28 19:16 trunk merge
Commit 5cca46469f24d7c862f04cff94c9ede557d26cf5e9396ded0d6eca462e4b0676
2 files changed +53 -12 +1 -1
+53 -12
--- src/main.c
+++ src/main.c
@@ -900,16 +900,28 @@
900900
g.argc = i;
901901
}
902902
903903
904904
/*
905
-** Look for a command-line option. If present, return a pointer.
906
-** Return NULL if missing.
905
+** Look for a command-line option. If present, remove it from the
906
+** argument list and return a pointer to either the flag's name (if
907
+** hasArg==0), sans leading - or --, or its value (if hasArg==1).
908
+** Return NULL if the flag is not found.
909
+**
910
+** zLong is the "long" form of the flag and zShort is the
911
+** short/abbreviated form (typically a single letter, but it may be
912
+** longer). zLong must not be NULL, but zShort may be.
907913
**
908914
** hasArg==0 means the option is a flag. It is either present or not.
909
-** hasArg==1 means the option has an argument. Return a pointer to the
910
-** argument.
915
+** hasArg==1 means the option has an argument, in which case a pointer
916
+** to the argument's value is returned. For zLong, a flag value (if
917
+** hasValue==1) may either be in the form (--flag=value) or (--flag
918
+** value). For zShort, only the latter form is accepted.
919
+**
920
+** If a standalone argument of "--" is encountered in the argument
921
+** list while searching for the given flag(s), this routine stops
922
+** searching and NULL is returned.
911923
*/
912924
const char *find_option(const char *zLong, const char *zShort, int hasArg){
913925
int i;
914926
int nLong;
915927
const char *zReturn = 0;
@@ -921,11 +933,12 @@
921933
z = g.argv[i];
922934
if( z[0]!='-' ) continue;
923935
z++;
924936
if( z[0]=='-' ){
925937
if( z[1]==0 ){
926
- remove_from_argv(i, 1);
938
+ /* Stop processing at "--" without consuming it.
939
+ verify_all_options() will consume this flag. */
927940
break;
928941
}
929942
z++;
930943
}
931944
if( strncmp(z,zLong,nLong)==0 ){
@@ -955,11 +968,17 @@
955968
int n = (int)strlen(zOption);
956969
for(i=1; i<g.argc; i++){
957970
char *z = g.argv[i];
958971
if( z[0]!='-' ) continue;
959972
z++;
960
- if( z[0]=='-' ) z++;
973
+ if( z[0]=='-' ){
974
+ if( z[1]==0 ){
975
+ /* Stop processing at "--" */
976
+ break;
977
+ }
978
+ z++;
979
+ }
961980
if( strncmp(z,zOption,n)==0 && (z[n]==0 || z[n]=='=') ) return 1;
962981
}
963982
return 0;
964983
}
965984
@@ -969,11 +988,14 @@
969988
**
970989
** Return a malloc allocated array of pointers to the arguments.
971990
**
972991
** pnUsedArgs is used to store the number of matched arguments.
973992
**
974
-** Caller is responsible to free allocated memory.
993
+** Caller is responsible for freeing allocated memory by passing the
994
+** head of the array (not each entry) to fossil_free(). (The
995
+** individual entries have the same lifetime as values returned from
996
+** find_option().)
975997
*/
976998
const char **find_repeatable_option(
977999
const char *zLong,
9781000
const char *zShort,
9791001
int *pnUsedArgs
@@ -1013,22 +1035,41 @@
10131035
10141036
/*
10151037
** Verify that there are no unprocessed command-line options. If
10161038
** Any remaining command-line argument begins with "-" print
10171039
** an error message and quit.
1040
+**
1041
+** Exception: if "--" is encountered, it is consumed from the argument
1042
+** list and this function immediately returns. The effect is to treat
1043
+** all arguments after "--" as non-flags (conventionally used to
1044
+** enable passing-in of filenames which start with a dash).
1045
+**
1046
+** This function must normally only be called one time per app
1047
+** invokation. The exception is commands which process their
1048
+** arguments, call this to confirm that there are no extraneous flags,
1049
+** then modify the arguments list for forwarding to another
1050
+** (sub)command (which itself will call this to confirm its own
1051
+** arguments).
10181052
*/
10191053
void verify_all_options(void){
10201054
int i;
10211055
for(i=1; i<g.argc; i++){
1022
- if( g.argv[i][0]=='-' && g.argv[i][1]!=0 ){
1023
- fossil_fatal(
1024
- "unrecognized command-line option, or missing argument: %s",
1025
- g.argv[i]);
1056
+ const char * arg = g.argv[i];
1057
+ if( arg[0]=='-' ){
1058
+ if( arg[1]=='-' && arg[2]==0 ){
1059
+ /* Remove "--" from the list and treat all following
1060
+ ** arguments as non-flags. */
1061
+ remove_from_argv(i, 1);
1062
+ break;
1063
+ }else if( arg[1]!=0 ){
1064
+ fossil_fatal(
1065
+ "unrecognized command-line option or missing argument: %s",
1066
+ arg);
1067
+ }
10261068
}
10271069
}
10281070
}
1029
-
10301071
10311072
/*
10321073
** This function returns a human readable version string.
10331074
*/
10341075
const char *get_version(){
10351076
--- src/main.c
+++ src/main.c
@@ -900,16 +900,28 @@
900 g.argc = i;
901 }
902
903
904 /*
905 ** Look for a command-line option. If present, return a pointer.
906 ** Return NULL if missing.
 
 
 
 
 
 
907 **
908 ** hasArg==0 means the option is a flag. It is either present or not.
909 ** hasArg==1 means the option has an argument. Return a pointer to the
910 ** argument.
 
 
 
 
 
 
911 */
912 const char *find_option(const char *zLong, const char *zShort, int hasArg){
913 int i;
914 int nLong;
915 const char *zReturn = 0;
@@ -921,11 +933,12 @@
921 z = g.argv[i];
922 if( z[0]!='-' ) continue;
923 z++;
924 if( z[0]=='-' ){
925 if( z[1]==0 ){
926 remove_from_argv(i, 1);
 
927 break;
928 }
929 z++;
930 }
931 if( strncmp(z,zLong,nLong)==0 ){
@@ -955,11 +968,17 @@
955 int n = (int)strlen(zOption);
956 for(i=1; i<g.argc; i++){
957 char *z = g.argv[i];
958 if( z[0]!='-' ) continue;
959 z++;
960 if( z[0]=='-' ) z++;
 
 
 
 
 
 
961 if( strncmp(z,zOption,n)==0 && (z[n]==0 || z[n]=='=') ) return 1;
962 }
963 return 0;
964 }
965
@@ -969,11 +988,14 @@
969 **
970 ** Return a malloc allocated array of pointers to the arguments.
971 **
972 ** pnUsedArgs is used to store the number of matched arguments.
973 **
974 ** Caller is responsible to free allocated memory.
 
 
 
975 */
976 const char **find_repeatable_option(
977 const char *zLong,
978 const char *zShort,
979 int *pnUsedArgs
@@ -1013,22 +1035,41 @@
1013
1014 /*
1015 ** Verify that there are no unprocessed command-line options. If
1016 ** Any remaining command-line argument begins with "-" print
1017 ** an error message and quit.
 
 
 
 
 
 
 
 
 
 
 
 
1018 */
1019 void verify_all_options(void){
1020 int i;
1021 for(i=1; i<g.argc; i++){
1022 if( g.argv[i][0]=='-' && g.argv[i][1]!=0 ){
1023 fossil_fatal(
1024 "unrecognized command-line option, or missing argument: %s",
1025 g.argv[i]);
 
 
 
 
 
 
 
 
1026 }
1027 }
1028 }
1029
1030
1031 /*
1032 ** This function returns a human readable version string.
1033 */
1034 const char *get_version(){
1035
--- src/main.c
+++ src/main.c
@@ -900,16 +900,28 @@
900 g.argc = i;
901 }
902
903
904 /*
905 ** Look for a command-line option. If present, remove it from the
906 ** argument list and return a pointer to either the flag's name (if
907 ** hasArg==0), sans leading - or --, or its value (if hasArg==1).
908 ** Return NULL if the flag is not found.
909 **
910 ** zLong is the "long" form of the flag and zShort is the
911 ** short/abbreviated form (typically a single letter, but it may be
912 ** longer). zLong must not be NULL, but zShort may be.
913 **
914 ** hasArg==0 means the option is a flag. It is either present or not.
915 ** hasArg==1 means the option has an argument, in which case a pointer
916 ** to the argument's value is returned. For zLong, a flag value (if
917 ** hasValue==1) may either be in the form (--flag=value) or (--flag
918 ** value). For zShort, only the latter form is accepted.
919 **
920 ** If a standalone argument of "--" is encountered in the argument
921 ** list while searching for the given flag(s), this routine stops
922 ** searching and NULL is returned.
923 */
924 const char *find_option(const char *zLong, const char *zShort, int hasArg){
925 int i;
926 int nLong;
927 const char *zReturn = 0;
@@ -921,11 +933,12 @@
933 z = g.argv[i];
934 if( z[0]!='-' ) continue;
935 z++;
936 if( z[0]=='-' ){
937 if( z[1]==0 ){
938 /* Stop processing at "--" without consuming it.
939 verify_all_options() will consume this flag. */
940 break;
941 }
942 z++;
943 }
944 if( strncmp(z,zLong,nLong)==0 ){
@@ -955,11 +968,17 @@
968 int n = (int)strlen(zOption);
969 for(i=1; i<g.argc; i++){
970 char *z = g.argv[i];
971 if( z[0]!='-' ) continue;
972 z++;
973 if( z[0]=='-' ){
974 if( z[1]==0 ){
975 /* Stop processing at "--" */
976 break;
977 }
978 z++;
979 }
980 if( strncmp(z,zOption,n)==0 && (z[n]==0 || z[n]=='=') ) return 1;
981 }
982 return 0;
983 }
984
@@ -969,11 +988,14 @@
988 **
989 ** Return a malloc allocated array of pointers to the arguments.
990 **
991 ** pnUsedArgs is used to store the number of matched arguments.
992 **
993 ** Caller is responsible for freeing allocated memory by passing the
994 ** head of the array (not each entry) to fossil_free(). (The
995 ** individual entries have the same lifetime as values returned from
996 ** find_option().)
997 */
998 const char **find_repeatable_option(
999 const char *zLong,
1000 const char *zShort,
1001 int *pnUsedArgs
@@ -1013,22 +1035,41 @@
1035
1036 /*
1037 ** Verify that there are no unprocessed command-line options. If
1038 ** Any remaining command-line argument begins with "-" print
1039 ** an error message and quit.
1040 **
1041 ** Exception: if "--" is encountered, it is consumed from the argument
1042 ** list and this function immediately returns. The effect is to treat
1043 ** all arguments after "--" as non-flags (conventionally used to
1044 ** enable passing-in of filenames which start with a dash).
1045 **
1046 ** This function must normally only be called one time per app
1047 ** invokation. The exception is commands which process their
1048 ** arguments, call this to confirm that there are no extraneous flags,
1049 ** then modify the arguments list for forwarding to another
1050 ** (sub)command (which itself will call this to confirm its own
1051 ** arguments).
1052 */
1053 void verify_all_options(void){
1054 int i;
1055 for(i=1; i<g.argc; i++){
1056 const char * arg = g.argv[i];
1057 if( arg[0]=='-' ){
1058 if( arg[1]=='-' && arg[2]==0 ){
1059 /* Remove "--" from the list and treat all following
1060 ** arguments as non-flags. */
1061 remove_from_argv(i, 1);
1062 break;
1063 }else if( arg[1]!=0 ){
1064 fossil_fatal(
1065 "unrecognized command-line option or missing argument: %s",
1066 arg);
1067 }
1068 }
1069 }
1070 }
 
1071
1072 /*
1073 ** This function returns a human readable version string.
1074 */
1075 const char *get_version(){
1076
--- src/unversioned.c
+++ src/unversioned.c
@@ -291,12 +291,12 @@
291291
const char *zAs;
292292
Blob file;
293293
int i;
294294
295295
zAs = find_option("as",0,1);
296
- if( zAs && g.argc!=4 ) usage("add DISKFILE --as UVFILE");
297296
verify_all_options();
297
+ if( zAs && g.argc!=4 ) usage("add DISKFILE --as UVFILE");
298298
db_begin_transaction();
299299
content_rcvid_init("#!fossil unversioned add");
300300
for(i=3; i<g.argc; i++){
301301
zIn = zAs ? zAs : g.argv[i];
302302
if( zIn[0]==0 ){
303303
--- src/unversioned.c
+++ src/unversioned.c
@@ -291,12 +291,12 @@
291 const char *zAs;
292 Blob file;
293 int i;
294
295 zAs = find_option("as",0,1);
296 if( zAs && g.argc!=4 ) usage("add DISKFILE --as UVFILE");
297 verify_all_options();
 
298 db_begin_transaction();
299 content_rcvid_init("#!fossil unversioned add");
300 for(i=3; i<g.argc; i++){
301 zIn = zAs ? zAs : g.argv[i];
302 if( zIn[0]==0 ){
303
--- src/unversioned.c
+++ src/unversioned.c
@@ -291,12 +291,12 @@
291 const char *zAs;
292 Blob file;
293 int i;
294
295 zAs = find_option("as",0,1);
 
296 verify_all_options();
297 if( zAs && g.argc!=4 ) usage("add DISKFILE --as UVFILE");
298 db_begin_transaction();
299 content_rcvid_init("#!fossil unversioned add");
300 for(i=3; i<g.argc; i++){
301 zIn = zAs ? zAs : g.argv[i];
302 if( zIn[0]==0 ){
303

Keyboard Shortcuts

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