Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli quick fixes #252

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/shared/src/main/scala/zio/cli/examples/Sample.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package zio.cli.examples

import zio.cli.{CliApp, ZIOCliDefault}
import zio.cli._
import zio.cli.HelpDoc.Span.text
import zio.Console.printLine

object Sample extends ZIOCliDefault {

val options: Options[Either[BigInt, String]] = Options.integer("opt1").orElseEither(Options.text("opt2"))
val arguments: Args[String] = Args.text("repository")
val help: HelpDoc = HelpDoc.p("Creates a copy of an existing repository")

val command: Command[(Either[BigInt, String], String)] =
Command("clone").subcommands(Command("clone", options, arguments).withHelp(help))

val cliApp = CliApp.make(
name = "Sample Git",
version = "1.1.0",
summary = text("Sample implementation of git clone"),
command = command
) {
// Implement logic of CliApp
case _ => printLine("executing git clone")
}

}
54 changes: 29 additions & 25 deletions zio-cli/shared/src/main/scala/zio/cli/CliApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,27 @@ sealed trait CliApp[-R, +E, +Model] {
object CliApp {

def make[R, E, Model](
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, Any]): CliApp[R, E, Model] =
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, Any]): CliApp[R, E, Model] =
CliAppImpl(name, version, summary, command, execute, footer, config, figFont)

private case class CliAppImpl[-R, +E, Model](
name: String,
version: String,
summary: HelpDoc.Span,
command: Command[Model],
execute: Model => ZIO[R, E, Any],
footer: HelpDoc = HelpDoc.Empty,
config: CliConfig = CliConfig.default,
figFont: FigFont = FigFont.Default
) extends CliApp[R, E, Model] { self =>
name: String,
version: String,
summary: HelpDoc.Span,
command: Command[Model],
execute: Model => ZIO[R, E, Any],
footer: HelpDoc = HelpDoc.Empty,
config: CliConfig = CliConfig.default,
figFont: FigFont = FigFont.Default
) extends CliApp[R, E, Model] {
self =>
def config(newConfig: CliConfig): CliApp[R, E, Model] = copy(config = newConfig)

def footer(newFooter: HelpDoc): CliApp[R, E, Model] =
Expand Down Expand Up @@ -90,13 +91,13 @@ object CliApp {
}
}
case ShowWizard(command) => {
val fancyName = p(code(self.figFont.render(self.name)))
val header = p(text("WIZARD of ") + text(self.name) + text(self.version) + text(" -- ") + self.summary)
val fancyName = p(code(self.figFont.render(self.name)))
val header = p(text("WIZARD of ") + text(self.name) + text(self.version) + text(" -- ") + self.summary)
val explanation = p(s"Wizard mode assist you in constructing commands for $name$version")

(for {
parameters <- Wizard(command, config, fancyName + header + explanation).execute
_ <- run(parameters)
_ <- run(parameters)
} yield ()).catchSome { case Wizard.QuitException() =>
ZIO.unit
}
Expand All @@ -108,18 +109,21 @@ object CliApp {
@tailrec
def prefix(command: Command[_]): List[String] =
command match {
case Command.Single(name, _, _, _) => List(name)
case Command.Map(command, _) => prefix(command)
case Command.OrElse(_, _) => Nil
case Command.Single(name, _, _, _) => List(name)
case Command.Map(command, _) => prefix(command)
case Command.OrElse(_, _) => Nil
case Command.Subcommands(parent, _) => prefix(parent)
}

self.command
.parse(prefix(self.command) ++ args, self.config)
.foldZIO(
e => printDocs(e.error) *> ZIO.fail(e),
e => {
printDocs(e.error) *> ZIO.fail(e)
},
{
case CommandDirective.UserDefined(_, value) => self.execute(value)
case CommandDirective.UserDefined(_, value) =>
self.execute(value)
case CommandDirective.BuiltIn(x) =>
executeBuiltIn(x).catchSome { case e: ValidationError =>
printDocs(e.error) *> ZIO.fail(e)
Expand Down
2 changes: 1 addition & 1 deletion zio-cli/shared/src/main/scala/zio/cli/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ object Command {
}

final case class OrElse[A](left: Command[A], right: Command[A]) extends Command[A] with Alternatives {
lazy val helpDoc: HelpDoc = left.helpDoc + right.helpDoc
lazy val helpDoc: HelpDoc = left.helpDoc addAlternative right.helpDoc

lazy val names: Set[String] = left.names ++ right.names

Expand Down
Loading
Loading