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

Show subcommands summary help #91

Merged
merged 5 commits into from
Sep 3, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zio.cli.examples

import java.nio.file.{Path => JPath}

import zio.cli.{Args, CliApp, Command, Exists, Options}
import zio.cli.{Args, CliApp, Command, Exists, HelpDoc, Options}
import zio.cli.HelpDoc.Span.text

import zio._
Expand All @@ -19,15 +19,17 @@ object GitExample extends App {

val modifiedFlag: Options[Boolean] = Options.boolean("m")

val addHelp: HelpDoc = HelpDoc.p("Add subcommand description")
val add =
Command("add", modifiedFlag, Args.directory("directory", Exists.Yes)).map { case (modified, directory) =>
Command("add", modifiedFlag, Args.directory("directory", Exists.Yes), addHelp).map { case (modified, directory) =>
Subcommand.Add(modified, directory)
}

val verboseFlag: Options[Boolean] = Options.boolean("verbose").alias("v")
val configPath: Options[Path] = Options.directory("c", Exists.Yes)

val remote = Command("remote", verboseFlag, Args.none).map { case (verbose, _) =>
val remoteHelp: HelpDoc = HelpDoc.p("Remote subcommand description")
val remote = Command("remote", verboseFlag, Args.none, remoteHelp).map { case (verbose, _) =>
Subcommand.Remote(verbose)
}

Expand Down
23 changes: 21 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 @@ -155,8 +155,27 @@ object Command {
}

final case class Subcommands[A, B](parent: Command[A], child: Command[B]) extends Command[(A, B)] { self =>
def helpDoc: HelpDoc =
parent.helpDoc + HelpDoc.h1("Subcommands") + HelpDoc.enumeration(child.names.toList.sorted.map(HelpDoc.p): _*)
def getHelpDescription(h: HelpDoc): HelpDoc.Span =
h match {
case HelpDoc.Header(value, _) => value
case HelpDoc.Paragraph(value) => value
case _ => HelpDoc.Span.space
}

def subcommandsDesc[C](c: Command[C]): HelpDoc =
c match {
case OrElse(left, right) =>
HelpDoc.enumeration(subcommandsDesc(left), subcommandsDesc(right))
case Single(name, desc, _, _) =>
HelpDoc.p(HelpDoc.Span.spans(HelpDoc.Span.text(name), HelpDoc.Span.text(" \t "), getHelpDescription(desc)))
case Map(cmd, _) =>
subcommandsDesc(cmd)
case c =>
HelpDoc.empty
}

def helpDoc =
parent.helpDoc + HelpDoc.h1("Subcommands") + subcommandsDesc(child)

def names: Set[String] = parent.names

Expand Down