mirror of
https://github.com/corda/corda.git
synced 2025-01-18 10:46:38 +00:00
[CORDA-1597] Make the NodeArgsParser fail on unrecognized (non-option… (#3394)
* [CORDA-1597] Make the NodeArgsParser fail on unrecognized (non-option) arguments. * Respond to feedback from anthonykeenan.
This commit is contained in:
parent
381aadc78f
commit
66b67b231a
@ -60,6 +60,7 @@ class NodeArgsParser : AbstractArgsParser<CmdLineOptions>() {
|
|||||||
private val clearNetworkMapCache = optionParser.accepts("clear-network-map-cache", "Clears local copy of network map, on node startup it will be restored from server or file system.")
|
private val clearNetworkMapCache = optionParser.accepts("clear-network-map-cache", "Clears local copy of network map, on node startup it will be restored from server or file system.")
|
||||||
|
|
||||||
override fun doParse(optionSet: OptionSet): CmdLineOptions {
|
override fun doParse(optionSet: OptionSet): CmdLineOptions {
|
||||||
|
require(optionSet.nonOptionArguments().isEmpty()) { "Unrecognized argument(s): ${optionSet.nonOptionArguments().joinToString(separator = ", ")}"}
|
||||||
require(!optionSet.has(baseDirectoryArg) || !optionSet.has(configFileArg)) {
|
require(!optionSet.has(baseDirectoryArg) || !optionSet.has(configFileArg)) {
|
||||||
"${baseDirectoryArg.options()[0]} and ${configFileArg.options()[0]} cannot be specified together"
|
"${baseDirectoryArg.options()[0]} and ${configFileArg.options()[0]} cannot be specified together"
|
||||||
}
|
}
|
||||||
|
@ -14,18 +14,23 @@ abstract class AbstractArgsParser<out T : Any> {
|
|||||||
* If the help option is specified then the process is also shutdown after printing the help output to stdout.
|
* If the help option is specified then the process is also shutdown after printing the help output to stdout.
|
||||||
*/
|
*/
|
||||||
fun parseOrExit(vararg args: String): T {
|
fun parseOrExit(vararg args: String): T {
|
||||||
val optionSet = try {
|
try {
|
||||||
optionParser.parse(*args)
|
val optionSet = optionParser.parse(*args)
|
||||||
} catch (e: OptionException) {
|
if (optionSet.has(helpOption)) {
|
||||||
System.err.println(e.message ?: "Unable to parse arguments.")
|
optionParser.printHelpOn(System.out)
|
||||||
optionParser.printHelpOn(System.err)
|
exitProcess(0)
|
||||||
exitProcess(1)
|
}
|
||||||
|
return doParse(optionSet)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
when (e) {
|
||||||
|
is OptionException, is IllegalArgumentException -> {
|
||||||
|
System.err.println(e.message ?: "Unable to parse arguments.")
|
||||||
|
optionParser.printHelpOn(System.err)
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
else -> throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (optionSet.has(helpOption)) {
|
|
||||||
optionParser.printHelpOn(System.out)
|
|
||||||
exitProcess(0)
|
|
||||||
}
|
|
||||||
return doParse(optionSet)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parse(vararg args: String): T = doParse(optionParser.parse(*args))
|
fun parse(vararg args: String): T = doParse(optionParser.parse(*args))
|
||||||
|
@ -179,4 +179,18 @@ class NodeArgsParserTest {
|
|||||||
assertThat(cmdLineOptions.unknownConfigKeysPolicy).isEqualTo(onUnknownConfigKeyPolicy)
|
assertThat(cmdLineOptions.unknownConfigKeysPolicy).isEqualTo(onUnknownConfigKeyPolicy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `invalid argument`() {
|
||||||
|
assertThatExceptionOfType(IllegalArgumentException::class.java).isThrownBy {
|
||||||
|
parser.parse("foo")
|
||||||
|
}.withMessageContaining("Unrecognized argument(s): foo")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `invalid arguments`() {
|
||||||
|
assertThatExceptionOfType(IllegalArgumentException::class.java).isThrownBy {
|
||||||
|
parser.parse("foo", "bar")
|
||||||
|
}.withMessageContaining("Unrecognized argument(s): foo, bar")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user