Fossil

Check-in [23200840]
Login

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

Overview
Comment:Minor improvements to the previous find_option() commit.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | th1-query-api
Files: files | file ages | folders
SHA1: 232008406fa462e3f12eb5bb2bc58738bca2777c
User & Date: stephan 2012-07-14 10:17:37
Original 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.
Context
2012-07-14
10:22
Added docs for the th1 query API. Added basic th1 argv-handling API. check-in: a8eace9a user: stephan tags: th1-query-api
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/main.c.

785
786
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
...
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


/*
** 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);
................................................................................
    }
    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;







|
|
>
>
>


|
|



|
>


>
|
|
>
|
|







 







>
|
|
>
>
>









>
|
|
>
>
>







785
786
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
...
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
860
861
862
863
864
865
866
867


/*
** 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. If hasArg is 0 and the argument is found then a
** pointer to an empty string is returned (to distinguish from
** NULL). If hasArg==1 and the option lies at the end of the argument
** list, it is treated as if it had not been found.
**
** 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        (hasArg==0)
**  -long VALUE  (hasArg==1)
**  -long=VALUE  (hasArg==1)
**  -short       (hasArg==0)
**  -short VALUE (hasArg==1)
**  -short=VALUE (hasArg==1)
*/
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);
................................................................................
    }
    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( hasArg ){
          if (i+hasArg >= g.argc) break;
          zReturn = g.argv[i+hasArg];
        }else{
          zReturn = "";
        }
        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( hasArg ){
          if (i+hasArg >= g.argc) break;
          zReturn = g.argv[i+hasArg];
        }else{
          zReturn = "";
        }
        remove_from_argv(i, 1+hasArg);
        break;
      }
    }
  }

  return zReturn;