You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure whether it's something that should be part of the core of argparser but I came across a usecase which consists of multiple arguments (flags) being collected to obtain a set of switches (and allowing this set of switches to be validated).
I monkey patched it as this:
fun <E, T:MutableCollection<E>> ArgParser.addMapping(
map:Map<String, E>,
help:String,
initialValue:T): ArgParser.Delegate<T> {
val names = map.keys.toTypedArray()
return option<T>(*names,
errorName = map.keys.joinToString("|"),
help = help,
isRepeating =true) {
val result = value.orElse { initialValue }
result.add(map.getValue(optionName))
result
}.default(initialValue)
}
fun <E> ArgParser.addMapping(map:Map<String, E>, help:String): ArgParser.Delegate<MutableCollection<E>> =
addMapping(map, help, mutableSetOf())
fun <E, T:MutableCollection<E>> ArgParser.addMapping(varargpairs:Pair<String, E>, help:String, initialValue:T): ArgParser.Delegate<T> =
addMapping(mapOf(*pairs), help, initialValue)
fun <E> ArgParser.addMapping(varargpairs:Pair<String, E>, help:String): ArgParser.Delegate<MutableCollection<E>> =
addMapping(mapOf(*pairs), help, mutableSetOf())
Here's an example of the usage:
privateval dumps:MutableCollection<DumpType> by commandParser.addMapping(
"--heap" to DumpType.HEAP_DUMP,
"--thread" to DumpType.THREAD_DUMP,
"--database" to DumpType.THREAD_DUMP, help ="Type of dump to process (more than one type is allowed)")
.addValidator { if (value.isEmpty()) throwInvalidArgumentException("At least one dump type must be selected") }
What do you think?
The text was updated successfully, but these errors were encountered:
That is definitely an interesting example! I'm just not sure how much this adds over simply having a separate flag for each dump type, and then validating that at least one is set later on.
value.isEmpty() is definitely nicer than !(heap || thread || database) (especially when it's possible that more of these may be added later), but is it enough to warrant adding this to the API?
(I was actually somewhat on the fence about the existing mapping method.)
Not sure whether it's something that should be part of the core of argparser but I came across a usecase which consists of multiple arguments (flags) being collected to obtain a set of switches (and allowing this set of switches to be validated).
I monkey patched it as this:
Here's an example of the usage:
What do you think?
The text was updated successfully, but these errors were encountered: