Skip to content

Commit

Permalink
Subcommand-nesting (#137)
Browse files Browse the repository at this point in the history
* Express GitRemote

* Show problematic usecase for sub-accounting
  • Loading branch information
DamianReeves authored Sep 28, 2022
1 parent b42d4f8 commit 77e4b28
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
25 changes: 14 additions & 11 deletions examples/shared/src/main/scala/zio/cli/examples/GitExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ object GitExample extends ZIOCliDefault {
}

val remoteHelp: HelpDoc = HelpDoc.p("Remote subcommand description")
val remote = Command("remote", verboseFlag, Args.none)
.withHelp(remoteHelp)
.map(Subcommand.Remote(_))
.subcommands(remoteAdd, remoteRemove)
.map { case (verbose, remoteSubcommand) =>
remoteSubcommand
val remote = {
// val gitRemote = Command("remote", verboseFlag).withHelp(remoteHelp).map(Subcommand.Remote(_))
// val gitRemoteAdd = Command("remote").withHelp(remoteHelp).subcommands(remoteAdd)
// val gitRemoteRemove = Command("remote").withHelp(remoteHelp).subcommands(remoteRemove)
// gitRemote | gitRemoteAdd | gitRemoteRemove
Command("remote", verboseFlag)
.withHelp(remoteHelp)
.map(Subcommand.Remote(_))
.subcommands(remoteAdd, remoteRemove)
.map(_._2) //TODO: We shouldn't have to discard the standalone remote command

}
}

val git: Command[Subcommand] =
Command("git", Options.none, Args.none).subcommands(add, remote)
Expand All @@ -66,14 +70,13 @@ object GitExample extends ZIOCliDefault {
) {
case Subcommand.Add(modified, directory) =>
printLine(s"Executing `git add $directory` with modified flag set to $modified")

case Subcommand.Remote(verbose) =>
printLine(s"Executing `git remote` with verbose flag set to $verbose")

case Subcommand.Remote.Add(name, url) =>
printLine(s"Executing `git remote add $name $url`")
case Subcommand.Remote.Remove(name) =>
printLine(s"Executing `git remote remove $name`")
case Subcommand.Remote(verbose) =>
printLine(s"Executing `git remote` with verbose flag set to $verbose")

}
}

Expand Down
9 changes: 7 additions & 2 deletions zio-cli/shared/src/main/scala/zio/cli/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,13 @@ object Command {
args: List[String],
conf: CliConfig
): IO[ValidationError, CommandDirective[(A, B)]] = {
val helpDirectiveForChild =
val helpDirectiveForChild = {
val safeTail = args match {
case Nil => Nil
case head :: tail => tail
}
self.child
.parse(args.tail, conf)
.parse(safeTail, conf)
.collect(ValidationError(ValidationErrorType.InvalidArgument, HelpDoc.empty)) {
case CommandDirective.BuiltIn(BuiltInOption.ShowHelp(synopsis, helpDoc)) =>
val parentName = self.names.headOption.getOrElse("")
Expand All @@ -235,6 +239,7 @@ object Command {
)
}
}
}

val helpDirectiveForParent =
ZIO.succeed(CommandDirective.builtIn(BuiltInOption.ShowHelp(self.synopsis, self.helpDoc)))
Expand Down

0 comments on commit 77e4b28

Please sign in to comment.