little.cli

package little.cli

Type members

Classlikes

sealed trait Application

Encapsulates components for providing command line interface.

Encapsulates components for providing command line interface.

See also
object Cli

Provides factory methods and other utilities.

Provides factory methods and other utilities.

import little.cli.{ *, given }
import Cli.{ application, option }

// Create application with supplied usage and options
val app = application("grep [ options ... ] <pattern> [ <fileName> ... ]",
 option("i", "ignore-case", false, "Perform case insensitive matching"),
 option("l", "files-with-matches", false, "Print file name only"),
 option("m", "max-count", true, "Stop reading file after 'num' matches").argName("num"),
 option("n", "line-number", false, "Include line number of match"),
 option("r", "recursive", false, "Recursively search subdirectories"),
 option("x", "exclude", true, "Exclude filename pattern from search").argName("pattern")
)

val args = Array("-ilr", "--exclude", "*.swp", "exception", "src/main/scala")

// Parse arguments
val cmd = app.parse(args)

cmd.hasOption("help") match
 case true  =>
   // Print help to System.out
   app.printHelp()
 case false =>
   // Get command arguments and pretend to do something
   val pattern  = cmd.getArg(0)
   val fileName = cmd.getArg(1)
   println(s"Searching for files with '$pattern' in $fileName directory...")
final class CommandLineExt(command: CommandLine) extends AnyVal

Adds extension methods to org.apache.commons.cli.CommandLine.

Adds extension methods to org.apache.commons.cli.CommandLine.

final class OptionExt(option: Option) extends AnyVal

Adds extension methods to org.apache.commons.cli.Option.

Adds extension methods to org.apache.commons.cli.Option.

final class OptionGroupExt(group: OptionGroup) extends AnyVal

Adds extension methods to org.apache.commons.cli.OptionGroup.

Adds extension methods to org.apache.commons.cli.OptionGroup.

final class OptionsExt(options: Options) extends AnyVal

Adds extension methods to org.apache.commons.cli.Options.

Adds extension methods to org.apache.commons.cli.Options.

trait ValueMapper[T]

Provides utility for mapping option values and arguments.

Provides utility for mapping option values and arguments.

import little.cli.{ *, given }
import Cli.{ application, option }

case class KeepAlive(idleTimeout: Int, maxRequests: Int)

// Define how to map value to KeepAlive
given ValueMapper[KeepAlive] =
 _.split(":") match
   case Array(timeout, max) => KeepAlive(timeout.toInt, max.toInt)

val app = application("start-server [ options ... ] port",
 option("d", "directory", true, "Location of public files directory"),
 option("k", "keep-alive", true, "Allow persistent connections").argName("timeout:max")
)

val cmd = app.parse("-d ./public_html --keep-alive 5:10 8080".split("\\s+"))

// Map keep-alive option
val keepAlive = cmd.mapOptionValue[KeepAlive]("keep-alive")

// Map directory option File
val directory = cmd.mapOptionValue[java.io.File]("directory")

// Map port argument via Int
val port = cmd.mapArg[Int](0)

Types

type Optionable = Option | OptionGroup

Option and OptionGroup can added to Options.

Option and OptionGroup can added to Options.

Givens

Givens

Maps value to File.

Maps value to File.

Maps value to Int.

Maps value to Int.

Maps value to Long.

Maps value to Long.

Maps value to Path.

Maps value to Path.