Fossil

Check-in [24b9fbca]
Login

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

Overview
Comment:Minor usage simplifications in the th1 argv API.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | th1-query-api
Files: files | file ages | folders
SHA1: 24b9fbca0fb2202874c3289c328f7cc04459078a
User & Date: stephan 2012-07-22 13:11:44.514
Context
2012-07-22
16:54
Fixed a memleak in the th1 variadic proc args support. ... (check-in: bee00366 user: stephan tags: th1-query-api)
13:11
Minor usage simplifications in the th1 argv API. ... (check-in: 24b9fbca user: stephan tags: th1-query-api)
11:55
Reduced the set of th1-exported SQLITE_xxx constants to those which are actually used in script APIs. Minor doc updates. ... (check-in: 8e040551 user: stephan tags: th1-query-api)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/th_main.c.
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
** Functions more or less like Fossil's find_option().
** If the given argument is found then its value is returned,
** else defaultValue is returned. If that is not set
** and the option is not found, an error is thrown.
** If defaultValue is provided, shortName must also be provided
** but it may be empty. For example:
**
** set foo [argv_getstr foo "" "hi, world"]
**
** ACHTUNG: find_option() removes any entries it finds from
** g.argv, such that future calls to find_option() will not
** find the same option.
*/
static int argvFindOptionStringCmd(
  Th_Interp *interp,







|







631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
** Functions more or less like Fossil's find_option().
** If the given argument is found then its value is returned,
** else defaultValue is returned. If that is not set
** and the option is not found, an error is thrown.
** If defaultValue is provided, shortName must also be provided
** but it may be empty. For example:
**
** set foo [argv getstr foo "" "hi, world"]
**
** ACHTUNG: find_option() removes any entries it finds from
** g.argv, such that future calls to find_option() will not
** find the same option.
*/
static int argvFindOptionStringCmd(
  Th_Interp *interp,
845
846
847
848
849
850
851

852

853

854
855
856
857
858
859
860
  const char **argv, 
  int *argl
){
  static Th_SubCommand aSub[] = {
    {"len",      argvArgcCmd},
    {"at",       argvGetAtCmd},
    {"getstr",   argvFindOptionStringCmd},

    {"getbool",  argvFindOptionBoolCmd},

    {"getint",   argvFindOptionIntCmd},

    {0, 0}
  };
  Th_CallSubCommand2( interp, ctx, argc, argv, argl, aSub );
}

int th_register_argv(Th_Interp *interp){
  static Th_Command_Reg aCommand[] = {







>

>

>







845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
  const char **argv, 
  int *argl
){
  static Th_SubCommand aSub[] = {
    {"len",      argvArgcCmd},
    {"at",       argvGetAtCmd},
    {"getstr",   argvFindOptionStringCmd},
    {"string",   argvFindOptionStringCmd},
    {"getbool",  argvFindOptionBoolCmd},
    {"bool",     argvFindOptionBoolCmd},
    {"getint",   argvFindOptionIntCmd},
    {"int",      argvFindOptionIntCmd},
    {0, 0}
  };
  Th_CallSubCommand2( interp, ctx, argc, argv, argl, aSub );
}

int th_register_argv(Th_Interp *interp){
  static Th_Command_Reg aCommand[] = {
Changes to test/th1-query-api-1.th1.
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
set ARGC [argv len]
puts ARGC = $ARGC "\n"
for {set i 0} {$i < $ARGC} {incr i} {
    puts "argv at $i = " [argv at $i] \n
}

set magicDefault hi
set optA [argv getstr AA a $magicDefault]
puts "argv getstr AA = " $optA \n

set optA [argv getbool BB b 0]
puts "argv getbool BB = " $optA \n

set exception 0
catch {
    argv getint noSuchOptionAndNoDefault
} exception
puts exception = $exception "\n"

enable_output 1

proc multiStmt {} {
    set max 5
    set i 0
    set s(0) 0
    for {set i 0} {$i < $max} {incr i} {
       set s($i) [query prepare "SELECT $i"]
       puts "s($i) = $s($i)\n"







|
|

|
|



|



<
<







103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121


122
123
124
125
126
127
128
set ARGC [argv len]
puts ARGC = $ARGC "\n"
for {set i 0} {$i < $ARGC} {incr i} {
    puts "argv at $i = " [argv at $i] \n
}

set magicDefault hi
set optA [argv string AA a $magicDefault]
puts "argv string AA = " $optA \n

set optA [argv bool BB b 0]
puts "argv bool BB = " $optA \n

set exception 0
catch {
    argv int noSuchOptionAndNoDefault
} exception
puts exception = $exception "\n"



proc multiStmt {} {
    set max 5
    set i 0
    set s(0) 0
    for {set i 0} {$i < $max} {incr i} {
       set s($i) [query prepare "SELECT $i"]
       puts "s($i) = $s($i)\n"
151
152
153
154
155
156
157
158
159
160
    for {set i 0} {$i < $max} {incr i} {
       puts "closing stmt $s($i)\n"
       query $s($i) finalize
    }
}
multiStmt

enable_output 1
puts "If you got this far, you win!\n"
</th1>







<


149
150
151
152
153
154
155

156
157
    for {set i 0} {$i < $max} {incr i} {
       puts "closing stmt $s($i)\n"
       query $s($i) finalize
    }
}
multiStmt


puts "If you got this far, you win!\n"
</th1>
Changes to www/th1_argv.wiki.
1
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16




17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<h1>TH1 "argv" API</h1>

The "argv" API provides features for accessing command-line arguments
and GET/POST values. They (unfortunately) do not provide access to
POST data submitted in JSON mode.


Example usage:

<nowiki><pre>
&lt;th1>
set argc [argv len]
set appName [argv at 0]
# Fetch --foo|-f argument:
set foo [argv getstr foo f "default value"]
&lt;th1>
</pre></nowiki>





The various subcommands are described below...

<h2>len</h2>

Returns the number of arguments.

<nowiki><pre>
set argc [argv len]
</pre></nowiki>


<h2>at</h2>

Fetches the argument at the given index.

<nowiki><pre>
set arg [argv at 3]
</pre></nowiki>



<h2>getstr</h2>

Searches for a CLI/GET/POST parameter. In CLI this function has some
non-intuitive behaviour inherited from fossil's internals: once a
flag/parameter is fetched, it is removed from the internal arguments
list, meaning that this function will never see it a second time.

<nowiki><pre>
set something [argv getstr "something" "S" "default"]
</pre></nowiki>

If no default value is provided, an error is triggered if the value is
not found.

If you do not want to search for a short-form flag, set it to an empty
string.

BUG: flag checking does not work properly in CGI mode when using
upper-case flags (apparently due to historic special-case behaviour in
fossil for upper-case vars).

<h2>getbool</h2>

Works almost like <tt>getstr</tt> but searches for boolean flags. CLI boolean flags
have no explicit value, and are "true" if the are set at all.

<nowiki><pre>
set doSomething [argv getbool "do-something" "D" 0]
</pre></nowiki>

<h2>getint</h2>

Works almost like <tt>getstr</tt> but searches for integer flags.


<nowiki><pre>
set limit [argv getbool "limit" "L" 10]
</pre></nowiki>




|
>











>
>
>
>














|





>

|







|








|
<
|

|





|


|





|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<h1>TH1 "argv" API</h1>

The "argv" API provides features for accessing command-line arguments
and GET/POST values. They (unfortunately) do not provide access to
POST data submitted in JSON mode (which fossil internally doesn't really
know about).

Example usage:

<nowiki><pre>
&lt;th1>
set argc [argv len]
set appName [argv at 0]
# Fetch --foo|-f argument:
set foo [argv getstr foo f "default value"]
&lt;th1>
</pre></nowiki>

(Note that fossil does not actually care if an argument starts
with 1 or 2 dashes. The convention of using 1 for "short-form"
flags and 2 for "long-form" is purely historical.)

The various subcommands are described below...

<h2>len</h2>

Returns the number of arguments.

<nowiki><pre>
set argc [argv len]
</pre></nowiki>


<h2>at</h2>

Fetches the argument at the given index (0-based).

<nowiki><pre>
set arg [argv at 3]
</pre></nowiki>

The fossil binary's name is stored in argument #0.

<h2>getstr|string</h2>

Searches for a CLI/GET/POST parameter. In CLI this function has some
non-intuitive behaviour inherited from fossil's internals: once a
flag/parameter is fetched, it is removed from the internal arguments
list, meaning that this function will never see it a second time.

<nowiki><pre>
set something [argv string "something" "S" "default"]
</pre></nowiki>

If no default value is provided, an error is triggered if the value is
not found.

If you do not want to search for a short-form flag, set it to an empty
string.

NOTE: flag checking does not work in CGI mode when using <em>upper-case</em>

flags (fossil treats upper-case names as environment variables).

<h2>getbool|bool</h2>

Works almost like <tt>getstr</tt> but searches for boolean flags. CLI boolean flags
have no explicit value, and are "true" if the are set at all.

<nowiki><pre>
set doSomething [argv bool "do-something" "D" 0]
</pre></nowiki>

<h2>getint|int</h2>

Works almost like <tt>getstr</tt> but searches for integer flags.


<nowiki><pre>
set limit [argv int "limit" "L" 10]
</pre></nowiki>