Gnocl Cookbook‎ > ‎

TCL C-Api, issues with Tcl_WrongNumArgs

When the gnocl interpreter is parsing commands and subcommands the Tcl C-API can use the Tcl_WrongNumArgs function to determine the next action. The definition of this function is given as:

Tcl_WrongNumArgs(interp, objc, objv, message);

Where:
Tcl_Interp interp
Interpreter in which error will be reported: error message gets stored in its result value.

int objc
Number of leading arguments from objv to include in error message.

Tcl_Obj *const objv[]
Arguments to command that had the wrong number of arguments.

const char *message
Additional error information to print after leading arguments from objv. This typically gives the acceptable syntax of the command. This argument may be NULL.

So, if a command needed a total 2 or 4 arguments to be valid, then a suitable error message might me

if ( objc != 2 && objc != 4 ) {
    Tcl_WrongNumArgs ( interp, 2, objv, "startPos|keyWord endPos|keyWord" );
    return TCL_ERROR;
}

But, this is not guareteed to work, but using Tcl_AddErrorInfo with a NULL string force the output of the message needed.

if ( objc != 2 && objc != 4 ) {
    Tcl_WrongNumArgs ( interp, 2, objv, "startPos|keyWord endPos|keyWord" );
    Tcl_AddErrorInfo ( interp, NULL );
    return TCL_ERROR;
}

Comments