Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| SHA1 Hash: | 24b9fbca0fb2202874c3289c328f7cc04459078a |
|---|---|
| Date: | 2012-07-22 13:11:44 |
| User: | stephan |
| Comment: | Minor usage simplifications in the th1 argv API. |
Tags And Properties
- branch=th1-query-api inherited from [c3b10e12a1]
- sym-th1-query-api inherited from [c3b10e12a1]
Changes
Changes to src/th_main.c
631 ** Functions more or less like Fossil's find_option(). 631 ** Functions more or less like Fossil's find_option(). 632 ** If the given argument is found then its value is returned, 632 ** If the given argument is found then its value is returned, 633 ** else defaultValue is returned. If that is not set 633 ** else defaultValue is returned. If that is not set 634 ** and the option is not found, an error is thrown. 634 ** and the option is not found, an error is thrown. 635 ** If defaultValue is provided, shortName must also be provided 635 ** If defaultValue is provided, shortName must also be provided 636 ** but it may be empty. For example: 636 ** but it may be empty. For example: 637 ** 637 ** 638 ** set foo [argv_getstr foo "" "hi, world"] | 638 ** set foo [argv getstr foo "" "hi, world"] 639 ** 639 ** 640 ** ACHTUNG: find_option() removes any entries it finds from 640 ** ACHTUNG: find_option() removes any entries it finds from 641 ** g.argv, such that future calls to find_option() will not 641 ** g.argv, such that future calls to find_option() will not 642 ** find the same option. 642 ** find the same option. 643 */ 643 */ 644 static int argvFindOptionStringCmd( 644 static int argvFindOptionStringCmd( 645 Th_Interp *interp, 645 Th_Interp *interp, ................................................................................................................................................................................ 845 const char **argv, 845 const char **argv, 846 int *argl 846 int *argl 847 ){ 847 ){ 848 static Th_SubCommand aSub[] = { 848 static Th_SubCommand aSub[] = { 849 {"len", argvArgcCmd}, 849 {"len", argvArgcCmd}, 850 {"at", argvGetAtCmd}, 850 {"at", argvGetAtCmd}, 851 {"getstr", argvFindOptionStringCmd}, 851 {"getstr", argvFindOptionStringCmd}, > 852 {"string", argvFindOptionStringCmd}, 852 {"getbool", argvFindOptionBoolCmd}, 853 {"getbool", argvFindOptionBoolCmd}, > 854 {"bool", argvFindOptionBoolCmd}, 853 {"getint", argvFindOptionIntCmd}, 855 {"getint", argvFindOptionIntCmd}, > 856 {"int", argvFindOptionIntCmd}, 854 {0, 0} 857 {0, 0} 855 }; 858 }; 856 Th_CallSubCommand2( interp, ctx, argc, argv, argl, aSub ); 859 Th_CallSubCommand2( interp, ctx, argc, argv, argl, aSub ); 857 } 860 } 858 861 859 int th_register_argv(Th_Interp *interp){ 862 int th_register_argv(Th_Interp *interp){ 860 static Th_Command_Reg aCommand[] = { 863 static Th_Command_Reg aCommand[] = {
Changes to test/th1-query-api-1.th1
103 set ARGC [argv len] 103 set ARGC [argv len] 104 puts ARGC = $ARGC "\n" 104 puts ARGC = $ARGC "\n" 105 for {set i 0} {$i < $ARGC} {incr i} { 105 for {set i 0} {$i < $ARGC} {incr i} { 106 puts "argv at $i = " [argv at $i] \n 106 puts "argv at $i = " [argv at $i] \n 107 } 107 } 108 108 109 set magicDefault hi 109 set magicDefault hi 110 set optA [argv getstr AA a $magicDefault] | 110 set optA [argv string AA a $magicDefault] 111 puts "argv getstr AA = " $optA \n | 111 puts "argv string AA = " $optA \n 112 112 113 set optA [argv getbool BB b 0] | 113 set optA [argv bool BB b 0] 114 puts "argv getbool BB = " $optA \n | 114 puts "argv bool BB = " $optA \n 115 115 116 set exception 0 116 set exception 0 117 catch { 117 catch { 118 argv getint noSuchOptionAndNoDefault | 118 argv int noSuchOptionAndNoDefault 119 } exception 119 } exception 120 puts exception = $exception "\n" 120 puts exception = $exception "\n" 121 121 122 enable_output 1 < 123 < 124 proc multiStmt {} { 122 proc multiStmt {} { 125 set max 5 123 set max 5 126 set i 0 124 set i 0 127 set s(0) 0 125 set s(0) 0 128 for {set i 0} {$i < $max} {incr i} { 126 for {set i 0} {$i < $max} {incr i} { 129 set s($i) [query prepare "SELECT $i"] 127 set s($i) [query prepare "SELECT $i"] 130 puts "s($i) = $s($i)\n" 128 puts "s($i) = $s($i)\n" ................................................................................................................................................................................ 151 for {set i 0} {$i < $max} {incr i} { 149 for {set i 0} {$i < $max} {incr i} { 152 puts "closing stmt $s($i)\n" 150 puts "closing stmt $s($i)\n" 153 query $s($i) finalize 151 query $s($i) finalize 154 } 152 } 155 } 153 } 156 multiStmt 154 multiStmt 157 155 158 enable_output 1 < 159 puts "If you got this far, you win!\n" 156 puts "If you got this far, you win!\n" 160 </th1> 157 </th1>
Changes to www/th1_argv.wiki
1 <h1>TH1 "argv" API</h1> 1 <h1>TH1 "argv" API</h1> 2 2 3 The "argv" API provides features for accessing command-line arguments 3 The "argv" API provides features for accessing command-line arguments 4 and GET/POST values. They (unfortunately) do not provide access to 4 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). 6 7 7 Example usage: 8 Example usage: 8 9 9 <nowiki><pre> 10 <nowiki><pre> 10 <th1> 11 <th1> 11 set argc [argv len] 12 set argc [argv len] 12 set appName [argv at 0] 13 set appName [argv at 0] 13 # Fetch --foo|-f argument: 14 # Fetch --foo|-f argument: 14 set foo [argv getstr foo f "default value"] 15 set foo [argv getstr foo f "default value"] 15 <th1> 16 <th1> 16 </pre></nowiki> 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.) 17 22 18 The various subcommands are described below... 23 The various subcommands are described below... 19 24 20 <h2>len</h2> 25 <h2>len</h2> 21 26 22 Returns the number of arguments. 27 Returns the number of arguments. 23 28 ................................................................................................................................................................................ 24 <nowiki><pre> 29 <nowiki><pre> 25 set argc [argv len] 30 set argc [argv len] 26 </pre></nowiki> 31 </pre></nowiki> 27 32 28 33 29 <h2>at</h2> 34 <h2>at</h2> 30 35 31 Fetches the argument at the given index. | 36 Fetches the argument at the given index (0-based). 32 37 33 <nowiki><pre> 38 <nowiki><pre> 34 set arg [argv at 3] 39 set arg [argv at 3] 35 </pre></nowiki> 40 </pre></nowiki> 36 41 > 42 The fossil binary's name is stored in argument #0. 37 43 38 <h2>getstr</h2> | 44 <h2>getstr|string</h2> 39 45 40 Searches for a CLI/GET/POST parameter. In CLI this function has some 46 Searches for a CLI/GET/POST parameter. In CLI this function has some 41 non-intuitive behaviour inherited from fossil's internals: once a 47 non-intuitive behaviour inherited from fossil's internals: once a 42 flag/parameter is fetched, it is removed from the internal arguments 48 flag/parameter is fetched, it is removed from the internal arguments 43 list, meaning that this function will never see it a second time. 49 list, meaning that this function will never see it a second time. 44 50 45 <nowiki><pre> 51 <nowiki><pre> 46 set something [argv getstr "something" "S" "default"] | 52 set something [argv string "something" "S" "default"] 47 </pre></nowiki> 53 </pre></nowiki> 48 54 49 If no default value is provided, an error is triggered if the value is 55 If no default value is provided, an error is triggered if the value is 50 not found. 56 not found. 51 57 52 If you do not want to search for a short-form flag, set it to an empty 58 If you do not want to search for a short-form flag, set it to an empty 53 string. 59 string. 54 60 55 BUG: flag checking does not work properly in CGI mode when using | 61 NOTE: flag checking does not work in CGI mode when using <em>upper-case</em> 56 upper-case flags (apparently due to historic special-case behaviour in | 62 flags (fossil treats upper-case names as environment variables). 57 fossil for upper-case vars). < 58 63 59 <h2>getbool</h2> | 64 <h2>getbool|bool</h2> 60 65 61 Works almost like <tt>getstr</tt> but searches for boolean flags. CLI boolean fl 66 Works almost like <tt>getstr</tt> but searches for boolean flags. CLI boolean fl 62 have no explicit value, and are "true" if the are set at all. 67 have no explicit value, and are "true" if the are set at all. 63 68 64 <nowiki><pre> 69 <nowiki><pre> 65 set doSomething [argv getbool "do-something" "D" 0] | 70 set doSomething [argv bool "do-something" "D" 0] 66 </pre></nowiki> 71 </pre></nowiki> 67 72 68 <h2>getint</h2> | 73 <h2>getint|int</h2> 69 74 70 Works almost like <tt>getstr</tt> but searches for integer flags. 75 Works almost like <tt>getstr</tt> but searches for integer flags. 71 76 72 77 73 <nowiki><pre> 78 <nowiki><pre> 74 set limit [argv getbool "limit" "L" 10] | 79 set limit [argv int "limit" "L" 10] 75 </pre></nowiki> 80 </pre></nowiki>