Last active
May 6, 2023 15:39
-
-
Save dacr/de47c3b6c3d96556129e60d74b6d4a6a to your computer and use it in GitHub Desktop.
ZIO learning - zio cli / published by https://github.com/dacr/code-examples-manager #e84b545b-7828-42ee-ad84-54fdfc3d001c/ae24c2d6a8c27d2be575a6a5fe1d6361737b9095
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO learning - zio cli | |
// keywords : scala, zio, learning, pure-functional, cli, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : e84b545b-7828-42ee-ad84-54fdfc3d001c | |
// created-on : 2021-12-29T19:12:47+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.2.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "dev.zio::zio-cli:0.4.0" | |
// --------------------- | |
import zio.* | |
import zio.cli.* | |
import zio.cli.HelpDoc.Span.text | |
sealed trait Subcommand | |
object Subcommand { | |
final case class Something(verbose: Boolean) extends Subcommand | |
} | |
object Encapsulated extends App { | |
val verboseFlag: Options[Boolean] = Options.boolean("verbose").alias("v") | |
val somethingHelp: HelpDoc = HelpDoc.p("something description") | |
val something = Command("something", verboseFlag, Args.none).withHelp(somethingHelp).map { verbose => | |
Subcommand.Something(verbose) | |
} | |
val cmd: Command[Subcommand] = | |
Command("app", Options.none, Args.none) | |
.subcommands(something) | |
val helloApp = CliApp.make( | |
name = "Command Line App", | |
version = "0.1.1", | |
summary = text("just an application"), | |
command = cmd | |
) { case Subcommand.Something(verbose) => | |
Console.printLine(s"Executing something with verbose=$verbose") | |
} | |
def run(args: List[String]) = | |
helloApp.run(args) | |
} | |
// --------------------------------------------------------------------------------- | |
Encapsulated.main(Array("something", "--verbose")) | |
Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment