I write a number of simple scala scripts that end up starting with a simple pattern match on args
like:
val Array(path, foo, whatever) = args
// .. rest of the script uses "path", "foo", etc.
Of course, if I supply the wrong number of arguments, I get an inscrutable error like:
scala.MatchError: [Ljava.lang.String;@7786df0f
at Main$$anon$1.<init>(FollowUsers.scala:5)
...
Is there an easy way to give a more useful error message? My current workaround is to do something like:
args match {
case Array(path, foo, whatever) => someFunction(path, foo, whatever)
case _ => System.err.println("usage: path foo whatever")
}
def someFunction(path: String, foo: String, whatever: String) = {
// .. rest of the script uses "path", "foo", etc.
}
But that feels like a lot of boilerplate what with having to define a whole other function, and having to repeat "path", "foo" and "whatever" in so many places. Is there a better way? I guess I could lose the function and put the body in the match statement, but that seems less readable to me.
I know I could use one of the many command line argument parsing packages, but I'm really looking for something extremely lightweight that I don't have to add a dependency and modify my classpath for.