Skip to content

Commit

Permalink
Merge pull request #308 from alexarchambault/develop
Browse files Browse the repository at this point in the history
Various changes
  • Loading branch information
alexarchambault authored Jul 12, 2021
2 parents 72ccb49 + 9b773b6 commit 3986432
Show file tree
Hide file tree
Showing 44 changed files with 536 additions and 240 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ jobs:
jvm: 8
apps: sbt-launcher:1.2.22
- name: Test
run: sbt ++${{ matrix.SCALA_VERSION }} test mimaReportBinaryIssues
run: sbt ++${{ matrix.SCALA_VERSION }} test

bin-compat:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
submodules: true
- uses: coursier/cache-action@v6.1
- uses: laughedelic/coursier-setup@v1
with:
jvm: 8
apps: sbt-launcher:1.2.22
- name: Test
run: sbt +mimaReportBinaryIssues

publish:
needs: test
needs: [test, bin-compat]
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions cats/shared/src/main/scala/caseapp/cats/IOCommandApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import caseapp.core.Error
import caseapp.core.help.CommandsHelp
import cats.effect.{ExitCode, IO}

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class IOCommandApp[T](implicit
commandParser: CommandParser[T],
commandsMessages: CommandsHelp[T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import caseapp.core.RemainingArgs
import cats.effect.{ExitCode, IO}

/* The A suffix stands for anonymous */
@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class IOCommandAppA[T](implicit
commandParser: CommandParser[T],
commandsMessages: CommandsHelp[T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import caseapp.core.parser.Parser
import caseapp.core.RemainingArgs
import cats.effect._

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class IOCommandAppWithPreCommand[D, T](implicit
val beforeCommandParser: Parser[D],
baseBeforeCommandMessages: Help[D],
Expand Down
10 changes: 8 additions & 2 deletions core/shared/src/main/scala/caseapp/core/Arg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ import dataclass._
noHelp: Boolean = false,
isFlag: Boolean = false,
@since
group: Option[Group] = None
)
group: Option[Group] = None,
@since
origin: Option[String] = None
) {
def withDefaultOrigin(defaultOrigin: String): Arg =
if (origin.isEmpty) withOrigin(Some(defaultOrigin))
else this
}

object Arg {
def apply(name: String): Arg =
Expand Down
47 changes: 35 additions & 12 deletions core/shared/src/main/scala/caseapp/core/app/CaseApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package caseapp.core.app
import caseapp.Name
import caseapp.core.{Error, RemainingArgs}
import caseapp.core.complete.{Completer, CompletionItem, HelpCompleter}
import caseapp.core.help.{Help, HelpFormat, WithHelp}
import caseapp.core.help.{Help, HelpFormat, WithFullHelp, WithHelp}
import caseapp.core.parser.Parser
import caseapp.core.util.Formatter

abstract class CaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]) {

def hasHelp: Boolean = true
def hasFullHelp: Boolean = false

def parser: Parser[T] = {
val p = parser0.nameFormatter(nameFormatter)
Expand All @@ -25,7 +26,15 @@ abstract class CaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]
new HelpCompleter[T](messages)

def complete(args: Seq[String], index: Int): List[CompletionItem] =
if (hasHelp)
if (hasFullHelp)
parser.withFullHelp.complete(
args,
index,
completer.withFullHelp,
stopAtFirstUnrecognized,
ignoreUnrecognized
)
else if (hasHelp)
parser.withHelp.complete(
args,
index,
Expand All @@ -52,24 +61,29 @@ abstract class CaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]
exit(1)
}

protected def helpWithProgName(progName: String): Help[WithHelp[T]] = {
val baseHelp = messages.withHelp
if (progName.isEmpty || progName == baseHelp.progName) baseHelp
else baseHelp.withProgName(progName)
lazy val finalHelp: Help[_] =
if (hasFullHelp) messages.withFullHelp
else if (hasHelp) messages.withHelp
else messages

def fullHelpAsked(progName: String): Nothing = {
val help = if (progName.isEmpty) finalHelp else finalHelp.withProgName(progName)
println(help.help(helpFormat, showHidden = true))
exit(0)
}

def helpAsked(): Nothing =
helpAsked("")
def helpAsked(progName: String): Nothing = {
val help = helpWithProgName(progName)
println(help.help(helpFormat))
val help = if (progName.isEmpty) finalHelp else finalHelp.withProgName(progName)
println(help.help(helpFormat, showHidden = false))
exit(0)
}

def usageAsked(): Nothing =
usageAsked("")
def usageAsked(progName: String): Nothing = {
val help = helpWithProgName(progName)
val help = if (progName.isEmpty) finalHelp else finalHelp.withProgName(progName)
println(help.usage(helpFormat))
exit(0)
}
Expand All @@ -78,7 +92,7 @@ abstract class CaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]
HelpFormat.default()

def ensureNoDuplicates(): Unit =
messages.ensureNoDuplicates()
finalHelp.ensureNoDuplicates()

/**
* Arguments are expanded then parsed. By default, argument expansion is the identity function.
Expand Down Expand Up @@ -123,10 +137,19 @@ abstract class CaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]
Formatter.DefaultNameFormatter

def main(args: Array[String]): Unit =
main(messages.progName, PlatformUtil.arguments(args))
main(finalHelp.progName, PlatformUtil.arguments(args))

def main(progName: String, args: Array[String]): Unit =
if (hasHelp)
if (hasFullHelp)
parser.withFullHelp.detailedParse(expandArgs(args.toList), stopAtFirstUnrecognized, ignoreUnrecognized) match {
case Left(err) => error(err)
case Right((WithFullHelp(_, _, true, _), _)) => fullHelpAsked(progName)
case Right((WithFullHelp(_, true, _, _), _)) => helpAsked(progName)
case Right((WithFullHelp(true, _, _, _), _)) => usageAsked(progName)
case Right((WithFullHelp(_, _, _, Left(err)), _)) => error(err)
case Right((WithFullHelp(_, _, _, Right(t)), remainingArgs)) => run(t, remainingArgs)
}
else if (hasHelp)
parser.withHelp.detailedParse(expandArgs(args.toList), stopAtFirstUnrecognized, ignoreUnrecognized) match {
case Left(err) => error(err)
case Right((WithHelp(_, true, _), _)) => helpAsked(progName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caseapp.core.app
import caseapp.core.commandparser.CommandParser
import caseapp.core.help.CommandsHelp

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class CommandApp[T](implicit
commandParser: CommandParser[T],
commandsMessages: CommandsHelp[T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import caseapp.core.commandparser.CommandParser
import caseapp.core.help.CommandsHelp

/* The A suffix stands for anonymous */
@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class CommandAppA[T](
commandParser: CommandParser[T],
commandsMessages: CommandsHelp[T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import caseapp.core.help.{CommandsHelp, Help, WithHelp}
import caseapp.core.parser.Parser
import caseapp.core.RemainingArgs

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class CommandAppWithPreCommand[D, T](implicit
val beforeCommandParser: Parser[D],
baseBeforeCommandMessages: Help[D],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ abstract class CommandsEntryPoint extends PlatformCommandsMethods {
RuntimeCommandsHelp(
progName,
Some(description).filter(_.nonEmpty),
defaultCommand.map(_.messages.withHelp: Help[_]).getOrElse(Help[Unit]()),
commands.map(cmd => RuntimeCommandHelp(cmd.names, cmd.messages.withHelp, cmd.group, cmd.hidden))
defaultCommand.map(_.finalHelp: Help[_]).getOrElse(Help[Unit]()),
commands.map(cmd => RuntimeCommandHelp(cmd.names, cmd.finalHelp, cmd.group, cmd.hidden))
)

def helpFormat: HelpFormat =
Expand Down Expand Up @@ -100,7 +100,7 @@ abstract class CommandsEntryPoint extends PlatformCommandsMethods {
case None =>
RuntimeCommandParser.parse(commands, actualArgs.toList) match {
case None =>
val usage = help.help(helpFormat)
val usage = help.help(helpFormat, showHidden = false)
println(usage)
PlatformUtil.exit(0)
case Some((commandName, command, commandArgs)) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import caseapp.util.AnnotationOption
import shapeless.{:+:, CNil, Coproduct, LabelledGeneric, Strict, Witness}
import shapeless.labelled.{FieldType, field}

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class AutoCommandParserImplicits {

implicit def cnil: CommandParser[CNil] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import scala.annotation.tailrec
*
* @tparam T: result type
*/
@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
abstract class CommandParser[T] {

/**
Expand Down Expand Up @@ -141,6 +142,7 @@ abstract class CommandParser[T] {

}

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
object CommandParser extends AutoCommandParserImplicits {

def apply[T](implicit parser: CommandParser[T]): CommandParser[T] = parser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import caseapp.core.app.CaseApp
import caseapp.core.parser.Parser
import shapeless.{:+:, Coproduct, Generic, ops}

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
final class CommandParserOps[T <: Coproduct](val commandParser: CommandParser[T]) extends AnyVal {

// foo added not to collide with the other add method below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import caseapp.core.parser.Parser
import dataclass.data
import shapeless.{:+:, Coproduct, Inl, Inr}

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
@data class ConsCommandParser[H, T <: Coproduct](
name: Seq[String],
parser: Parser[H],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caseapp.core.commandparser
import caseapp.core.parser.Parser
import dataclass.data

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
@data class MappedCommandParser[T, U](parser: CommandParser[T], f: T => U) extends CommandParser[U] {

def commandMap: Map[Seq[String], Parser[U]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caseapp.core.commandparser
import caseapp.core.parser.Parser
import shapeless.CNil

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
case object NilCommandParser extends CommandParser[CNil] {

def commandMap: Map[Seq[String], Parser[CNil]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import caseapp.core.parser.Parser
import caseapp.core.help.WithHelp
import dataclass.data

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
@data class WithHelpCommandParser[T](parser: CommandParser[T]) extends CommandParser[WithHelp[T]] {

def commandMap: Map[Seq[String], Parser[WithHelp[T]]] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package caseapp.core.complete

import caseapp.core.Arg
import caseapp.core.help.WithHelp
import caseapp.core.help.{WithFullHelp, WithHelp}

trait Completer[T] { self =>
def optionName(prefix: String, state: Option[T]): List[CompletionItem]
Expand All @@ -19,4 +19,6 @@ trait Completer[T] { self =>
}
def withHelp: Completer[WithHelp[T]] =
contramapOpt(_.baseOrError.toOption)
def withFullHelp: Completer[WithFullHelp[T]] =
contramapOpt(_.baseOrError.toOption)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scala.language.implicitConversions
import caseapp.HelpMessage


@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
@data class CommandsHelp[T](
messages: Seq[(Seq[String], CommandHelp)]
) {
Expand All @@ -20,6 +21,7 @@ import caseapp.HelpMessage
CommandsHelp(messages)
}

@deprecated("Use Command and CommandsEntryPoint instead", "2.1.0")
object CommandsHelp {

def apply[T](implicit messages: CommandsHelp[T]): CommandsHelp[T] = messages
Expand Down
Loading

0 comments on commit 3986432

Please sign in to comment.