From 26e9cf33ee09c8e5d0a59a1e67bc3965ce1be998 Mon Sep 17 00:00:00 2001 From: NoakPalander Date: Tue, 31 May 2022 01:41:44 +0200 Subject: [PATCH 1/3] extended optional to allow for hardcoded 'acceptable' values for a storing-like delegate --- .../com/xenomachina/argparser/ArgParser.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt index 7375cca..75cb231 100644 --- a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt +++ b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt @@ -254,6 +254,43 @@ class ArgParser( return delegate } + /** + * Creates a Delegate for an option with a list of hardcoded acceptable values + * @param names names of options, with leading "-" or "--" + * @param errorName name to use when talking about this option in error messages, or null to base it upon the + * option names + * @param help the help text for this option + * @param values the valid values the argument can have + * @param transform transforms the value after parsing + */ + fun option( + vararg names: String, + help: String, + errorName: String?, + values: List, + transform: String.() -> T): Delegate { + + return option( + *names, + errorName = errorName, + help = help + ) { + if (!values.contains(arguments.first())) + throw SystemExitException("Invalid option '${arguments.first()}', valid options are: $values", -1) + + transform(arguments.first()) + } + } + + /** + * Creates a Delegate for an optional with hardcoded acceptable values, which returns the argument's value + */ + fun option( + vararg names: String, + errorName: String? = null, + values: List, + help: String): Delegate = option(*names, errorName = errorName, values = values, help = help) { this } + /** * Creates a Delegate for a single positional argument which returns the argument's value. */ From c5f53696a91f9a01f335c4713f681c3b172fabbc Mon Sep 17 00:00:00 2001 From: NoakPalander Date: Tue, 31 May 2022 01:58:17 +0200 Subject: [PATCH 2/3] defaulted the errorName parameter to null --- src/main/kotlin/com/xenomachina/argparser/ArgParser.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt index 75cb231..b356e7f 100644 --- a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt +++ b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt @@ -266,7 +266,7 @@ class ArgParser( fun option( vararg names: String, help: String, - errorName: String?, + errorName: String? = null, values: List, transform: String.() -> T): Delegate { From c94086aa8bded8922125093191d0bfbb9910cbbd Mon Sep 17 00:00:00 2001 From: NoakPalander Date: Tue, 31 May 2022 02:36:04 +0200 Subject: [PATCH 3/3] fixed nonNullArgName --- src/main/kotlin/com/xenomachina/argparser/ArgParser.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt index b356e7f..48440f3 100644 --- a/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt +++ b/src/main/kotlin/com/xenomachina/argparser/ArgParser.kt @@ -270,9 +270,12 @@ class ArgParser( values: List, transform: String.() -> T): Delegate { + val nonNullArgName = optionNameToArgName(selectRepresentativeOptionName(names)) + return option( *names, errorName = errorName, + argNames = listOf(nonNullArgName), help = help ) { if (!values.contains(arguments.first()))