argparse4s: clean, powerful CLI handling for Scala
I needed a command line argument parsing library for Scala. Most of
the existing ones either didn’t support subcommands, had strange APIs,
had strange semi-standard parsing behavior, or weren’t very clean to use
from Scala. The best I’d found, JCommander and JewelCLI, both had their
own limitations, and JewelCLI at least was difficult to use from Scala
due to namespace collision with its @Option
annotation.
I found argparse4j,
though, and was quite taken with it. It is modelled after Python’s
argparse
module, which sets the bar in my book for power
and sanity in argument parsing APIs. It had all the power I was seeking
— subcommands, GNU-style option parsing, automatic help generation, etc.
— but the API wasn’t particularly fluent to use from Scala.
So I wrapped it up in a bit of Scala to produce argparse4s. There are
some code examples in the README; basically you just write down options
you want using flag
, option
, and
argument
methods, and then you can retrieve their values
using an implicit execution context handed to you by the outer parsing
and invocation logic.
It also makes use of type classes to automatically handle type
conversions and default metavariables, so if you have
option[String]("delimiter")
, it will have a default metavar
of “ARG” (or something like that), while an
option[File]('o', "output-file")
will have a default
metavar of “FILE”. It will also automatically handle type conversions
(hooking into argparse4j’s type conversion logic, and doing a few things
of its own). And, of course, you can define the type class for your own
types so that you can have options of any type you want.
The API winds up being very comfortable and declarative (although it does some non-declarative stuff under the hood to make everything work). There are few things to add, of course, but I’m quite satisfied with it.
I hope you like it!