OptionParser and multiple instances of the same option
Consider:
iex(2)> OptionParser.parse(["--time", "true", "--time", "false"] switches: [time: :boolean])
{[time: true, time: false],[]}
For Boolean switches (among others), it is often the case that a command line utility only wants to consider the last instance of the switch.
Given the above, we wouldn't get what we'd expect:
iex(4)> (OptionParser.parse(["--time", "true", "--time", "false"], switches: [time: :boolean]) |> elem 0)[:time]
true
I wonder if it would make sense to have some mechanism that either a) indicates that only the last value given to an option should be included or b) indicates that all instance of an option should be reversed?
I agree that the last instance should have precedence.
+1 liked the idea of the last option to be the valid one.
Here is what we discussed on IRC:
- The default in the OptionParser will be to override previous entries, keeping the latest one.
--time true --foo bar --time falseshould return[foo: "bar", time: false]. - A configuration option called
:listwill keep the entries untouched in the list, without accumulating them:--time true --foo bar --time false, if time is list of booleans, would then return:[time: true, foo: "bar", time: false] - Another option named
:accwill also be added, which allows one to accumulate the values:--time true --foo bar --time false, if time is an acc of booleans, would then return:[time: [true, false], foo: "bar"](or[foo: "bar", time: [true, false]], I have no preference in this case).
:+1:
Hey @alexrp, you told me you are working on this. Since this will slightly change the behaviour of OptionParser, I would love to include it on 0.8.0 release. Do you think we will be able to? :)
Unfortunately I'm quite busy right now (and for the next couple of weeks) due to moving and picking up a new job. I don't think I can get this done for the 0.8.0 release.
No problem. After 0.8.0 is out is out we will be back to frequent releases, so we will have plenty of time to add this back and release. :) Thanks!
I have pushed this. I have implemented options 1) and 2) discussed above (except I used :keep instead of :list). 3) was not implemented, if someone needs it though, please go ahead. :)