I would like to create a generic Scala script template (using Conscript). The script should take command-line options, for example a flag "-v" to enable verbose mode.
In say a Perl script, I would create a global variable $verbose, which I would set to true or false (or rather 1 or 0) depending if the flag is present. I understand that setting global variables in a script is not the Scala way.
Consider the following example:
object Main {
def main(args: Array[String]) {
val options = parseOptions(args); // some way of parsing
// how to use 'options' in the remainder of the script?
}
}
How to do it? One way I could see is to keep the option value as context variables in a closure, i.e., access them from nested functions. However, the problem with using nested functions is that I can't write unit tests for them. Another straightforward way would be to pass the options along with every function call, but that seems unwieldy.
Any suggestions?