-
Notifications
You must be signed in to change notification settings - Fork 33
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
parser.adding(...).default(...) does not work #23
Comments
In case you need a workaround:
|
This is the intended behavior: That said, I am considering revamping how defaults work in a future release, and if there are issues with the current semantics, this would be the right time to adjust them. I'm curious to know the use-case here, so while I am closing this issue for now, please feel free to add comments if you'd like to influence that future redesign. |
The use-case is to "add" one or multiple arguments and to provide a default if no argument is passed. Why is this not a valid use-case? Does the library provide a way to achieve this? |
Sorry if I wasn't clear. I was not asking for the general behavior you want -- I already knew that. I'm asking for specific examples of where this would be useful. As for whether the library provides a way to achieve this: yes, but not as conveniently. The methods on fun <E, T : MutableCollection<E>> ArgParser.addingWithDefault(
name: String,
default: () -> T,
help: String,
argName: String,
initialValue: T,
transform: String.() -> E
): ArgParser.Delegate<T> {
return option<T>(
name,
help = help,
argNames = listOf(argName),
isRepeating = true) {
val result = value.orElse { initialValue }
result.add(transform(arguments.first()))
result
}.default(default())
} Now you can do something like: val differentWhenEmpty by parser.addingWithDefault(
"-X",
{ mutableListOf("def0", "def1", "def2") },
help = "list of things",
argName = "X",
initialValue = mutableListOf("init0", "init1", "init2")) { this } Not passing So you can do it, but it obviously isn't as convenient as using the existing An even simpler approach is to use adding, and then simply to treat the empty list as a case where you should use you default list instead. As I mentioned, if there are common cases where people actually need this behavior, then I'm happy to consider changing the behavior in a future release. So far I haven't seen any concrete examples, though. |
As
parser.adding(...)
creates default delegate by itself (ArgParser.kt:137), nested default delegates are confused as to whose default value should be used, and it does not work.The text was updated successfully, but these errors were encountered: