Accurately checking the presence of parameters and keywords

This section is concerned with examining the problem of checking inside a routine, what exactly was present of the command line typed by the user, as this often determines the behavior of the routine. You will have to do that for instance to implement an on-line help when no parameter is given (with the n_params() function).

More generally, the presence of parameters and keywords as well as their content will often drive the behavior of your routines and functions. The number of parameters present on the command line is easily checked with:

number_of_par = n_params() This however, says nothing about keywords which are of a different nature (and are named, i.e. appear on the command line as key1=variable). One must distinguish three types of keyword: input keywords that provide a variable to the routine, boolean (i.e. yes/no, true/false) keywords, and output keywords that will be used to retrieve results from the routine.

Input keywords

In that case, the keyword is used to provide an information to the routine, and therefore, the variable it contains must exist (it does not make sense to provide inexistent information). In general then, the instruction keyword_set() will give you satisfactory results. For instance, if the input keyword key1was present on the command line, keyword_set(key1) is true.

Well it is true only if the variable you provide is non-zero. If the value is 0 then keyword_set(key1) is false... So you have to use a first trick to be able to send keywords with null values to your routine and check that they are there (i.e. differentiate between an absent keyword and one with a zero value).

If you want to set a keyword to 0, then the test you have to make is not that the keyword is set, but that it actually contains something. A simple way to do that is to use the function n_elements(key1). In that case, whatever the content of key1, it will have at least one element (the value you provide), and thus you are now able to know that a keyword is set, even if the value it is set to is 0.

Boolean keywords

Boolean keyword are true/false keywords. Either they appear on the command line with a syntax as /key1 or they don't. In their case therefore, the function keyword_set(key1) has a satifactory behavior. Indeed, it would not make sense here to set the keyword to a zero value!

Output keywords

Output keywords are a very special kind of keyword. In general you will use output keywords to retrieve some computational results produced inside your procedure. Almost by definition then, the variable that you set in front of the keyword (in the calling statement key1=variable) does not exist yet (i.e. it is a new one). In that case, the statement keyword_set(key1) is false and the result of n_elements(key1) is 0, i.e. both previous methods are unable to tell you wether the keyword was present on the calling statement or not.

Hopefully there is still a way to do that: with the statement arg_present(key1). This statement is true if the calling statement contains the keyword, whatever the value it has taken (0 or not, existing variable or not).

In fact, the arg_present() function can be used instead of the n_elements() function in the case of input keywords. It can also be used to check the presence of parameters on the calling line, with a higher accuracy than the n_params() function that simply counts them.