Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added skip unknown arguments mode #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/kotlin/com/xenomachina/argparser/ArgParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import kotlin.reflect.KProperty
*/
class ArgParser(args: Array<out String>,
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. */
Expand Down Expand Up @@ -543,7 +544,11 @@ class ArgParser(args: Array<out String>,
}
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) {
Expand All @@ -569,7 +574,11 @@ class ArgParser(args: Array<out String>,

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)
Expand Down