Last active
May 25, 2024 10:18
-
-
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/833d283b97772f6c27cc1acb70b14cc75af0c844
This file contains hidden or 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.4.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