Hi everyone, I did not find a quick and simple com...
# feed
l
Hi everyone, I did not find a quick and simple command line argument parser. I've taken the gnu argument syntax and made one. Here is my take on it fitting in a single function: https://gist.github.com/L-Briand/15ddf5787ba8e78f842b3de90c3bf10c
d
Not bad. I would suggest a change to the callback structure.
Copy code
class Argument(val name: String?, getValue: ()->String?) {
   val value by lazy { getValue() }
}
and have the callback: be
Argument.() -> Unit
That way just makes the usage a little cleaner:
Copy code
gnuParse(args) {
   when (name) {
       "n", "name" -> name = value ?: "default"
       "e", "enable" -> bar = true   
   }
}
l
I like the idea for simple one-on-one argument's value. However, for multi element argument like
--input file1 file2
I think getValue is more correct.
d
Hmm, that's true, but how would the client code know how many times to call getValue?
l
getValue returns null if it encounter a value starting with a hyphen (in args) or reach the end of the args elements.
d
Ah. In that case, you could have a
values
sequence instead or along side.
Anyway, just a thought.
e
how about Clikt? it's a bit more than just plain parsing, but it has great functionality and quite easy to use
plus1 3
d
+1 for Clikt I'm currently working with it to build a command line client to complement a graphical client in my project; it's well thought-out and easy to use.
a
just curious, did you find
org.jetbrains.kotlinx:kotlinx-cli
to not fit your needs?
d
No, I didn't find that - only because I wasn't aware of its existence.
Will check it out - thanks!
a
ive been using it and liking it, however I just noticed that it's apparently abandoned now 👀
that too bad, it was pretty nice to work with!
d
Ah, ok... try clikt; 'official' is always nice, but there's only so much they can support I guess.
I know clikt has grown popular, so JB may have consciously decided to defer to that and place their efforts elsewhere.
a
ya
m
If you're targeting the JVM then PicoCLI is excellent.