Skip to content

francoiscabrol/scala-args-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Command line arguments parser library in scala

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages