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:
As everyone posted it's own solution here is mine, cause I wanted something easier to write for the user : https://gist.github.com/gwenzek/78355526e476e08bb34d
The gist contains a code file, plus a test file and a short example copied here:
There is not fancy options to force a variable to be in some bounds, cause I don't feel that the parser is the best place to do so.
Note : you can have as much alias as you want for a given variable.
scopt/scopt
The above generates the following usage text:
This is what I currently use. Clean usage without too much baggage. (Disclaimer: I now maintain this project)
I like the clean look of this code... gleaned from a discussion here: http://www.scala-lang.org/old/node/4380
I based my approach on the top answer (from dave4420), and tried to improve it by making it more general-purpose.
It returns a
Map[String,String]
of all command line parameters You can query this for the specific parameters you want (eg using.contains
) or convert the values into the types you want (eg usingtoInt
).Example:
Gives:
I've attempted generalize @pjotrp's solution by taking in a list of required positional key symbols, a map of flag -> key symbol and default options:
I am from Java world, I like args4j because its simple, specification is more readable( thanks to annotations) and produces nicely formatted output.
Here is my example snippet:
Specification
Parse
On invalid arguments