Skip to content

xuwei-k/optparse-applicative

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0dbe028 · Mar 7, 2025
Jan 9, 2025
Aug 26, 2024
Jun 20, 2020
Mar 4, 2025
Dec 8, 2016
Mar 7, 2025
Apr 2, 2018
Apr 2, 2018
Apr 14, 2024
Jan 28, 2025
Apr 14, 2024

Repository files navigation

scala-optparse-applicative

scaladoc

A port of the optparse-applicative library to the Scala programming language.

Most functionality has been ported, except completion.

This library depends on Scalaz for functional data structures, type classes and combinators.

How to get it

for jvm

libraryDependencies += "com.github.xuwei-k" %% "optparse-applicative" % "0.9.4"

for scala-js, scala-native

libraryDependencies += "com.github.xuwei-k" %%% "optparse-applicative" % "0.9.4"

License

This library is distributed under a BSD 3-Clause license (see LICENSE).

Simple example

This example follows the one from the optparse-applicative docs.

case class Sample(hello: String, quiet: Boolean)

object SampleMain {

  val sample: Parser[Sample] =
    ^(
      strOption(long("hello"), metavar("TARGET"), help("Target for the greeting")),
      switch(long("quiet"), help("Whether to be quiet"))
    )(Sample.apply)

  def greet(s: Sample): Unit = s match {
    case Sample(h, false) => println("Hello, " ++ h)
    case _ =>
  }

  def main(args: Array[String]): Unit = {
    val opts = info(sample <*> helper,
      progDesc("Print a greeting for TARGET"),
      header("hello - a test for scala-optparse-applicative"))
    greet(execParser(args, "SampleMain", opts))
  }

}

When run with the --help option, it prints:

hello - a test for scala-optparse-applicative

Usage: SampleMain --hello TARGET [--quiet]
  Print a greeting for TARGET

Available options:
  -h,--help                Show this help text
  --hello TARGET           Target for the greeting
  --quiet                  Whether to be quiet

Scalaz 7.2.x

https://github.com/xuwei-k/optparse-applicative/tree/0.8.x