From 67144b1fad7f4654d0e2ff13add1d75f7addbf5a Mon Sep 17 00:00:00 2001 From: Lokkie Date: Tue, 26 Dec 2017 23:11:09 +0200 Subject: [PATCH] Added skip unknown arguments mode --- .../kotlin/com/xenomachina/argparser/ArgParser.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt index 06dbb0e..4c929da 100644 --- a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt +++ b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt @@ -37,7 +37,8 @@ import kotlin.reflect.KProperty */ class ArgParser(args: Array, mode: Mode = Mode.GNU, - helpFormatter: HelpFormatter? = DefaultHelpFormatter()) { + helpFormatter: HelpFormatter? = DefaultHelpFormatter(), + val skipUnknown: Boolean = false) { enum class Mode { /** For GNU-style option parsing, where options may appear after positional arguments. */ @@ -543,7 +544,11 @@ class ArgParser(args: Array, } val delegate = longOptionDelegates.get(name) if (delegate == null) { - throw UnrecognizedOptionException(name) + if (!skipUnknown) { + throw UnrecognizedOptionException(name) + } else { + return 2 // @todo rewrite to correct count detection + } } else { var consumedArgs = delegate.parseOption(name, firstArg, index + 1, args) if (firstArg != null) { @@ -569,7 +574,11 @@ class ArgParser(args: Array, val delegate = shortOptionDelegates.get(optKey) if (delegate == null) { - throw UnrecognizedOptionException(optName) + if (!skipUnknown) { + throw UnrecognizedOptionException(optName) + } else { + return 2 // @todo rewrite to correct count detection + } } else { val firstArg = if (optIndex >= opts.length) null else opts.substring(optIndex) val consumed = delegate.parseOption(optName, firstArg, index + 1, args)