Overview
| Artifact ID: | a693330fa9d00328b6ff53e070fa142255586874 |
|---|---|
| Ticket: | 1446696fffffffffffffffffffffffffffffffff
Tcl_ParseArgv as a useful non-Tk version of Tk_ParseArgv |
| User & Date: | sdeasey on 2006-06-18 03:03:41 |
Changes
- icomment:
Logged In: YES user_id=87254 Hi Sam. I wrote the anonymous comment (sure I was logged in -- silly source forge) and tclobjv.c in naviserver. Yes, tclobjv.c parses both options and arguments, handling type conversion and defaults for both, and an optional 'args' to gather remaining arguments in. It does this with the Ns_ObjvArgs callback. There's lots of examples of it being used here: http://naviserver.cvs.sourceforge.net/naviserver/naviserver/nsd/tclsched.c?revision=1.5&view=markup You can parse the remaining arguments however you like. > (i) Does the order of options matter? No, which is the expected behaviour, I think. > (ii) Can options be interspersed with arguments? No. All options must come before arguments, which again I think is the expected behaviour. > (iii) What happens in the case of unknown arguments? Not sure what you mean here. If the args supplied don't match the signature, an error message is generated. If you expect an argument of an unusual type, you can use Ns_ObjvObj and the Tcl object will be returned, which you can interpret however you like. > (iv) Is there an automatically generated help? Error messages are automatically generated which descibe the command's signature. Perhaps the signature should be made available, so that commands could implement a -help switch (although this feels wrong to me for commands, fine for command lines). > (v) Do all arguments have to be known in advance? The number of remaining, unparsed arguments can be captured and dealt with however you like (see above). tclobjv.c is the fastest implementation I could come up with (standalone, not including the Tcl byte code compiler), which is important for Tcl commands, though probably not for application command lines. Some things which tclobjv.c does to make things fast are: no memory is allocated for parsing; uses GetIndexFromObj to shimmer switches to speed parsing in the future; splits option and arg parsing into two phases so that un-passed options can be skipped efficiently; direct jump to type conversion callbacks rather than giant switch statement. The last is obviously more felxible, too. The core Ns_ParseObjv() is only ~45 lines. The type conversion callbacks are often trivial wrappers. There's also a complete set of tests available: http://naviserver.cvs.sourceforge.net/naviserver/naviserver/tests/ns_parseargs.test?revision=1.3&view=markup
- login: "sdeasey"
- mimetype: "text/plain"
- username: "sdeasey"