Command line parsing for Scala applications.

Example

In Scala:

import argparse.default as ap

/** This is an example app. It shows how a command line interface can be
  * generated from various kinds of method parameters.
  *
  * @param server a sample named parameter
  * @param secure this is a flag
  * @param path a positional parameter
  */
@ap.command()
def main(
  server: String = "localhost",
  secure: Boolean = false,
  path: os.SubPath
): String =
  val scheme = if secure then "https" else "http"
  s"$scheme://$server/$path"

// boilerplate necessary until macro annotations become available in Scala 3
def main(args: Array[String]): Unit = argparse.main(this, args)

In a terminal:

$ app --server example.org --secure a/b/c
https://example.org/a/b/c

$ app
missing argument: path
run with '--help' for more information

$ app --help
Usage: [OPTIONS] PATH

This is an example app. It shows how a command line interface can be generated from various kinds of method parameters.

Options:
      --bash-completion string  generate bash completion for this command
      --help                    show this message and exit
      --secure                  this is a flag
      --server string           a sample named parameter

Highlights

  • Simple interfaces

    • High-level, annotation-based, CLI generator.

    • Lower-level interface inspired by the argparse package from Python.

  • Bash completion

    • Standalone bash completion for a super snappy user experience, even on the JVM.

    • Interactive bash completion for the most custom needs.

  • Works with Scala 3.2, on the JVM and Native (the lower-level interface also works with Scala 2.13)

  • Support for subcommands (aka "verbs")

Binaries

This library is published for Scala 3.2 and 2.13, for the JVM and Native. It is available on maven central under the coordinates:

  • mill: ivy"io.crashbox::argparse::0.20.0"

  • sbt: "io.crashbox" %%% "argparse" % "0.20.0"

Getting Help

ChannelLinks
ForumGitHub Discussions
Chatdiscord project chat
IssuesGitHub