From: SourceForge.net <no...@so...> - 2005-02-15 08:07:35
|
Feature Requests item #1122940, was opened at 2005-02-15 01:05 Message generated for change (Comment added) made by sdeasey You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=719009&aid=1122940&group_id=130646 Category: C-API Group: None Status: Open Resolution: None Priority: 5 Submitted By: Stephen Deasey (sdeasey) Assigned to: Stephen Deasey (sdeasey) Summary: Tcl command switch parsing routines Initial Comment: Parsing the switches and arguments for a Tcl command can be a real pain: it's very easy to make off-by-one errors, and it often involves a lot of repetitive typing which obscures the real intent of the command. The attached patch provides the routine Ns_ParseObjv. It parses switches and arguments, checks types, and generates error/usage messages automatically. Switches are handled efficiently via indexed lookups. The intent is not only to reduce the number of errors in switch handling code but to make switch handling such an easy task that developers are more likely to design easy to use, tcl-friendly APIs rather than APIs that are convenient to implement. ---------------------------------------------------------------------- >Comment By: Stephen Deasey (sdeasey) Date: 2005-02-15 01:07 Message: Logged In: YES user_id=87254 Here's an example: int NsTclCacheCreateObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { Ns_Cache *cache; Ns_DString ds; int scope = CACHE_SERVER; int size = 0, objsize = 0, entries = 0, timeout = 0; char *name = NULL; Ns_ObjvTable scopes[] = { {"global", CACHE_GLOBAL}, {"server", CACHE_SERVER}, {"interp", CACHE_INTERP}, NULL, NULL }; Ns_ObjvSpec opts[] = { {"-scope", Ns_ObjvIndex, &scope, &scopes}, {"-size", Ns_ObjvInt, &size, NULL}, {"-objsize", Ns_ObjvInt, &objsize, NULL}, {"-entries", Ns_ObjvInt, &entries, NULL}, {"-timeout", Ns_ObjvInt, &timeout, NULL}, {"--", Ns_ObjvBreak, NULL, NULL}, {NULL, NULL, NULL, NULL} }; Ns_ObjvSpec args[] = { {"cache", Ns_ObjvString, &name, NULL}, {NULL, NULL, NULL, NULL} }; if (Ns_ParseObjv(opts, args, interp, 1, objc, objv) != NS_OK) { return TCL_ERROR; } /* . . . */ return TCL_OK; } ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=719009&aid=1122940&group_id=130646 |