Swift Function reference
This section details functions that are available for use in the Swift language.
Takes a command line parameter name as a string parameter and an optional default value and returns the value of that string parameter from the command line. If no default value is specified and the command line parameter is missing, an error is generated. If a default value is specified and the command line parameter is missing, @arg will return the default value.
Command line parameters recognized by @arg begin with exactly one hyphen and need to be positioned after the script name.
extractInt(file) will read the specified file, parse an integer from the file contents and return that integer.
Similar to extractInt, extractFloat(file) will read the specified file, parse a float from the file contents and return that float.
filename(v) will return a string containing the filename(s) for the file(s) mapped to the variable v. When more than one filename is returned, the filenames will be space separated inside a single string return value.
filenames(v) will return multiple values containing the filename(s) for the file(s) mapped to the variable v.
length(array) will return the length of an array in Swift. This function will wait for all elements in the array to be written before returning the length.
readData will read data from a specified file and assign it to Swift variable. The format of the input file is controlled by the type of the return value. For scalar return types, such as int, the specified file should contain a single value of that type. For arrays of scalars, the specified file should contain one value per line. For complex types of scalars, the file should contain two rows. The first row should be structure member names separated by whitespace. The second row should be the corresponding values for each structure member, separated by whitespace, in the same order as the header row. For arrays of structs, the file should contain a heading row listing structure member names separated by whitespace. There should be one row for each element of the array, with structure member elements listed in the same order as the header row and separated by whitespace. The following example shows how readData() can be used to populate an array of Swift struct-like complex type:
Where the contents of the "emps.txt" file are:
This will result in the array "emps" with 3 members. This can be processed within a Swift script using the foreach construct as follows: