Checking args with new main function syntax

Viewed 126

When using new main function syntax:

@main def prog(nb: Int, stuff: String) = ...

vs old one :

def main(args: Array[String]) = ...

Is it possible to check the number of arguments passed on the command line? Something we can do with a test on args.length in old syntax.

I would like to produce a customized syntax error instead of "Illegal command line: more arguments expected".

1 Answers

As demonstrated here, the final argument of the method attached to @main should be type String* (or, I suppose, any other vararg type) if you want to allow for a variable number of arguments on the command line.

@main def prog(c:Char, n:Int, rest:String*) =
   println(s"$c:$n and ${rest.length} other args")
Related