Command line arguments parser
This library is a snapshot but a release is coming. Currently, I support the JVM. But I did some tests and it can be compiled for Scala-native, The only thing to do is to configure the sbt-crossproject things.**
- Add the library in the build.sbt
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
libraryDependencies += "io.github.francoiscabrol" %%% "scala-args-parser" % "0.1-SNAPSHOT"
- Import the classes
import argsparser._
- Initialize the parser
val parser = new Parser()
- Register an action
parser register new Action(
cmd = "hello",
description = "print Hello World",
task = {
println("Hello World.")
}
)
- Parse your application arguments in the main method and execute the actions
val (actions, _) = parser.parse(args)
actions.foreach(_.execute)
- Compile and run you app
sbt run hello
- Simple app with a simple action (
sbt run hello
) See it running on Scastie
object SimpleApp {
val parser = new Parser()
parser register new Action(
cmd = "hello",
description = "print Hello World",
task = {
println("Hello World.")
}
)
def main(args: Array[String]) {
val (actions, _) = parser.parse(args)
actions.foreach(_.execute)
}
}
- Simple app with a parameters (
sbt run doAction --word World
)
object SimpleApp {
val parser = new Parser()
val P1 = parser register new Param[String](
cmd = "--word",
description = "word to print",
defaultValue = "Not specified"
)
parser register new Action(
cmd = "doAction",
description = "print Hello World",
task = {
println(s"Hello " + P1.value + ".")
}
)
def main(args: Array[String]) {
val (actions, _) = parser.parse(args)
actions.foreach(_.execute)
}
}
-
The best examples are written as functional tests in the the Scenarios.scala file
-
See also a good example in the gitmaster's repository.
-
It can be also useful to check the Parser's unit tests.
Run the unit tests with sbt test