Skip to main content
Version: 2.x

CliApp and Running a CLI

A CliApp[R, E, A] is a complete command-line application. It holds the program's name, version and summary, the Command that describes what the user may type, and the function that runs once the input is parsed. It needs an environment R, can fail with an error of type E, and produces a value of type A.

We build a CliApp with CliApp.make, run it with CliApp#run, and, for a stand-alone program, wrap it in ZIOCliDefault:

sealed trait CliApp[-R, +E, +A] {
def run(args: List[String]): ZIO[R, CliError[E], Option[A]]
def config(newConfig: CliConfig): CliApp[R, E, A]
def map[B](f: A => B): CliApp[R, E, B]
def flatMap[R1 <: R, E1 >: E, B](f: A => ZIO[R1, E1, B]): CliApp[R1, E1, B]
def footer(newFooter: HelpDoc): CliApp[R, E, A]
def summary(s: HelpDoc.Span): CliApp[R, E, A]
}

Building a CliApp With CliApp.makeโ€‹

CliApp.make takes the description of the app in its first parameter list and the function that runs the command in its second:

object CliApp {
def make[R, E, Model, A](
name: String,
version: String,
summary: HelpDoc.Span,
command: Command[Model],
footer: HelpDoc = HelpDoc.Empty,
config: CliConfig = CliConfig.default,
figFont: FigFont = FigFont.Default
)(execute: Model => ZIO[R, E, A]): CliApp[R, E, A]
}

The following app greets the name given with --name:

import zio._
import zio.cli._
import zio.cli.HelpDoc.Span.text

val nameOpt: Options[String] = Options.text("name").withDefault("world") ?? "Who to greet."
val greet: Command[String] = Command("greet", nameOpt).withHelp("Prints a greeting.")

val greetApp: CliApp[Any, java.io.IOException, Unit] = CliApp.make(
name = "greet",
version = "1.0.0",
summary = text("Prints a greeting"),
command = greet,
footer = HelpDoc.p("Example footer text.")
)(name => Console.printLine(s"Hello, $name!"))

Each parameter of CliApp.make has the following meaning:

ParameterTypeDefaultWhat it does
nameStringnoneThe program name. It is drawn as the banner and starts the header line of the help; the wizard header also contains it.
versionStringnoneThe version string, shown after the name in the header line.
summaryHelpDoc.SpannoneA one-line description, shown after -- in the header line.
commandCommand[Model]noneThe grammar of valid input. The value it parses is the Model passed to execute. See Commands.
footerHelpDocHelpDoc.EmptyText printed at the end of the help.
configCliConfigCliConfig.defaultParsing and help settings, such as case sensitivity and auto-correction. See CLI Configuration.
figFontFigFontFigFont.DefaultThe font used to draw the name as a banner. See Figlet banners.
executeModel => ZIO[R, E, A]noneThe second parameter list. It runs with the parsed value of the command.

The Help Screenโ€‹

--help prints the banner, a header line with the name, version and summary, the USAGE section, the help of the command and the footer. For greetApp, the help is:

__
____ _________ ___ / /_
/ __ `/ ___/ _ \/ _ \/ __/
/ /_/ / / / __/ __/ /_
\__, /_/ \___/\___/\__/
/____/


greet 1.0.0 -- Prints a greeting

USAGE

$ greet [--name <text>]

DESCRIPTION

Prints a greeting.

OPTIONS

--name <text>
A user-defined piece of text.

Who to greet.

This setting is optional. Default: 'world'.

Example footer text.

The built-in options themselves, such as --help, are not listed. See Built-in Commands for all of them.

Running a CliApp With CliApp#runโ€‹

CliApp#run takes the command-line words as a List[String], without the program name, and returns ZIO[R, CliError[E], Option[A]]:

object GreetMain extends ZIOAppDefault {
def run =
greetApp.run(List("--name", "Ada"))
}

Before parsing, CliApp#run puts the name of the top-level command in front of the words, so the user never types it. For a command with subcommands, it adds the parent's name, and the user starts with the subcommand name. For a top-level command built with Command#|, it adds nothing, and the user types the name of one alternative first.

The result depends on the input:

InputWhat CliApp#run doesResult
A valid commandRuns execute with the parsed value.Some(a), or CliError.Execution(e) on failure
--help or -hPrints the help.None
A parent command with no subcommandPrints the parent's help.None
--shell-completion-script or --shell-completion-indexPrints the completion script or the completions.None
--wizardAsks for the values, then runs the command they form.The result of that run, or None on quit
Invalid inputPrints the error message to standard output.Fails with CliError.Parsing

Words left over after a complete command are ignored, so greet --name Ada extra still greets Ada. The error type CliError[E] has four cases, Parsing, Execution, IO and BuiltIn, described in Validation and Errors.

Running a Stand-alone Program With ZIOCliDefaultโ€‹

ZIOCli is a ZIOApp whose ZIOApp#run reads the program arguments and passes them to cliApp.run. ZIOCliDefault combines it with ZIOAppDefault and is the usual way to write a command-line program:

trait ZIOCli extends ZIOApp {
def cliApp: CliApp[Environment with ZIOAppArgs with Scope, Any, Any]
}

trait ZIOCliDefault extends ZIOCli with ZIOAppDefault

With ZIOAppDefault, Environment is Any, so cliApp may require at most ZIOAppArgs and Scope. Provide other services inside execute. The program becomes an object that names its CliApp:

object Greet extends ZIOCliDefault {
val cliApp = greetApp
}

The program exits with code 0 when cliApp.run succeeds and with code 1 otherwise; Exit Codes lists each outcome. To choose other exit codes, see How to choose the exit code for each error.

Use ZIOCli directly when your program extends another ZIOApp subtype.

Changing a CliAppโ€‹

These methods return a new CliApp and leave the original unchanged. Each one changes a single aspect of the app.

CliApp#mapโ€‹

CliApp#map transforms the value that execute returns. Built-in actions that return None, such as --help, are not affected:

val greetingApp: CliApp[Any, Nothing, String] = CliApp.make(
name = "greet",
version = "1.0.0",
summary = text("Builds a greeting"),
command = greet
)(name => ZIO.succeed(s"Hello, $name!"))

val lengthApp: CliApp[Any, Nothing, Int] = greetingApp.map(_.length)

CliApp#flatMapโ€‹

CliApp#flatMap runs another effect after execute, with the value that execute returns. The environment and error types widen to include those of the new effect:

val loggedApp: CliApp[Any, java.io.IOException, Unit] =
greetApp.flatMap(_ => Console.printLine("Greeting sent."))

CliApp#footerโ€‹

CliApp#footer appends a HelpDoc to the footer. Calling it more than once keeps all the text, in call order:

val withFooter = greetApp.footer(HelpDoc.p("Report problems to the issue tracker."))

CliApp#summaryโ€‹

CliApp#summary appends a HelpDoc.Span to the summary in the header line. No space is added, so include one in the span:

val withSummary = greetApp.summary(text(" (beta)"))

CliApp#configโ€‹

CliApp#config replaces the whole CliConfig of the app:

val caseSensitiveApp = greetApp.config(CliConfig.default.copy(caseSensitive = true))

The Usage Lineโ€‹

The USAGE section of the help has one line per shape of input the command accepts. Each line starts with $ . The line comes from a UsageSynopsis value, which every Command, Options and Args has. The synopsis is built as follows:

  • A command starts with its name, followed by the synopsis of its options and then of its arguments.
  • An option is written as its names and, unless it is a flag, its value type, such as --name <text>. An enumeration option shows its choices, such as --mode fast | slow.
  • An option with several names lists them in parentheses, such as (-q, --quiet).
  • Options with a default value, options made with Options#optional, and flags are wrapped in square brackets, such as [--verbose].
  • Two options combined with Options#| are joined by | with no spaces, such as [-a]|[-b].
  • A repeated argument, made with Args#* or Args#+, ends with ....
  • A command with two or more subcommands shows <command> in their place. A command with exactly one subcommand shows that subcommand's name, options and arguments.
  • Empty parts are removed, so a command with no options and no arguments shows only its name.

To print the usage line of a few commands, we render each synopsis with the default configuration:

def usage(command: Command[_]): String =
command.synopsis.enumerate(CliConfig.default).map(_.text).mkString("\n")

val tail = Command("tail", Options.integer("lines").alias("n").withDefault(BigInt(10)), Args.file("file").+)
val repo = Command("repo").subcommands(Command("clone", Args.text("repository")), Command("add", Args.text("path")))
val singleRepo = Command("repo").subcommands(Command("clone", Args.text("repository")))

The three commands render as follows:

println(usage(tail))
// tail [(-n, --lines <integer>)] <file>...
println(usage(repo))
// repo <command>
println(usage(singleRepo))
// repo clone <repository>

CliConfig#showAllNames and CliConfig#showTypes change how names and types appear in the USAGE lines (see Help Appearance). They do not affect the OPTIONS and COMMANDS sections, which always show every name and type.

UsageSynopsis has these public members:

sealed trait UsageSynopsis {
def enumerate(config: CliConfig): List[HelpDoc.Span]
def helpDoc: HelpDoc
def +(that: UsageSynopsis): UsageSynopsis
def optional: UsageSynopsis
}

UsageSynopsis#enumerate returns the lines for a given configuration. UsageSynopsis#helpDoc returns the same lines as a HelpDoc, always rendered with CliConfig.default. UsageSynopsis#+ places two synopses one after the other, and UsageSynopsis#optional wraps a synopsis in square brackets. The cases that make up a synopsis (UsageSynopsis.Named, Optional, Repeated, Sequence, Alternation, Mixed and None) are public, but the constructors of Command, Options and Args create them for you.

ZIO Chat

๐ŸŽ‰ Hey there, ZIO aficionado! ๐ŸŽ‰ Step into the ZIO wonderland with me as your guide. Whether you're a functional fanatic or just dipping your toes into the ZIO ecosystem, I'm here to make your journey smooth. Got ZIO questions? I've got answers! Let's navigate the ZIO universe together and unlock its infinite possibilities!