You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by ji...@apache.org on 2023/08/10 12:30:42 UTC

[daffodil] branch main updated: Update scallop to 5.0.0

This is an automated email from the ASF dual-hosted git repository.

jinterrante pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil.git


The following commit(s) were added to refs/heads/main by this push:
     new 702952850 Update scallop to 5.0.0
702952850 is described below

commit 702952850b9e5c7459d052b2f41855ea595d0003
Author: Scala Steward <sc...@virtuslab.com>
AuthorDate: Thu Aug 3 11:47:00 2023 -0400

    Update scallop to 5.0.0
    
    Implements new Scallop stdoutPrintln and stderrPrintln variables to
    output to the STDOUT and STDERR variables instead of Console.out/err.
    
    Implements new Scallop exitHandler variable to throw a new exception
    that contains the exit code instead of calling Sys.exit. This exception
    is later caught, turned into a Daffodil ExitCode enumeration value, and
    handle appropriately.
    
    These changes allow unit tests using --help and --version to work as
    expected. Previously the expect library could not see the usage output
    and the call to Sys.exit caused SBT to exit. Now scallop writes to the
    provider buffers and does no force exit, letting our CLI testing
    infrastructure capture those values. When not run via test, the CLI
    behaves the same as before.
---
 .../src/main/scala/org/apache/daffodil/cli/Main.scala    | 16 ++++++++++++++--
 .../org/apache/daffodil/cli/cliTest/TestCLIParsing.scala |  6 ++++++
 project/Dependencies.scala                               |  2 +-
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/daffodil-cli/src/main/scala/org/apache/daffodil/cli/Main.scala b/daffodil-cli/src/main/scala/org/apache/daffodil/cli/Main.scala
index 2d1fb0029..fa8ca15be 100644
--- a/daffodil-cli/src/main/scala/org/apache/daffodil/cli/Main.scala
+++ b/daffodil-cli/src/main/scala/org/apache/daffodil/cli/Main.scala
@@ -93,7 +93,10 @@ import org.xml.sax.InputSource
 import org.xml.sax.SAXParseException
 import org.xml.sax.helpers.XMLReaderFactory
 
-class CLIConf(arguments: Array[String]) extends scallop.ScallopConf(arguments) {
+class ScallopExitException(val exitCode: Int) extends Exception
+
+class CLIConf(arguments: Array[String], stdout: PrintStream, stderr: PrintStream)
+  extends scallop.ScallopConf(arguments) {
 
   /**
    * This is used when the flag is optional and so is its
@@ -251,6 +254,12 @@ class CLIConf(arguments: Array[String]) extends scallop.ScallopConf(arguments) {
     throw new GenericScallopException(msg)
   }
 
+  exitHandler = int => throw new ScallopExitException(int)
+
+  stdoutPrintln = string => stdout.println(string)
+
+  stderrPrintln = string => stderr.println(string)
+
   banner("""|Usage: daffodil [GLOBAL_OPTS] <subcommand> [SUBCOMMAND_OPTS]
             |
             |Global Options:""".stripMargin)
@@ -1038,7 +1047,7 @@ class Main(
   }
 
   def runIgnoreExceptions(arguments: Array[String]): ExitCode.Value = {
-    val conf = new CLIConf(arguments)
+    val conf = new CLIConf(arguments, STDOUT, STDERR)
 
     // Set the log level to whatever was parsed by the options
     setLogLevel(conf.verbose())
@@ -1888,6 +1897,9 @@ class Main(
         case e: DebuggerExitException => {
           ExitCode.Failure
         }
+        case e: ScallopExitException => {
+          ExitCode(e.exitCode)
+        }
         case e: GenericScallopException => {
           Logger.log.error(e.message)
           ExitCode.Usage
diff --git a/daffodil-cli/src/test/scala/org/apache/daffodil/cli/cliTest/TestCLIParsing.scala b/daffodil-cli/src/test/scala/org/apache/daffodil/cli/cliTest/TestCLIParsing.scala
index bec0c1a5f..e6693b5ac 100644
--- a/daffodil-cli/src/test/scala/org/apache/daffodil/cli/cliTest/TestCLIParsing.scala
+++ b/daffodil-cli/src/test/scala/org/apache/daffodil/cli/cliTest/TestCLIParsing.scala
@@ -28,6 +28,12 @@ import org.junit.Test
 
 class TestCLIParsing {
 
+  @Test def test_CLI_help(): Unit = {
+    runCLI(args"parse --help") { cli =>
+      cli.expect("Usage: daffodil parse")
+    }(ExitCode.Success)
+  }
+
   @Test def test_3677_CLI_Parsing_elementFormDefault_qualified(): Unit = {
     val schema = path(
       "daffodil-test/src/test/resources/org/apache/daffodil/section00/general/elementFormDefaultQualified.dfdl.xsd",
diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index 70b54b829..a8ac832fd 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -46,7 +46,7 @@ object Dependencies {
   lazy val cli = Seq(
     "org.fusesource.jansi" % "jansi" % "2.4.0",
     "org.jline" % "jline" % "3.22.0",
-    "org.rogach" %% "scallop" % "4.1.0",
+    "org.rogach" %% "scallop" % "5.0.0",
     "net.sf.expectit" % "expectit-core" % "0.9.0" % "test",
   )