lhns / scala-commandline   0.4.0

Apache License 2.0 GitHub

Scala command line parser

Scala versions: 3.x 2.13 2.12
Scala.js versions: 1.x

scala-commandline

build Maven Central Apache License 2.0 Scala Steward badge

A small state-monad-based command line parser library written in scala.

build.sbt

libraryDependencies += "de.lhns" %% "scala-commandline" % "0.4.0"

Example

case class Options(help: Boolean,
                   version: Boolean,
                   logLevel: Option[String],
                   decode: Boolean,
                   validate: Boolean,
                   params: Seq[String])

def main(args: Array[String]): Unit = {
  val options: Options = CommandLine(args) {
    for {
      default <- CommandLine.defaultOpts()
      /* shorthand for:
      empty <- CommandLine.isEmpty
      help <- CommandLine.opt("-h", "--help").flag
      version <- CommandLine.opt("--version").flag
      */
      logLevel <- CommandLine.opt("--loglevel").arg.map(_.lastOption)
      decode <- CommandLine.opt("-d", "--decode").flag
      validate <- CommandLine.opt("--validate").flag
      _ <- CommandLine.errorOnUnrecognizedOpts()
      params <- CommandLine.args
    } yield Options(
      help = default.empty || default.help,
      version = default.version,
      logLevel = logLevel,
      decode = decode,
      validate = validate,
      params = params
    )
  }
}

License

This project uses the Apache 2.0 License. See the file called LICENSE.