Skip to content

Commit b702ae6

Browse files
authored
Merge pull request #1627 from avdv/add-help
Support `--help` and `--usage` CLI options
2 parents 2ae97f1 + 66fc1d6 commit b702ae6

File tree

2 files changed

+21
-3
lines changed
  • modules/core/src
    • main/scala/org/scalasteward/core/application
    • test/scala/org/scalasteward/core/application

2 files changed

+21
-3
lines changed

modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.scalasteward.core.application
1818

1919
import caseapp._
20-
import caseapp.core.Error.MalformedValue
20+
import caseapp.core.Error.{MalformedValue, Other}
2121
import caseapp.core.argparser.{ArgParser, SimpleArgParser}
2222
import cats.syntax.all._
2323
import org.http4s.Uri
@@ -29,7 +29,15 @@ import scala.concurrent.duration._
2929
final class Cli[F[_]](implicit F: ApplicativeThrowable[F]) {
3030
def parseArgs(args: List[String]): F[Args] =
3131
F.fromEither {
32-
CaseApp.parse[Args](args).bimap(e => new Throwable(e.message), { case (parsed, _) => parsed })
32+
CaseApp
33+
.parseWithHelp[Args](args)
34+
.flatMap {
35+
case (_, true, _, _) => Left(Other(CaseApp.helpMessage[Args]))
36+
case (_, _, true, _) => Left(Other(CaseApp.usageMessage[Args]))
37+
case (parsed @ Right(_), _, _, _) => parsed
38+
case (e @ Left(_), _, _, _) => e
39+
}
40+
.leftMap(e => new Throwable(e.message))
3341
}
3442
}
3543

modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.scalasteward.core.application
2+
23
import org.http4s.syntax.literals._
34
import org.scalasteward.core.application.Cli.EnvVar
5+
import org.scalatest.EitherValues
46
import org.scalatest.funsuite.AnyFunSuite
57
import org.scalatest.matchers.should.Matchers
68
import scala.concurrent.duration._
79

8-
class CliTest extends AnyFunSuite with Matchers {
10+
class CliTest extends AnyFunSuite with Matchers with EitherValues {
911
type Result[A] = Either[Throwable, A]
1012
val cli: Cli[Result] = new Cli[Result]
1113

@@ -66,6 +68,14 @@ class CliTest extends AnyFunSuite with Matchers {
6668
cli.parseArgs(Nil).isLeft shouldBe true
6769
}
6870

71+
test("parseArgs --help") {
72+
cli.parseArgs(List("--help")).left.value.getMessage should include("--git-author-email")
73+
}
74+
75+
test("parseArgs --usage") {
76+
cli.parseArgs(List("--usage")).left.value.getMessage should startWith("Usage: args")
77+
}
78+
6979
test("env-var without equals sign") {
7080
Cli.envVarArgParser(None, "SBT_OPTS").isLeft shouldBe true
7181
}

0 commit comments

Comments
 (0)