Tcl_ParseArgsObjv(3tcl)
π Tcl_ParseArgsObjv π
okay bestie, welcome to the Tcl Library Procedures glow-up, this is the page for Tcl_ParseArgsObjv and it is literally the thing that makes -someName someValue style arguments happen β¨
πΈ NAME πΈ
Tcl_ParseArgsObjv β parse arguments according to a tabular description π
β¨ SYNOPSIS β¨
#include <tcl.h>
int
Tcl_ParseArgsObjv(interp, argTable, objcPtr, objv, remObjv)
π ARGUMENTS π
Tcl_Interp *interp(out) β where to store error messages. that's it, that's the job π«Άconst Tcl_ArgvInfo *argTable(in) β pointer to the array of option descriptors.Tcl_Size | int *objcPtr(in/out) β a pointer to a variable holding the number of arguments inobjv. it gets modified to hold the number of arguments left in the unprocessed argument list stored inremObjv. it may be(Tcl_Size *)NULLwhen it's not used. if it points to a variable whose type is notTcl_Size, a compiler warning will be generated. and if your extension is compiled with-DTCL_8_API, this function will returnNULLfor argument lists with more thanINT_MAXelements (which should trigger proper error-handling), otherwise expect it to crash ππTcl_Obj *const *objv(in) β the array of arguments to be parsed.Tcl_Obj ***remObjv(out) β pointer to a variable that will hold the array of unprocessed arguments. should beNULLif no return of unprocessed arguments is required. ifobjcPtris updated to a non-zero value, the array returned through this must be deallocated usingTcl_Free. no cap, free your memory π§
π DESCRIPTION π
Tcl_ParseArgsObjv provides a whole system for parsing argument lists shaped like "-someName someValue ...". this vibe is everywhere β in the arguments to a program and in the arguments to an individual Tcl command. this parser assumes the order of the arguments does not matter, except in the way that later copies of a duplicated option override earlier ones. so the last one wins, periodt π
the argument array is described by the objcPtr and objv parameters, and an array of unprocessed arguments is returned through the objcPtr and remObjv parameters. if no return of unprocessed arguments is desired, the remObjv parameter should be NULL.
if any problems happen β including if the "generate help" option is selected β an error message is left in the interpreter result and TCL_ERROR is returned. otherwise, the interpreter result is left unchanged and TCL_OK is returned. iconic behavior, no drama β¨
the collection of arguments to be parsed is described by the argTable parameter. this points to a table of descriptor structures that is terminated by an entry with the type field set to TCL_ARGV_END.
as convenience, the following prototypical entries are provided:
TCL_ARGV_AUTO_HELPβ enables the argument processor to provide help when passed the argument"-help". bestie said "read the docs" πTCL_ARGV_AUTO_RESTβ instructs the argument processor that arguments after"--"are to be unprocessed.TCL_ARGV_TABLE_ENDβ marks the end of the table of argument descriptors.
π¦ ARGUMENT DESCRIPTOR ENTRIES π¦
each entry of the argument descriptor table must be a structure of type Tcl_ArgvInfo. the structure is defined as this:
typedef struct {
int type;
const char *keyStr;
void *srcPtr;
void *dstPtr;
const char *helpStr;
void *clientData;
} Tcl_ArgvInfo;
the keyStr field contains the name of the option; by convention, this will normally begin with a "-" character. the type, srcPtr, dstPtr and clientData fields describe the interpretation of the value of the argument, as described below. the helpStr field gives some text that is used to provide help to users when they request it. π«Ά
as noted above, the type field is used to describe the interpretation of the argument's value. the following values are acceptable values for type:
TCL_ARGV_CONSTANTβ the argument does not take any following value argument. if this argument is present, the (integer) value of thesrcPtrfield is copied to the variable pointed to by thedstPtrfield. theclientDatafield is ignored. it just shows up and drops a number, main character energy πTCL_ARGV_ENDβ this value marks the end of all option descriptors in the table. all other fields are ignored. that's the β¨ the end β¨TCL_ARGV_FLOATβ this argument takes a following floating point value argument. the value (once parsed byTcl_GetDoubleFromObj) will be stored as a double-precision value in the variable pointed to by thedstPtrfield. thesrcPtrandclientDatafields are ignored.TCL_ARGV_FUNCβ this argument optionally takes a following value argument; it is up to the handler callback function passed insrcPtrto decide. that function will have the following signature:
typedef int (Tcl_ArgvFuncProc)(
void *clientData,
Tcl_Obj *objPtr,
void *dstPtr);
the result is a boolean value indicating whether to consume the following argument. the clientData is the value from the table entry, the objPtr is the value that represents the following argument or NULL if there are no following arguments at all, and the dstPtr argument to the Tcl_ArgvFuncProc is the location to write the parsed value to.
TCL_ARGV_GENFUNCβ this argument takes zero or more following arguments; the handler callback function passed insrcPtrreturns how many (or a negative number to signal an error, in which case it should also set the interpreter result). the function will have the following signature:
typedef Tcl_Size (Tcl_ArgvGenFuncProc)(
void *clientData,
Tcl_Interp *interp,
Tcl_Size objc,
Tcl_Obj *const *objv,
void *dstPtr);
the clientData is the value from the table entry, the interp is where to store any error messages, objc and objv describe an array of all the remaining arguments, and the dstPtr argument to the Tcl_ArgvGenFuncProc is the location to write the parsed value (or values) to. go off, callback π₯
TCL_ARGV_HELPβ this special argument does not take any following value argument, but instead causesTcl_ParseArgsObjvto generate an error message describing the arguments supported. all other fields except thehelpStrfield are ignored. yes bestie, "help" is technically an error, that's just how it is πTCL_ARGV_INTβ this argument takes a following integer value argument. the value (once parsed byTcl_GetIntFromObj) will be stored as anintin the variable pointed to by thedstPtrfield. thesrcPtrfield is ignored.TCL_ARGV_RESTβ this special argument does not take any following value argument, but instead marks all following arguments to be left unprocessed. thesrcPtr,dstPtrandclientDatafields are ignored.TCL_ARGV_STRINGβ this argument takes a following string value argument. a pointer to the string will be stored atdstPtr; the string inside will have a lifetime linked to the lifetime of the string representation of the argument value that it came from, and so should be copied if it needs to be retained. thesrcPtrandclientDatafields are ignored.
β οΈ girlypop, listen to the TCL_ARGV_STRING note. that pointer is on a timer. if you need it longer, copy it. no borrowing forever, we don't do that here π
π REFERENCE COUNT MANAGEMENT π
the values in the objv argument to Tcl_ParseArgsObjv will not have their reference counts modified by this function. the interpreter result may be modified on error; the values passed should not be the interpreter result with no further reference added. stay safe out there, handle your refcounts π«Ά
π SEE ALSO π
π KEYWORDS π
argument, parse
okay that's the whole page, you're officially a Tcl_ParseArgsObjv girlie now π this was the Tcl 8.6 era of the manual, and honestly? she ate. go parse something with a cute little argTable and text me when it works, mwah ππ¦β¨
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2008 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms