ZIO 2: Processing ZIO command line arguments

Without much explanation, here’s a Scala 3 and ZIO 2 example that shows how to process command-line arguments (command-line input) in a ZIO 2 application:

//> using scala "3"
//> using lib "dev.zio::zio::2.1.0"
package zio_done

import zio.*
import zio.Console.*

object ZioCommandLineArgsTest extends ZIOAppDefault:

    val failWithMsgEffect =
        printLineError("Usage: yada yada...").flatMap { _ =>
            ZIO.fail(new Exception("Usage error"))
        }

    val blueprint =
        for
            args <- ZIOAppArgs.getArgs
            rez  <- if args.size >= 1 then ZIO.succeed(()) else failWithMsgEffect
            _    <- printLine(s"\nfor is still running\n")
        yield
            args(0)

    def run = blueprint.foldZIO(
        failure => printLineError(s"FAILURE = $failure"),
        success => printLine(     s"SUCCESS = $success")
    )

Note that I need to write more about how ZIOAppArgs is provided in this application, like this example below, but I don’t have much time for discussion today.

val run =
    blueprint.provide(
        Client.default,
        Scope.default
    ).foldZIO(
        failure => Console.printLineError(s"failure = $failure"),
        success => Console.printLine(s"success = $success")
    )

More information

For more information on ZIO 2 applications, see my free “How to create a small example ZIO 2 application” video.