![]() ![]() |
![]() |
![]() A Standard Method of Passing Parameters in AMLPassing parameters to AML programs using &args is fairly straightforward. The typical AML programmer will write some ad-hoc code to handle passed parameters when needed, resulting in functional but non-standard parameter handling over a number of programs. I developed a standard method for passing parameters that is easy and flexible, and requires only a few lines of code. By including the following fragment at the top of an AML file, you can pass any number of parameters to the program in a logical and consistent manner. I recommend using this methodology for both programs called by users as well as for AMLs that act as subroutines for other AMLs. Since the variables can be named on the command line, it is very clear what value is being assigned to what variable, and the process is, in some respects, self-documenting.
The above code should be placed at the top of your AML; the &args line must be the first AML statement in your program, though comments and blank lines may precede it. How it worksThe &args opts:rest statement takes all parameters that were passed to your AML and puts them into the variable opts. (You could add some other variable names between the &args command and the opts:rest if you wanted to pass required values without using named parameters, as shown in the example below.) The main bit of code processes all parameters that were passed to the program, and sets program variables accordingly. If a particular option was not specified, the associated variable will retain the default value assigned to it. Parameter syntaxParameters can be specified in one of two ways:
Any number of parameters may be passed in this manner, but should be separated by at least one space. Strings containing spaces should be surrounded by single quotes. Parameters can be passed in any order. Example
Arc: &r sample tiger shape=line quiet string='Two words' will produce the following output: mainoption = tiger and Arc: &r sample habitat fastmode quiet number=15 string=OneWord will produce the following output: mainoption = habitat As you can see, it is easy to mix required and optional parameters and switches, and this technique provides a simple way to set default values. By using this code in all your AMLs that accept passed parameters, you will make your programs more consistent and easier to understand.
Entire site © 1996-2004 by Christopher Eykamp |