[−][src]Function molt::check_args
pub fn check_args(
namec: usize,
argv: &[Value],
min: usize,
max: usize,
argsig: &str
) -> MoltResult
This function is used in command functions to check whether the command's argument
list is of a proper size for the given command. If it is, check_args
returns
the empty result; if not, it returns a Molt error message
wrong # args: should be "syntax..."
, where syntax is the command's syntax.
It is typically called at the beginning of a command function.
The argv is the argument list, including the command name.
The namec is the number of tokens in the argument that constitute the command name. It is usually 1, but would be 2 for a command with subcommands. The error message will take those tokens verbatim from argv.
min and max are the minimum and maximum valid length for argv. If max is zero, the command takes an arbitrary number of arguments (but at least min).
argsig is the argument signature, to be appended to the command name for inclusion in the error message.
Example
Here are a couple of examples from the Molt code base. The relevant commands are documented in the Molt Book.
First, here is the call from the definition of the set
command, which has the signature
set varName ?newValue?
. In TCL command signatures, question marks denote optional
values. The first argument is the command name, and the argv array must be at least 2
arguments in length but no more than 3.
check_args(1, argv, 2, 3, "varName ?newValue?")?;
Next, here the call from the definition of the append
command, which appends strings to
the content of a variable. It has signature append varName ?value value ...?
. The first
argument is the command name, and
the second is the variable name to which data will be appended. The remaining arguments
are string values to append; the question marks indicate that they are optional, and the
ellipsis indicates that there can be any number of them.
check_args(1, argv, 2, 0, "varName ?value value ...?")?;