Repository: scala-steward-org/scala-steward
Pull request #1627 (2020-09-29 09:41:07)

Support `--help` and `--usage` CLI options

codecov[bot]: # [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627?src=pr&el=h1) Report
> Merging [#1627](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627?src=pr&el=desc) into [master](https://codecov.io/gh/scala-steward-org/scala-steward/commit/2ae97f1a550112ad4dc1f038d38808b546b012dd?el=desc) will **decrease** coverage by `0.04%`.
> The diff coverage is `60.00%`.

[![Impacted file tree graph](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627/graphs/tree.svg?width=650&height=150&src=pr&token=udzMsc1Dne)](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1627      +/-   ##
==========================================
- Coverage   71.58%   71.53%   -0.05%     
==========================================
  Files         113      113              
  Lines        1879     1883       +4     
  Branches       54       57       +3     
==========================================
+ Hits         1345     1347       +2     
- Misses        534      536       +2     
```


| [Impacted Files](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [.../scala/org/scalasteward/core/application/Cli.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627/diff?src=pr&el=tree#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS9hcHBsaWNhdGlvbi9DbGkuc2NhbGE=) | `85.00% <60.00%> (-8.75%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627?src=pr&el=footer). Last update [2ae97f1...c9b879e](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1627?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala b/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala
index f0fc1adc..8fae55fe 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala
@@ -17,7 +17,7 @@
 package org.scalasteward.core.application
 
 import caseapp._
-import caseapp.core.Error.MalformedValue
+import caseapp.core.Error.{MalformedValue, Other}
 import caseapp.core.argparser.{ArgParser, SimpleArgParser}
 import cats.syntax.all._
 import org.http4s.Uri
@@ -29,7 +29,15 @@ import scala.concurrent.duration._
 final class Cli[F[_]](implicit F: ApplicativeThrowable[F]) {
   def parseArgs(args: List[String]): F[Args] =
     F.fromEither {
-      CaseApp.parse[Args](args).bimap(e => new Throwable(e.message), { case (parsed, _) => parsed })
+      CaseApp
+        .parseWithHelp[Args](args)
+        .flatMap {
+          case (_, true, _, _)              => Left(Other(CaseApp.helpMessage[Args]))
+          case (_, _, true, _)              => Left(Other(CaseApp.usageMessage[Args]))
+          case (parsed @ Right(_), _, _, _) => parsed
+          case (e @ Left(_), _, _, _)       => e
+        }
+        .leftMap(e => new Throwable(e.message))
     }
 }
 


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala b/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala
index 6ada1b40..d1cbc7e6 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala
@@ -1,11 +1,13 @@
 package org.scalasteward.core.application
+
 import org.http4s.syntax.literals._
 import org.scalasteward.core.application.Cli.EnvVar
+import org.scalatest.EitherValues
 import org.scalatest.funsuite.AnyFunSuite
 import org.scalatest.matchers.should.Matchers
 import scala.concurrent.duration._
 
-class CliTest extends AnyFunSuite with Matchers {
+class CliTest extends AnyFunSuite with Matchers with EitherValues {
   type Result[A] = Either[Throwable, A]
   val cli: Cli[Result] = new Cli[Result]
 
@@ -66,6 +68,14 @@ class CliTest extends AnyFunSuite with Matchers {
     cli.parseArgs(Nil).isLeft shouldBe true
   }
 
+  test("parseArgs --help") {
+    cli.parseArgs(List("--help")).left.value.getMessage should include("--git-author-email")
+  }
+
+  test("parseArgs --usage") {
+    cli.parseArgs(List("--usage")).left.value.getMessage should startWith("Usage: args")
+  }
+
   test("env-var without equals sign") {
     Cli.envVarArgParser(None, "SBT_OPTS").isLeft shouldBe true
   }