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.
Commit
aa3ea63c58f0a33e9e1e4f91173cb48e4bcbd9bd
Parent
e30002440a9280e…
1 file changed
+29
-6
+29
-6
| --- src/main.c | ||
| +++ src/main.c | ||
| @@ -789,20 +789,34 @@ | ||
| 789 | 789 | ** Return NULL if missing. |
| 790 | 790 | ** |
| 791 | 791 | ** hasArg==0 means the option is a flag. It is either present or not. |
| 792 | 792 | ** hasArg==1 means the option has an argument. Return a pointer to the |
| 793 | 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 | |
| 794 | 808 | */ |
| 795 | 809 | const char *find_option(const char *zLong, const char *zShort, int hasArg){ |
| 796 | 810 | int i; |
| 797 | - int nLong; | |
| 811 | + int nLong, nShort; | |
| 798 | 812 | const char *zReturn = 0; |
| 799 | 813 | assert( hasArg==0 || hasArg==1 ); |
| 800 | 814 | nLong = strlen(zLong); |
| 815 | + nShort = zShort ? strlen(zShort) : 0; | |
| 801 | 816 | for(i=1; i<g.argc; i++){ |
| 802 | 817 | char *z; |
| 803 | - if (i+hasArg >= g.argc) break; | |
| 804 | 818 | z = g.argv[i]; |
| 805 | 819 | if( z[0]!='-' ) continue; |
| 806 | 820 | z++; |
| 807 | 821 | if( z[0]=='-' ){ |
| 808 | 822 | if( z[1]==0 ){ |
| @@ -815,20 +829,29 @@ | ||
| 815 | 829 | if( hasArg && z[nLong]=='=' ){ |
| 816 | 830 | zReturn = &z[nLong+1]; |
| 817 | 831 | remove_from_argv(i, 1); |
| 818 | 832 | break; |
| 819 | 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; | |
| 820 | 846 | zReturn = g.argv[i+hasArg]; |
| 821 | 847 | remove_from_argv(i, 1+hasArg); |
| 822 | 848 | break; |
| 823 | 849 | } |
| 824 | - }else if( fossil_strcmp(z,zShort)==0 ){ | |
| 825 | - zReturn = g.argv[i+hasArg]; | |
| 826 | - remove_from_argv(i, 1+hasArg); | |
| 827 | - break; | |
| 828 | 850 | } |
| 829 | 851 | } |
| 852 | + | |
| 830 | 853 | return zReturn; |
| 831 | 854 | } |
| 832 | 855 | |
| 833 | 856 | /* |
| 834 | 857 | ** Verify that there are no unprocessed command-line options. If |
| 835 | 858 |
| --- 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 |