What's the best way to parse command-line parameters in Scala? I personally prefer something lightweight that does not require external jar.
Related:
What's the best way to parse command-line parameters in Scala? I personally prefer something lightweight that does not require external jar.
Related:
I've just found an extensive command line parsing library in scalac's scala.tools.cmd package.
See http://www.assembla.com/code/scala-eclipse-toolchain/git/nodes/src/compiler/scala/tools/cmd?rev=f59940622e32384b1e08939effd24e924a8ba8db
This is what I cooked. It returns a tuple of a map and a list. List is for input, like input file names. Map is for switches/options.
will return
Switches can be "--t" which x will be set to true, or "--x 10" which x will be set to "10". Everything else will end up in list.
I realize that the question was asked some time ago, but I thought it might help some people, who are googling around (like me), and hit this page.
Scallop looks quite promising as well.
Features (quote from the linked github page):
And some example code (also from that Github page):
I'm going to pile on. I solved this with a simple line of code. My command line arguments look like this:
This creates an array via Scala's native command line functionality (from either App or a main method):
I can then use this line to parse out the default args array:
Which creates a map with names associated with the command line values:
I can then access the values of named parameters in my code and the order they appear on the command line is no longer relevant. I realize this is fairly simple and doesn't have all the advanced functionality mentioned above but seems to be sufficient in most cases, only needs one line of code, and doesn't involve external dependencies.
Here's a scala command line parser that is easy to use. It automatically formats help text, and it converts switch arguments to your desired type. Both short POSIX, and long GNU style switches are supported. Supports switches with required arguments, optional arguments, and multiple value arguments. You can even specify the finite list of acceptable values for a particular switch. Long switch names can be abbreviated on the command line for convenience. Similar to the option parser in the Ruby standard library.
scala-optparse-applicative
I think scala-optparse-applicative is the most functional command line parser library in Scala.
https://github.com/bmjames/scala-optparse-applicative