Site Tools


arglist

Argument Lists

Argument lists are an optional way to automatically shift values from $* into local variables. You can use argument lists in your aliases or your ons. The fix_arglist function also handles arglists.

Behavior of Argument Lists

When an alias or on is called, the magic variable $* is set with the arguments passed into your code. Usually the first thing most people do is copy arguments from $* into local variables, such as:

alias oofda {
	local var1 $0;
	local var2 $1;
	local var3 $2-;
	echo $var1 and $var2 and $var3!;
};

But wouldn't it be easier if you could just have the client automatically assign the values for you? Argument lists are how you do this

alias oofda (var1, var2, var3) {
	echo $var1 and $var2 and $var3!;
};

or for ons,

on ^join * (nick, chan, userhost, extra) {
	xecho -b $nick!$userhost has joined $chan $extra;
};

Ordinarily, the whole of $* is put into the arguments; anything left in $* is put into the final variable. Thus, $* is usually the empty string in an alias or on that uses argument lists. You can overrule this.

Syntax of Argument Lists

An argument list is a comma separated list of terms, surrounded in parenthesis. Each term should be a variable, with an optional number of words specification, with an optional default value. The final term must be either a regular variable term, or the literal string “void”, or the literal string “…”.

Syntax Meaning
varname Assign the next word to $varname
varname words N Assign the next N words to $varname
varname uwords N Assign the next N uwords to $varname
varname qwords N Assign the next N qwords to $varname
varname dwords N Assign the next N dwords to $varname
varname default “val Assign the next word to $varname, but use the value val if an argument wasn't provided.
(Final argument only) The remainder of the arguments (if any) should be assigned to the $* magic expando.
void (Final argument only) The remainder of the arguments (if any) should be discarded.
  • The default word-specification is “words 1”
  • The default default-value is the empty string (“”)
  • If you don't provide any argument list, (…) is the default.
Word Type Definition
words
uwords
qwords
dwords

Semantics of Argument Lists

When you call an alias that has an argument list, each term of the argument list is created as a local variable, and its initial value is set to $0 (or however many words you specified), and those word(s) are removed from $*.

If $* runs out before the argument list does, then any remaining arguments receive their default values (including the default-default value of “”)

The last argument in the list is either a variable, the literal string '…', or the literal string 'void'. The remainder of $* is either moved to the variable, kept in $*, or discarded entirely, respectively.

Naturally, argument lists change $*, and there is no way to recover the original value of $* in an alias (or on) that uses arglists.

History:

arglist.txt · Last modified: 2007/02/27 06:02 by 127.0.0.1