From a4e27fcccf22cc99cf15410366f165e4b00fe1bd Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Fri, 1 Apr 2022 16:00:29 -0400 Subject: [PATCH] CommandApp exit with non-zero code on parse failure JS implementation from: https://github.com/alexarchambault/case-app/blob/d1da1ac55e97ce49748b84b2a99aecbb2d8452cd/core/js/src/main/scala/caseapp/core/app/PlatformUtil.scala --- .../src/main/scala/com/monovore/decline/PlatformApp.scala | 7 +++++++ .../src/main/scala/com/monovore/decline/PlatformApp.scala | 4 ++++ .../src/main/scala/com/monovore/decline/PlatformApp.scala | 4 ++++ .../src/main/scala/com/monovore/decline/CommandApp.scala | 4 +++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/js/src/main/scala/com/monovore/decline/PlatformApp.scala b/core/js/src/main/scala/com/monovore/decline/PlatformApp.scala index 2ffa8ade..979ef905 100644 --- a/core/js/src/main/scala/com/monovore/decline/PlatformApp.scala +++ b/core/js/src/main/scala/com/monovore/decline/PlatformApp.scala @@ -2,6 +2,7 @@ package com.monovore.decline import scala.scalajs.js import scala.scalajs.js.annotation.JSGlobalScope +import js.Dynamic.global object PlatformApp { @@ -21,4 +22,10 @@ object PlatformApp { * otherwise. */ def ambientArgs: Option[Seq[String]] = Process.process.toOption.map { _.argv.drop(2).toSeq } + + private lazy val process = global.require("process") + def exit(code: Int): Nothing = { + process.exit(code) + sys.error(s"Attempt to exit with code $code failed") + } } diff --git a/core/jvm/src/main/scala/com/monovore/decline/PlatformApp.scala b/core/jvm/src/main/scala/com/monovore/decline/PlatformApp.scala index fbccb460..a6b7f278 100644 --- a/core/jvm/src/main/scala/com/monovore/decline/PlatformApp.scala +++ b/core/jvm/src/main/scala/com/monovore/decline/PlatformApp.scala @@ -7,4 +7,8 @@ object PlatformApp { * otherwise. */ def ambientArgs: Option[Seq[String]] = None + + def exit(code: Int): Nothing = { + sys.exit(code) + } } diff --git a/core/native/src/main/scala/com/monovore/decline/PlatformApp.scala b/core/native/src/main/scala/com/monovore/decline/PlatformApp.scala index fbccb460..a6b7f278 100644 --- a/core/native/src/main/scala/com/monovore/decline/PlatformApp.scala +++ b/core/native/src/main/scala/com/monovore/decline/PlatformApp.scala @@ -7,4 +7,8 @@ object PlatformApp { * otherwise. */ def ambientArgs: Option[Seq[String]] = None + + def exit(code: Int): Nothing = { + sys.exit(code) + } } diff --git a/core/shared/src/main/scala/com/monovore/decline/CommandApp.scala b/core/shared/src/main/scala/com/monovore/decline/CommandApp.scala index 5600fb02..02cc2718 100644 --- a/core/shared/src/main/scala/com/monovore/decline/CommandApp.scala +++ b/core/shared/src/main/scala/com/monovore/decline/CommandApp.scala @@ -50,7 +50,9 @@ For suggested usage, see: http://monovore.com/decline/usage.html#defining-an-app ) final def main(args: Array[String]): Unit = command.parse(PlatformApp.ambientArgs getOrElse args, sys.env) match { - case Left(help) => System.err.println(help) + case Left(help) => + System.err.println(help) + PlatformApp.exit(1) case Right(_) => () } }