Fossil

Check-in [aa3ea63c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment: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.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | th1-query-api
Files: files | file ages | folders
SHA1: aa3ea63c58f0a33e9e1e4f91173cb48e4bcbd9bd
User & Date: stephan 2012-07-14 09:14:37
Context
2012-07-14
10:17
Minor improvements to the previous find_option() commit. check-in: 23200840 user: stephan tags: th1-query-api
09:14
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. check-in: aa3ea63c user: stephan tags: th1-query-api
02:13
Added th1 query_bind functions. check-in: e3000244 user: stephan tags: th1-query-api
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/main.c.

787
788
789
790
791
792
793














794
795
796
797
798
799
800

801
802
803
804
805
806
807
808
809
810
...
813
814
815
816
817
818
819












820
821
822
823
824
825
826
827
828
829

830
831
832
833
834
835
836
/*
** 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.














*/
const char *find_option(const char *zLong, const char *zShort, int hasArg){
  int i;
  int nLong;
  const char *zReturn = 0;
  assert( hasArg==0 || hasArg==1 );
  nLong = strlen(zLong);

  for(i=1; i<g.argc; i++){
    char *z;
    if (i+hasArg >= g.argc) break;
    z = g.argv[i];
    if( z[0]!='-' ) continue;
    z++;
    if( z[0]=='-' ){
      if( z[1]==0 ){
        remove_from_argv(i, 1);
        break;
................................................................................
    }
    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 ){












        zReturn = g.argv[i+hasArg];
        remove_from_argv(i, 1+hasArg);
        break;
      }
    }else if( fossil_strcmp(z,zShort)==0 ){
      zReturn = g.argv[i+hasArg];
      remove_from_argv(i, 1+hasArg);
      break;
    }
  }

  return zReturn;
}

/*
** Verify that there are no unprocessed command-line options.  If
** Any remaining command-line argument begins with "-" print
** an error message and quit.







>
>
>
>
>
>
>
>
>
>
>
>
>
>



|



>


<







 







>
>
>
>
>
>
>
>
>
>
>
>




<
<
<
<


>







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
821
822
823
824
...
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
854
855
856
857
858
859
/*
** 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);
  nShort = zShort ? strlen(zShort) : 0;
  for(i=1; i<g.argc; i++){
    char *z;

    z = g.argv[i];
    if( z[0]!='-' ) continue;
    z++;
    if( z[0]=='-' ){
      if( z[1]==0 ){
        remove_from_argv(i, 1);
        break;
................................................................................
    }
    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;
}

/*
** Verify that there are no unprocessed command-line options.  If
** Any remaining command-line argument begins with "-" print
** an error message and quit.