Skip to content

Latest commit

 

History

History
99 lines (79 loc) · 2.6 KB

README.md

File metadata and controls

99 lines (79 loc) · 2.6 KB

scala-args-parser

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.**

Get Started

  1. 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"
  1. Import the classes
   import argsparser._
  1. Initialize the parser
   val parser = new Parser()
  1. Register an action
  parser register new Action(
    cmd = "hello",
    description = "print Hello World",
    task = {
      println("Hello World.")
    }
  )
  1. Parse your application arguments in the main method and execute the actions
    val (actions, _) = parser.parse(args)
    actions.foreach(_.execute)
  1. Compile and run you app sbt run hello

Examples

  1. 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)
  }
}
  1. 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)
  }
}
  1. The best examples are written as functional tests in the the Scenarios.scala file

  2. See also a good example in the gitmaster's repository.

  3. It can be also useful to check the Parser's unit tests.

Unit tests

Run the unit tests with sbt test