From 1bcac23457d19bbc7574acf7a86f24aad7ce70c0 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Tue, 6 Feb 2024 15:57:56 -0600 Subject: [PATCH] Fix more argument handling bugs Recognized JVM arguments all start with a dash. My original plan was to make such dashes implicit, with the the Jaunch configurator adding them... but upon reflection I think it's better for the TOML file to be explicit. Otherwise, there is no way to properly support a JVM that offers any non-dash-prefixed arguments. --- jaunch.toml | 52 ++++++++++++++++----------------- src/commonMain/kotlin/Jaunch.kt | 4 +-- test/jy.t | 20 +++++++++++++ 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/jaunch.toml b/jaunch.toml index 574782b..1fc34a6 100644 --- a/jaunch.toml +++ b/jaunch.toml @@ -131,32 +131,32 @@ supported-options = [ # mix and match to their hearts' content, being as sloppy as they want, and Jaunch will # sort out the mess. As long as the JVM args are on the list here, of course. recognized-jvm-args = [ - '?', - 'D*', - 'X*', - 'agentlib', - 'agentpath', - 'client', - 'd32', - 'd64', - 'da', - 'disableassertions', - 'disablesystemassertions', - 'dsa', - 'ea', - 'enableassertions', - 'enablesystemassertions', - 'esa', - 'help', - 'jar', - 'javaagent', - 'jre-restrict-search', - 'no-jre-restrict-search', - 'server', - 'showversion', - 'splash', - 'verbose', - 'version', + '-?', + '-D*', + '-X*', + '-agentlib', + '-agentpath', + '-client', + '-d32', + '-d64', + '-da', + '-disableassertions', + '-disablesystemassertions', + '-dsa', + '-ea', + '-enableassertions', + '-enablesystemassertions', + '-esa', + '-help', + '-jar', + '-javaagent', + '-jre-restrict-search', + '-no-jre-restrict-search', + '-server', + '-showversion', + '-splash', + '-verbose', + '-version', ] # ============================================================================== diff --git a/src/commonMain/kotlin/Jaunch.kt b/src/commonMain/kotlin/Jaunch.kt index 95b49c5..df1c743 100644 --- a/src/commonMain/kotlin/Jaunch.kt +++ b/src/commonMain/kotlin/Jaunch.kt @@ -162,7 +162,7 @@ fun main(args: Array) { if (argVal != null) error("Divider symbol (--) does not accept a parameter") if (i - 1 != divider) error("Divider symbol (--) may only be given once") } - else if (argKey in supportedOptions) { + else if ((divider < 0 || i <= divider) && argKey in supportedOptions) { // The argument is declared in Jaunch's configuration. Deal with it appropriately. val option: JaunchOption = supportedOptions[argKey]!! if (option.assignment == null) { @@ -192,7 +192,7 @@ fun main(args: Array) { // No dash-dash divider was given, so we need to guess: is this a JVM arg, or a main arg? (if (config.recognizes(argKey)) jvmArgs else mainArgs) += arg } - else if (i < divider) { + else if (i <= divider) { // This argument is before the dash-dash divider, so must be treated as a JVM arg. // But we only allow it through if it's a recognized JVM argument, or the // allow-unrecognized-jvm-args configuration flag is set to true. diff --git a/test/jy.t b/test/jy.t index 263d66e..782f49b 100644 --- a/test/jy.t +++ b/test/jy.t @@ -12,6 +12,26 @@ Test system property assignment. $ ./jy-linux-x64 -Dcake=chocolate -c 'from java.lang import System; print(System.getProperty("cake"))' chocolate +Test divider symbol handling. + + $ ./jy-linux-x64 -- --dry-run 2>&1 + Unknown option: -- or '--dry-run' + usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ... + Try 'jython -h' for more information. + [1] + + $ ./jy-linux-x64 --dry-run -- + /*/bin/java -Dpython.import.site=false -Dpython.cachedir.skip=true -Dpython.console.encoding=UTF-8 -Djava.class.path=/*/lib/jython-2.7.3.jar* -Xmx* org.python.util.jython (glob) + + $ ./jy-linux-x64 --dry-run -Dfoo=before -- -Dfoo=after + /*/bin/java -Dfoo=before -Dpython.import.site=false -Dpython.cachedir.skip=true -Dpython.console.encoding=UTF-8 -Djava.class.path=/*/lib/jython-2.7.3.jar* -Xmx* org.python.util.jython -Dfoo=after (glob) + + $ ./jy-linux-x64 -Dfoo=before -- -Dfoo=after -c 'from java.lang import System; print(System.getProperty("foo"))' + after + + $ ./jy-linux-x64 bad -- good 2>&1 | head -n1 + kotlin.IllegalStateException: Unrecognized JVM argument: bad + Test command line argument combinations. $ ./jy-linux-x64 --help