Skip to main content
Version: 2.x

How to make an option optional or give it a default

An option built with a constructor such as Options.text is required: a command without it fails with Expected to find --user option.. This guide makes options optional, so the user can leave them out, and chooses what the program receives when they do.

You need the zio-cli library on your classpath (see Installation) and a command whose options you want to relax; Options describes how options are built.

Choose What a Missing Option Meansโ€‹

Pick the method by the value your program needs when the user leaves the option out:

The program needsUseValue type
To know whether the option was givenOptions#optionalOption[A]
A fixed valueOptions#withDefaultA
true or false from a flagOptions.boolean, nothing to addBoolean

Stepsโ€‹

We build the options of a serve command step by step: a flag, an option with a default, an optional option, and a pair of alternatives.

1. Use a Flag for a Yes-or-No Choiceโ€‹

A boolean option already has a default, the opposite of the value it gives when present. To declare a --quiet flag that is false unless given:

import zio.cli._

val quiet: Options[Boolean] = Options.boolean("quiet")

To invert a flag, pass ifPresent = false, as described in Boolean Options.

2. Give an Option a Defaultโ€‹

Options#withDefault uses its value when the option is missing. To default the number of retries to 3, convert the BigInt of Options.integer first and annotate the type:

val retries: Options[Int] = Options.integer("retries").map(_.toInt).withDefault(3)

The type annotation matters: without it, Options.integer("retries").withDefault(3) compiles as an Options[Any], as Defaults explains.

3. Make an Option Optionalโ€‹

Options#optional gives Some(value) when the option is given and None when it is missing. To accept an optional user name:

val user: Options[Option[String]] = Options.text("user").optional

4. Put a Default on Alternativesโ€‹

For two alternative options combined with Options#orElseEither or Options#|, put the default on the whole alternative, not on each option. To accept --port or --socket, with port 8080 when neither is given:

val endpoint: Options[Either[BigInt, String]] =
(Options.integer("port") orElseEither Options.text("socket")).withDefault(Left(BigInt(8080)))

An option with a default inside an alternative always reports a collision; see Choosing Between Options.

5. Combine the Options in a Commandโ€‹

Options#++ combines the options, and the command receives a flat tuple with one value per option, in the order they are combined. To build the command:

val serve: Command[(Boolean, Int, Option[String], Either[BigInt, String])] =
Command("serve", quiet ++ retries ++ user ++ endpoint)

6. Check the Helpโ€‹

The help of a single option with Options#withDefault ends with This setting is optional. Default: '<value>'., and the help of an option with Options#optional ends with This setting is optional.. Alternatives with a default get no such sentence. To print the help of the command:

println(serve.helpDoc.toPlaintext(color = false))
//
// OPTIONS
//
// --quiet
// A true or false value.
//
// This setting is optional. Default: 'false'.
//
// --retries <integer>
// An integer.
//
// This setting is optional. Default: '3'.
//
// --user <text>
// A user-defined piece of text.
//
// This setting is optional.
//
// --port <integer>
// An integer.
//
// --socket <text>
// A user-defined piece of text.
//
//

The usage line shows every optional option, including the alternatives with a default, in square brackets:

println(serve.synopsis.helpDoc.toPlaintext(color = false))
// serve [--quiet] [--retries <integer>] [--user <text>] [--port <integer>|--socket <text>]
//

What the Default Does Not Coverโ€‹

A default is used only when the option is missing; an invalid value such as --retries many still fails with many is not a integer., and the default itself is not validated (see Defaults).

Positional arguments have no counterpart of Options#withDefault or Options#optional. To make an argument optional, accept at most one value with Args#atMost(1), as described in Args.

Complete Exampleโ€‹

The complete program runs the serve command and prints the values it receives:

import zio._
import zio.cli._

object ServeApp extends ZIOCliDefault {
val quiet: Options[Boolean] = Options.boolean("quiet")
val retries: Options[Int] = Options.integer("retries").map(_.toInt).withDefault(3)
val user: Options[Option[String]] = Options.text("user").optional
val endpoint: Options[Either[BigInt, String]] =
(Options.integer("port") orElseEither Options.text("socket")).withDefault(Left(BigInt(8080)))

val serve = Command("serve", quiet ++ retries ++ user ++ endpoint)

val cliApp = CliApp.make("serve", "1.0.0", HelpDoc.Span.text("Starts the server."), serve) {
case (quiet, retries, user, endpoint) =>
Console.printLine(s"quiet=$quiet retries=$retries user=$user endpoint=$endpoint")
}
}

Run without options, the program prints quiet=false retries=3 user=None endpoint=Left(8080).

See Alsoโ€‹

ZIO Chat

Hi, fellow coder! ๐ŸŒŸ Welcome to your ZIO adventure with the ZIO Chat Bot. ๐Ÿš€ Let's master functional Scala together. From referential transparency to concurrent prowess, I've got your back. Ready to dive in? Your questions, my insights โ€“ let's conquer ZIO! ๐Ÿ’ก๐Ÿค–