How to use OptionParser

Hi *, I’m trying to write a command line application, but I’m somewhat stuck at using Optparse in the “right” way.
This is what I have right now: https://gist.github.com/ngw/cb9120d421b56894b5f159c84d08d747

What is very weird to me is that it appears to me that I have to add a case clause for every possible combination of options I’m going to have on the command line, and that is obviously not right.

Can anybody explain to me how to do this efficiently, maybe with an example of a project that solves this “problem” idiomatically, so I can learn? In Ruby I’m used to Thor (https://github.com/erikhuda/thor) which does a very good job at it, is there anything similar in Elixir?

Thanks a lot,
ngw

2 Likes

Ok, OptionParser.parse/2 does return a tuple of type {parsed, argv, errors}, parsed is a synonym for Keyword.t, so lets take a look at the Keyword-module, which does have a lot of stuff available.

1 Like

Thank you very much NobbZ, I managed to solve the problem and the code is, for my level, decent.

ngw

(Discourse did a bad job getting that line number…)

Won’t that expression on line 22 only parse through the options once, although it may work here since OptionParser has :help passed in first (I am unsure, but if the user passes in --from “blah” --help it could cause an issue that way?), it really should be iterated over or, in this case, the Keyword module should be used.