home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tk3.3b1 / doc / ParseArgv.3 < prev    next >
Encoding:
Text File  |  1993-04-01  |  15.0 KB  |  365 lines

  1. '\"
  2. '\" Copyright (c) 1990-1992 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/wish/man/RCS/ParseArgv.3,v 1.7 93/04/01 09:41:47 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .so man.macros
  24. .HS Tk_ParseArgv tkc
  25. .BS
  26. .SH NAME
  27. Tk_ParseArgv \- process command-line options
  28. .SH SYNOPSIS
  29. \fB#include <tk.h>\fR
  30. .sp
  31. int
  32. .br
  33. \fBTk_ParseArgv\fR(\fIinterp, tkwin, argcPtr, argv, argTable, flags\fR)
  34. .SH ARGUMENTS
  35. .AS Tk_ArgvInfo *argTable
  36. .AP Tcl_Interp *interp in
  37. Interpreter to use for returning error messages.
  38. .AP Tk_Window tkwin in
  39. Window to use when arguments specify Tk options.  If NULL, then
  40. no Tk options will be processed.
  41. .AP int argcPtr in/out
  42. Pointer to number of arguments in argv;  gets modified to hold
  43. number of unprocessed arguments that remain after the call.
  44. .AP char **argv in/out
  45. Command line arguments passed to main program.  Modified to
  46. hold unprocessed arguments that remain after the call.
  47. .AP Tk_ArgvInfo *argTable in
  48. Array of argument descriptors, terminated by element with
  49. type TK_ARGV_END.
  50. .AP int flags in
  51. If non-zero, then it specifies one or more flags that control the
  52. parsing of arguments.  Different flags may be OR'ed together.
  53. .na
  54. The flags currently defined are TK_ARGV_DONT_SKIP_FIRST_ARG,
  55. TK_ARGV_NO_ABBREV, TK_ARGV_NO_LEFTOVERS, and TK_ARGV_NO_DEFAULTS.
  56. .ad
  57. .BE
  58. .SH DESCRIPTION
  59. .PP
  60. \fBTk_ParseArgv\fR processes an array of command-line arguments according
  61. to a table describing the kinds of arguments that are expected.
  62. Each of the arguments in \fIargv\fR is processed in turn:  if it matches
  63. one of the entries in \fIargTable\fR, the argument is processed
  64. according to that entry and discarded.  The arguments that do not
  65. match anything in \fIargTable\fR are copied down to the beginning
  66. of \fIargv\fR (retaining their original order) and returned to
  67. the caller.  At the end of the call
  68. \fBTk_ParseArgv\fR sets \fI*argcPtr\fR to hold the number of
  69. arguments that are left in \fIargv\fR, and \fIargv[*argcPtr]\fR
  70. will hold the value NULL.  Normally, \fBTk_ParseArgv\fR
  71. assumes that \fIargv[0]\fR is a command name, so it is treated like
  72. an argument that doesn't match \fIargTable\fR and returned to the
  73. caller;  however, if the TK_ARGV_DONT_SKIP_FIRST_ARG bit is set in
  74. \fIflags\fR then \fIargv[0]\fR will be processed just like the other
  75. elements of \fIargv\fR.
  76. .PP
  77. \fBTk_ParseArgv\fR normally returns the value TCL_OK.  If an error
  78. occurs while parsing the arguments, then TCL_ERROR is returned and
  79. \fBTk_ParseArgv\fR will leave an error message in \fIinterp->result\fR
  80. in the standard Tcl fashion.  In
  81. the event of an error return, \fI*argvPtr\fR will not have been
  82. modified, but \fIargv\fR could have been partially modified.  The
  83. possible causes of errors are explained below.
  84. .PP
  85. The \fIargTable\fR array specifies the kinds of arguments that are
  86. expected;  each of its entries has the following structure:
  87. .DS
  88. .ta 2c
  89. \fBtypedef struct\fR {
  90.     \fBchar\fR    *\fIkey\fR;
  91.     \fBint\fR    \fItype\fR;
  92.     \fBchar\fR    *\fIsrc\fR;
  93.     \fBchar\fR    *\fIdst\fR;
  94.     \fBchar\fR    *\fIhelp\fR;
  95. \fB} Tk_ArgvInfo;\fR
  96. .DE
  97. .LP
  98. The \fIkey\fR field is a string such as ``\-display'' or ``\-bg''
  99. that is compared with the values in \fIargv\fR.  \fIType\fR
  100. indicates how to process an argument that matches \fIkey\fR
  101. (more on this below).  \fISrc\fR and \fIdst\fR are additional
  102. values used in processing the argument.  Their exact usage
  103. depends on \fItype\fR, but typically \fIsrc\fR indicates
  104. a value and \fIdst\fR indicates where to store the
  105. value.  The \fBchar *\fR declarations for \fIsrc\fR and \fIdst\fR
  106. are placeholders:  the actual types may be different.  Lastly,
  107. \fIhelp\fR is a string giving a brief description
  108. of this option;  this string is printed when users ask for help
  109. about command-line options.
  110. .PP
  111. When processing an argument in \fIargv\fR, \fBTk_ParseArgv\fR
  112. compares the argument to each of the \fIkey\fR's in \fIargTable\fR.
  113. \fBTk_ParseArgv\fR selects the first specifier whose \fIkey\fR matches
  114. the argument exactly, if such a specifier exists.  Otherwise
  115. \fBTk_ParseArgv\fR selects a specifier for which the argument
  116. is a unique abbreviation.  If the argument is a unique abbreviation
  117. for more than one specifier, then an error is returned.  If there
  118. is no matching entry in \fIargTable\fR, then the argument is
  119. skipped and returned to the caller.
  120. .PP
  121. Once a matching argument specifier is found, \fBTk_ParseArgv\fR
  122. processes the argument according to the \fItype\fR field of the
  123. specifier.  The argument that matched \fIkey\fR is called ``the matching
  124. argument'' in the descriptions below.  As part of the processing,
  125. \fBTk_ParseArgv\fR may also use the next argument in \fIargv\fR
  126. after the matching argument, which is called ``the following
  127. argument''.  The legal values for \fItype\fR, and the processing
  128. that they cause, are as follows:
  129. .TP
  130. \fBTK_ARGV_END\fR
  131. Marks the end of the table.  The last entry in \fIargTable\fR
  132. must have this type;  all of its other fields are ignored and it
  133. will never match any arguments.
  134. .TP
  135. \fBTK_ARGV_CONSTANT\fR
  136. \fISrc\fR is treated as an integer and \fIdst\fR is treated
  137. as a pointer to an integer.  \fISrc\fR is stored at \fI*dst\fR.
  138. The matching argument is discarded.
  139. .TP
  140. \fBTK_ARGV_INT\fR
  141. The following argument must contain an
  142. integer string in the format accepted by \fBstrtol\fR (e.g. ``0''
  143. and ``0x'' prefixes may be used to specify octal or hexadecimal
  144. numbers, respectively).  \fIDst\fR is treated as a pointer to an
  145. integer;  the following argument is converted to an integer value
  146. and stored at \fI*dst\fR.  \fISrc\fR is ignored.  The matching
  147. and following arguments are discarded from \fIargv\fR.
  148. .TP
  149. \fBTK_ARGV_FLOAT\fR
  150. The following argument must contain a floating-point number in
  151. the format accepted by \fBstrtol\fR.
  152. \fIDst\fR is treated as the address of an double-precision
  153. floating point value;  the following argument is converted to a
  154. double-precision value and stored at \fI*dst\fR.  The matching
  155. and following arguments are discarded from \fIargv\fR.
  156. .TP
  157. \fBTK_ARGV_STRING\fR
  158. In this form, \fIdst\fR is treated as a pointer to a (char *);
  159. \fBTk_ParseArgv\fR stores at \fI*dst\fR a pointer to the following
  160. argument, and discards the matching and following arguments from
  161. \fIargv\fR.  \fISrc\fR is ignored.
  162. .TP
  163. \fBTK_ARGV_UID\fR
  164. This form is similar to TK_ARGV_STRING, except that the argument
  165. is turned into a Tk_Uid by calling \fBTk_GetUid\fR.
  166. \fIDst\fR is treated as a pointer to a
  167. Tk_Uid; \fBTk_ParseArgv\fR stores at \fI*dst\fR the Tk_Uid
  168. corresponding to the following
  169. argument, and discards the matching and following arguments from
  170. \fIargv\fR.  \fISrc\fR is ignored.
  171. .TP
  172. \fBTK_ARGV_CONST_OPTION\fR
  173. This form causes a Tk option to be set (as if the \fBoption\fR
  174. command had been invoked).  The \fIsrc\fR field is treated as a
  175. pointer to a string giving the value of an option, and \fIdst\fR
  176. is treated as a pointer to the name of the option.  The matching
  177. argument is discarded.  If \fItkwin\fR is NULL, then argument
  178. specifiers of this type are ignored (as if they did not exist).
  179. .TP
  180. \fBTK_ARGV_OPTION_VALUE\fR
  181. This form is similar to TK_ARGV_CONST_OPTION, except that the
  182. value of the option is taken from the following argument instead
  183. of from \fIsrc\fR.  \fIDst\fR is used as the name of the option.
  184. \fISrc\fR is ignored.  The matching and following arguments
  185. are discarded.  If \fItkwin\fR is NULL, then argument
  186. specifiers of this type are ignored (as if they did not exist).
  187. .TP
  188. \fBTK_ARGV_OPTION_NAME_VALUE\fR
  189. In this case the following argument is taken as the name of a Tk
  190. option and the argument after that is taken as the value for that
  191. option.  Both \fIsrc\fR and \fIdst\fR are ignored.  All three
  192. arguments are discarded from \fIargv\fR.  If \fItkwin\fR is NULL,
  193. then argument
  194. specifiers of this type are ignored (as if they did not exist).
  195. .TP
  196. \fBTK_ARGV_HELP\fR
  197. When this kind of option is encountered, \fBTk_ParseArgv\fR uses the
  198. \fIhelp\fR fields of \fIargTable\fR to format a message describing
  199. all the valid arguments.  The message is placed in \fIinterp->result\fR
  200. and \fBTk_ParseArgv\fR returns TCL_ERROR.  When this happens, the
  201. caller normally prints the help message and aborts.  If the \fIkey\fR
  202. field of a TK_ARGV_HELP specifier is NULL, then the specifier will
  203. never match any arguments;  in this case the specifier simply provides
  204. extra documentation, which will be included when some other
  205. TK_ARGV_HELP entry causes help information to be returned.
  206. .TP
  207. \fBTK_ARGV_REST\fR
  208. This option is used by programs or commands that allow the last
  209. several of their options to be the name and/or options for some
  210. other program.  If a \fBTK_ARGV_REST\fR argument is found, then
  211. \fBTk_ParseArgv\fR doesn't process any
  212. of the remaining arguments;  it returns them all at
  213. the beginning of \fIargv\fR (along with any other unprocessed arguments).
  214. In addition, \fBTk_ParseArgv\fR treats \fIdst\fR as the address of an
  215. integer value, and stores at \fI*dst\fR the index of the first of the
  216. \fBTK_ARGV_REST\fR options in the returned \fIargv\fR.  This allows the
  217. program to distinguish the \fBTK_ARGV_REST\fR options from other
  218. unprocessed options that preceeded the \fBTK_ARGV_REST\fR.
  219. .TP
  220. \fBTK_ARGV_FUNC\fR
  221. For this kind of argument, \fIsrc\fR is treated as the address of
  222. a procedure, which is invoked to process the following argument.
  223. The procedure should have the following structure:
  224. .DS
  225. .ta 1c 2c 3c 4c 5c 6c
  226. \fBint\fI
  227. func(dst, key, nextArg)
  228.     \fBchar\fR    *\fIdst\fR;
  229.     \fBchar\fR    *\fIkey\fR;
  230.     \fBchar\fR    *\fInextArg\fR;
  231. {
  232. }
  233. .DE
  234. .IP
  235. The \fIdst\fR and \fIkey\fR parameters will contain the
  236. corresponding fields from the \fIargTable\fR entry, and
  237. \fInextArg\fR will point to the following argument from \fIargv\fR
  238. (or NULL if there aren't any more arguments left in \fIargv\fR).
  239. If \fIfunc\fR uses \fInextArg\fR (so that
  240. \fBTk_ParseArgv\fR should discard it), then it should return 1.  Otherwise it
  241. should return 0 and \fBTkParseArgv\fR will process the following
  242. argument in the normal fashion.  In either event the matching argument
  243. is discarded.
  244. .TP
  245. \fBTK_ARGV_GENFUNC\fR
  246. This form provides a more general procedural escape.  It treats
  247. \fIsrc\fR as the address of a procedure, and passes that procedure
  248. all of the remaining arguments.  The procedure should have the following
  249. form:
  250. .DS
  251. .ta 1c 2c 3c 4c 5c 6c
  252. \fBint\fI
  253. genfunc(dst, interp, key, argc, argv)
  254.     \fBchar\fR    *\fIdst\fR;
  255.     \fBTcl_Interp\fR    *\fIinterp\fR;
  256.     \fBchar\fR    *\fIkey\fR;
  257.     \fBint\fR    \fIargc\fR;
  258.     \fBchar\fR    **\fIargv\fR;
  259. {
  260. }
  261. .DE
  262. .IP
  263. The \fIdst\fR and \fIkey\fR parameters will contain the
  264. corresponding fields from the \fIargTable\fR entry.  \fIInterp\fR
  265. will be the same as the \fIinterp\fR argument to \fBTcl_ParseArgv\fR.
  266. \fIArgc\fR and \fIargv\fR refer to all of the options after the
  267. matching one.  \fIGenfunc\fR should behave in a fashion similar
  268. to \fBTk_ParseArgv\fR:  parse as many of the remaining arguments as it can,
  269. then return any that are left by compacting them to the beginning of
  270. \fIargv\fR (starting at \fIargv\fR[0]).  \fIGenfunc\fR
  271. should return a count of how many arguments are left in \fIargv\fR;
  272. \fBTk_ParseArgv\fR will process them.  If \fIgenfunc\fR encounters
  273. an error then it should leave an error message in \fIinterp->result\fR,
  274. in the usual Tcl fashion, and return -1;  when this happens
  275. \fBTk_ParseArgv\fR will abort its processing and return TCL_ERROR.
  276.  
  277. .SH "FLAGS"
  278. .IP \fBTK_ARGV_DONT_SKIP_FIRST_ARG\fR
  279. \fBTk_ParseArgv\fR normally treats \fIargv[0]\fR as a program
  280. or command name, and returns it to the caller just as if it
  281. hadn't matched \fIargTable\fR.  If this flag is given, then
  282. \fIargv[0]\fR is not given special treatment.
  283. .IP \fBTK_ARGV_NO_ABBREV\fR
  284. Normally, \fBTk_ParseArgv\fR accepts unique abbreviations for
  285. \fIkey\fR values in \fIargTable\fR.  If this flag is given then
  286. only exact matches will be acceptable.
  287. .IP \fBTK_ARGV_NO_LEFTOVERS\fR
  288. Normally, \fBTk_ParseArgv\fR returns unrecognized arguments to the
  289. caller.  If this bit is set in \fIflags\fR then \fBTk_ParseArgv\fR
  290. will return an error if it encounters any argument that doesn't
  291. match \fIargTable\fR.  The only exception to this rule is \fIargv[0]\fR,
  292. which will be returned to the caller with no errors as
  293. long as TK_ARGV_DONT_SKIP_FIRST_ARG isn't specified.
  294. .IP \fBTK__NO_DEFAULTS\fR
  295. Normally, \fBTk_ParseArgv\fR searches an internal table of
  296. standard argument specifiers in addition to \fIargTable\fR.  If
  297. this bit is set in \fIflags\fR, then \fBTk_ParseArgv\fR will
  298. use only \fIargTable\fR and not its default table.
  299.  
  300. .SH EXAMPLE
  301. .PP
  302. Here is an example definition of an \fIargTable\fR and
  303. some sample command lines that use the options.  Note the effect
  304. on \fIargc\fR and \fIargv\fR;  arguments processed by \fBTk_ParseArgv\fR
  305. are eliminated from \fIargv\fR, and \fIargc\fR
  306. is updated to reflect reduced number of arguments.
  307. .DS L
  308. \fC/*
  309.  * Define and set default values for globals.
  310.  */
  311. int debugFlag = 0;
  312. int numReps = 100;
  313. char defaultFileName[] = "out";
  314. char *fileName = defaultFileName;
  315. Boolean exec = FALSE;
  316.  
  317. /*
  318.  * Define option descriptions.
  319.  */
  320. Tk_ArgvInfo argTable[] = {
  321.     {"-X", TK_ARGV_CONSTANT, (char *) 1, (char *) &debugFlag,
  322.         "Turn on debugging printfs"},
  323.     {"-N", TK_ARGV_INT, (char *) NULL, (char *) &numReps,
  324.         "Number of repetitions"},
  325.     {"-of", TK_ARGV_STRING, (char *) NULL, (char *) &fileName,
  326.         "Name of file for output"},
  327.     {"x", TK_ARGV_REST, (char *) NULL, (char *) &exec,
  328.         "File to exec, followed by any arguments (must be last argument)."},
  329.     {(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL,
  330.         (char *) NULL}
  331. };
  332.  
  333. main(argc, argv)
  334.     int argc;
  335.     char *argv[];
  336. {
  337.     \&...
  338.  
  339.     if (Tk_ParseArgv(interp, tkwin, &argc, argv, argTable, 0) != TCL_OK) {
  340.         fprintf(stderr, "%s\en", interp->result);
  341.         exit(1);
  342.     }
  343.  
  344.     /*
  345.      * Remainder of the program.
  346.      */
  347. }\fR
  348. .DE
  349. .PP
  350. Note that default values can be assigned to variables named in
  351. \fIargTable\fR:  the variables will only be overwritten if the
  352. particular arguments are present in \fIargv\fR.
  353. Here are some example command lines and their effects.
  354. .DS
  355. \fCprog -N 200 infile        # just sets the numReps variable to 200
  356. prog -of out200 infile     # sets fileName to reference "out200"
  357. prog -XN 10 infile        # sets the debug flag, also sets numReps\fR
  358. .DE
  359. In all of the above examples, \fIargc\fR will be set by \fBTk_ParseArgv\fR to 2,
  360. \fIargv\fR[0] will be ``prog'', \fIargv\fR[1] will be ``infile'',
  361. and \fIargv\fR[2] will be NULL.
  362.  
  363. .SH KEYWORDS
  364. arguments, command line, options
  365.