Fossil SCM

Minor usage simplifications in the th1 argv API.

stephan 2012-07-22 13:11 th1-query-api
Commit 24b9fbca0fb2202874c3289c328f7cc04459078a
+4 -1
--- src/th_main.c
+++ src/th_main.c
@@ -633,11 +633,11 @@
633633
** else defaultValue is returned. If that is not set
634634
** and the option is not found, an error is thrown.
635635
** If defaultValue is provided, shortName must also be provided
636636
** but it may be empty. For example:
637637
**
638
-** set foo [argv_getstr foo "" "hi, world"]
638
+** set foo [argv getstr foo "" "hi, world"]
639639
**
640640
** ACHTUNG: find_option() removes any entries it finds from
641641
** g.argv, such that future calls to find_option() will not
642642
** find the same option.
643643
*/
@@ -847,12 +847,15 @@
847847
){
848848
static Th_SubCommand aSub[] = {
849849
{"len", argvArgcCmd},
850850
{"at", argvGetAtCmd},
851851
{"getstr", argvFindOptionStringCmd},
852
+ {"string", argvFindOptionStringCmd},
852853
{"getbool", argvFindOptionBoolCmd},
854
+ {"bool", argvFindOptionBoolCmd},
853855
{"getint", argvFindOptionIntCmd},
856
+ {"int", argvFindOptionIntCmd},
854857
{0, 0}
855858
};
856859
Th_CallSubCommand2( interp, ctx, argc, argv, argl, aSub );
857860
}
858861
859862
--- src/th_main.c
+++ src/th_main.c
@@ -633,11 +633,11 @@
633 ** else defaultValue is returned. If that is not set
634 ** and the option is not found, an error is thrown.
635 ** If defaultValue is provided, shortName must also be provided
636 ** but it may be empty. For example:
637 **
638 ** set foo [argv_getstr foo "" "hi, world"]
639 **
640 ** ACHTUNG: find_option() removes any entries it finds from
641 ** g.argv, such that future calls to find_option() will not
642 ** find the same option.
643 */
@@ -847,12 +847,15 @@
847 ){
848 static Th_SubCommand aSub[] = {
849 {"len", argvArgcCmd},
850 {"at", argvGetAtCmd},
851 {"getstr", argvFindOptionStringCmd},
 
852 {"getbool", argvFindOptionBoolCmd},
 
853 {"getint", argvFindOptionIntCmd},
 
854 {0, 0}
855 };
856 Th_CallSubCommand2( interp, ctx, argc, argv, argl, aSub );
857 }
858
859
--- src/th_main.c
+++ src/th_main.c
@@ -633,11 +633,11 @@
633 ** else defaultValue is returned. If that is not set
634 ** and the option is not found, an error is thrown.
635 ** If defaultValue is provided, shortName must also be provided
636 ** but it may be empty. For example:
637 **
638 ** set foo [argv getstr foo "" "hi, world"]
639 **
640 ** ACHTUNG: find_option() removes any entries it finds from
641 ** g.argv, such that future calls to find_option() will not
642 ** find the same option.
643 */
@@ -847,12 +847,15 @@
847 ){
848 static Th_SubCommand aSub[] = {
849 {"len", argvArgcCmd},
850 {"at", argvGetAtCmd},
851 {"getstr", argvFindOptionStringCmd},
852 {"string", argvFindOptionStringCmd},
853 {"getbool", argvFindOptionBoolCmd},
854 {"bool", argvFindOptionBoolCmd},
855 {"getint", argvFindOptionIntCmd},
856 {"int", argvFindOptionIntCmd},
857 {0, 0}
858 };
859 Th_CallSubCommand2( interp, ctx, argc, argv, argl, aSub );
860 }
861
862
--- test/th1-query-api-1.th1
+++ test/th1-query-api-1.th1
@@ -105,24 +105,22 @@
105105
for {set i 0} {$i < $ARGC} {incr i} {
106106
puts "argv at $i = " [argv at $i] \n
107107
}
108108
109109
set magicDefault hi
110
-set optA [argv getstr AA a $magicDefault]
111
-puts "argv getstr AA = " $optA \n
110
+set optA [argv string AA a $magicDefault]
111
+puts "argv string AA = " $optA \n
112112
113
-set optA [argv getbool BB b 0]
114
-puts "argv getbool BB = " $optA \n
113
+set optA [argv bool BB b 0]
114
+puts "argv bool BB = " $optA \n
115115
116116
set exception 0
117117
catch {
118
- argv getint noSuchOptionAndNoDefault
118
+ argv int noSuchOptionAndNoDefault
119119
} exception
120120
puts exception = $exception "\n"
121121
122
-enable_output 1
123
-
124122
proc multiStmt {} {
125123
set max 5
126124
set i 0
127125
set s(0) 0
128126
for {set i 0} {$i < $max} {incr i} {
@@ -153,8 +151,7 @@
153151
query $s($i) finalize
154152
}
155153
}
156154
multiStmt
157155
158
-enable_output 1
159156
puts "If you got this far, you win!\n"
160157
</th1>
161158
--- test/th1-query-api-1.th1
+++ test/th1-query-api-1.th1
@@ -105,24 +105,22 @@
105 for {set i 0} {$i < $ARGC} {incr i} {
106 puts "argv at $i = " [argv at $i] \n
107 }
108
109 set magicDefault hi
110 set optA [argv getstr AA a $magicDefault]
111 puts "argv getstr AA = " $optA \n
112
113 set optA [argv getbool BB b 0]
114 puts "argv getbool BB = " $optA \n
115
116 set exception 0
117 catch {
118 argv getint noSuchOptionAndNoDefault
119 } exception
120 puts exception = $exception "\n"
121
122 enable_output 1
123
124 proc multiStmt {} {
125 set max 5
126 set i 0
127 set s(0) 0
128 for {set i 0} {$i < $max} {incr i} {
@@ -153,8 +151,7 @@
153 query $s($i) finalize
154 }
155 }
156 multiStmt
157
158 enable_output 1
159 puts "If you got this far, you win!\n"
160 </th1>
161
--- test/th1-query-api-1.th1
+++ test/th1-query-api-1.th1
@@ -105,24 +105,22 @@
105 for {set i 0} {$i < $ARGC} {incr i} {
106 puts "argv at $i = " [argv at $i] \n
107 }
108
109 set magicDefault hi
110 set optA [argv string AA a $magicDefault]
111 puts "argv string AA = " $optA \n
112
113 set optA [argv bool BB b 0]
114 puts "argv bool BB = " $optA \n
115
116 set exception 0
117 catch {
118 argv int noSuchOptionAndNoDefault
119 } exception
120 puts exception = $exception "\n"
121
 
 
122 proc multiStmt {} {
123 set max 5
124 set i 0
125 set s(0) 0
126 for {set i 0} {$i < $max} {incr i} {
@@ -153,8 +151,7 @@
151 query $s($i) finalize
152 }
153 }
154 multiStmt
155
 
156 puts "If you got this far, you win!\n"
157 </th1>
158
+16 -11
--- www/th1_argv.wiki
+++ www/th1_argv.wiki
@@ -1,10 +1,11 @@
11
<h1>TH1 "argv" API</h1>
22
33
The "argv" API provides features for accessing command-line arguments
44
and GET/POST values. They (unfortunately) do not provide access to
5
-POST data submitted in JSON mode.
5
+POST data submitted in JSON mode (which fossil internally doesn't really
6
+know about).
67
78
Example usage:
89
910
<nowiki><pre>
1011
&lt;th1>
@@ -12,10 +13,14 @@
1213
set appName [argv at 0]
1314
# Fetch --foo|-f argument:
1415
set foo [argv getstr foo f "default value"]
1516
&lt;th1>
1617
</pre></nowiki>
18
+
19
+(Note that fossil does not actually care if an argument starts
20
+with 1 or 2 dashes. The convention of using 1 for "short-form"
21
+flags and 2 for "long-form" is purely historical.)
1722
1823
The various subcommands are described below...
1924
2025
<h2>len</h2>
2126
@@ -26,50 +31,50 @@
2631
</pre></nowiki>
2732
2833
2934
<h2>at</h2>
3035
31
-Fetches the argument at the given index.
36
+Fetches the argument at the given index (0-based).
3237
3338
<nowiki><pre>
3439
set arg [argv at 3]
3540
</pre></nowiki>
3641
42
+The fossil binary's name is stored in argument #0.
3743
38
-<h2>getstr</h2>
44
+<h2>getstr|string</h2>
3945
4046
Searches for a CLI/GET/POST parameter. In CLI this function has some
4147
non-intuitive behaviour inherited from fossil's internals: once a
4248
flag/parameter is fetched, it is removed from the internal arguments
4349
list, meaning that this function will never see it a second time.
4450
4551
<nowiki><pre>
46
-set something [argv getstr "something" "S" "default"]
52
+set something [argv string "something" "S" "default"]
4753
</pre></nowiki>
4854
4955
If no default value is provided, an error is triggered if the value is
5056
not found.
5157
5258
If you do not want to search for a short-form flag, set it to an empty
5359
string.
5460
55
-BUG: flag checking does not work properly in CGI mode when using
56
-upper-case flags (apparently due to historic special-case behaviour in
57
-fossil for upper-case vars).
61
+NOTE: flag checking does not work in CGI mode when using <em>upper-case</em>
62
+flags (fossil treats upper-case names as environment variables).
5863
59
-<h2>getbool</h2>
64
+<h2>getbool|bool</h2>
6065
6166
Works almost like <tt>getstr</tt> but searches for boolean flags. CLI boolean flags
6267
have no explicit value, and are "true" if the are set at all.
6368
6469
<nowiki><pre>
65
-set doSomething [argv getbool "do-something" "D" 0]
70
+set doSomething [argv bool "do-something" "D" 0]
6671
</pre></nowiki>
6772
68
-<h2>getint</h2>
73
+<h2>getint|int</h2>
6974
7075
Works almost like <tt>getstr</tt> but searches for integer flags.
7176
7277
7378
<nowiki><pre>
74
-set limit [argv getbool "limit" "L" 10]
79
+set limit [argv int "limit" "L" 10]
7580
</pre></nowiki>
7681
--- www/th1_argv.wiki
+++ www/th1_argv.wiki
@@ -1,10 +1,11 @@
1 <h1>TH1 "argv" API</h1>
2
3 The "argv" API provides features for accessing command-line arguments
4 and GET/POST values. They (unfortunately) do not provide access to
5 POST data submitted in JSON mode.
 
6
7 Example usage:
8
9 <nowiki><pre>
10 &lt;th1>
@@ -12,10 +13,14 @@
12 set appName [argv at 0]
13 # Fetch --foo|-f argument:
14 set foo [argv getstr foo f "default value"]
15 &lt;th1>
16 </pre></nowiki>
 
 
 
 
17
18 The various subcommands are described below...
19
20 <h2>len</h2>
21
@@ -26,50 +31,50 @@
26 </pre></nowiki>
27
28
29 <h2>at</h2>
30
31 Fetches the argument at the given index.
32
33 <nowiki><pre>
34 set arg [argv at 3]
35 </pre></nowiki>
36
 
37
38 <h2>getstr</h2>
39
40 Searches for a CLI/GET/POST parameter. In CLI this function has some
41 non-intuitive behaviour inherited from fossil's internals: once a
42 flag/parameter is fetched, it is removed from the internal arguments
43 list, meaning that this function will never see it a second time.
44
45 <nowiki><pre>
46 set something [argv getstr "something" "S" "default"]
47 </pre></nowiki>
48
49 If no default value is provided, an error is triggered if the value is
50 not found.
51
52 If you do not want to search for a short-form flag, set it to an empty
53 string.
54
55 BUG: flag checking does not work properly in CGI mode when using
56 upper-case flags (apparently due to historic special-case behaviour in
57 fossil for upper-case vars).
58
59 <h2>getbool</h2>
60
61 Works almost like <tt>getstr</tt> but searches for boolean flags. CLI boolean flags
62 have no explicit value, and are "true" if the are set at all.
63
64 <nowiki><pre>
65 set doSomething [argv getbool "do-something" "D" 0]
66 </pre></nowiki>
67
68 <h2>getint</h2>
69
70 Works almost like <tt>getstr</tt> but searches for integer flags.
71
72
73 <nowiki><pre>
74 set limit [argv getbool "limit" "L" 10]
75 </pre></nowiki>
76
--- www/th1_argv.wiki
+++ www/th1_argv.wiki
@@ -1,10 +1,11 @@
1 <h1>TH1 "argv" API</h1>
2
3 The "argv" API provides features for accessing command-line arguments
4 and GET/POST values. They (unfortunately) do not provide access to
5 POST data submitted in JSON mode (which fossil internally doesn't really
6 know about).
7
8 Example usage:
9
10 <nowiki><pre>
11 &lt;th1>
@@ -12,10 +13,14 @@
13 set appName [argv at 0]
14 # Fetch --foo|-f argument:
15 set foo [argv getstr foo f "default value"]
16 &lt;th1>
17 </pre></nowiki>
18
19 (Note that fossil does not actually care if an argument starts
20 with 1 or 2 dashes. The convention of using 1 for "short-form"
21 flags and 2 for "long-form" is purely historical.)
22
23 The various subcommands are described below...
24
25 <h2>len</h2>
26
@@ -26,50 +31,50 @@
31 </pre></nowiki>
32
33
34 <h2>at</h2>
35
36 Fetches the argument at the given index (0-based).
37
38 <nowiki><pre>
39 set arg [argv at 3]
40 </pre></nowiki>
41
42 The fossil binary's name is stored in argument #0.
43
44 <h2>getstr|string</h2>
45
46 Searches for a CLI/GET/POST parameter. In CLI this function has some
47 non-intuitive behaviour inherited from fossil's internals: once a
48 flag/parameter is fetched, it is removed from the internal arguments
49 list, meaning that this function will never see it a second time.
50
51 <nowiki><pre>
52 set something [argv string "something" "S" "default"]
53 </pre></nowiki>
54
55 If no default value is provided, an error is triggered if the value is
56 not found.
57
58 If you do not want to search for a short-form flag, set it to an empty
59 string.
60
61 NOTE: flag checking does not work in CGI mode when using <em>upper-case</em>
62 flags (fossil treats upper-case names as environment variables).
 
63
64 <h2>getbool|bool</h2>
65
66 Works almost like <tt>getstr</tt> but searches for boolean flags. CLI boolean flags
67 have no explicit value, and are "true" if the are set at all.
68
69 <nowiki><pre>
70 set doSomething [argv bool "do-something" "D" 0]
71 </pre></nowiki>
72
73 <h2>getint|int</h2>
74
75 Works almost like <tt>getstr</tt> but searches for integer flags.
76
77
78 <nowiki><pre>
79 set limit [argv int "limit" "L" 10]
80 </pre></nowiki>
81

Keyboard Shortcuts

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