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

Expose val command: Command[IO[ExitCode]] in CommandIOApp #542

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -24,6 +24,18 @@ class CommandIOAppSpec extends AnyFlatSpec with Matchers {
runApp() shouldBe ExitCode.Error
}

it should "expose command so showHelp can be called" in {
PureHelloWorld.command.showHelp shouldBe """Usage: pure-hello <to-greet>
|
|Pure Hello World with Decline
|
|Options and flags:
| --help
| Display this help text.
| --version, -v
| Print the version number and exit.""".stripMargin
}

private[this] def runApp(args: String*): ExitCode =
PureHelloWorld.run(args.toList).unsafeRunSync()(IORuntime.global)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ abstract class CommandIOApp(

def main: Opts[IO[ExitCode]]

val command: Command[IO[ExitCode]] =
CommandIOApp.createCommand(
name = name,
header = header,
helpFlag = helpFlag,
version = Option(version).filter(_.nonEmpty),
main
)

override final def run(args: List[String]): IO[ExitCode] =
CommandIOApp
.run[IO](name, header, helpFlag, Option(version).filter(_.nonEmpty))(main, args)
CommandIOApp.run[IO](command, args)

}

Expand Down Expand Up @@ -54,6 +62,14 @@ object CommandIOApp {
else ExitCode.Success
}

private[CommandIOApp] def createCommand(
name: String,
header: String,
helpFlag: Boolean,
version: Option[String],
opts: Opts[IO[ExitCode]]
) = Command(name, header, helpFlag)(version.map(addVersionFlag(opts)).getOrElse(opts))

private[CommandIOApp] def addVersionFlag[F[_]: Console: Functor](
opts: Opts[F[ExitCode]]
)(version: String): Opts[F[ExitCode]] = {
Expand Down
Loading