elixir icon indicating copy to clipboard operation
elixir copied to clipboard

OptionParser and multiple instances of the same option

Open alexrp opened this issue 13 years ago • 8 comments

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?

alexrp avatar Jan 23 '13 14:01 alexrp

I agree that the last instance should have precedence.

rafaelfranca avatar Jan 23 '13 15:01 rafaelfranca

+1 liked the idea of the last option to be the valid one.

pedrosnk avatar Jan 23 '13 16:01 pedrosnk

Here is what we discussed on IRC:

  1. The default in the OptionParser will be to override previous entries, keeping the latest one. --time true --foo bar --time false should return [foo: "bar", time: false].
  2. A configuration option called :list will 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]
  3. Another option named :acc will 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).

josevalim avatar Jan 23 '13 16:01 josevalim

:+1:

rafaelfranca avatar Jan 23 '13 16:01 rafaelfranca

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? :)

josevalim avatar Jan 25 '13 15:01 josevalim

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.

alexrp avatar Jan 25 '13 15:01 alexrp

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!

josevalim avatar Jan 25 '13 15:01 josevalim

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. :)

josevalim avatar Feb 17 '13 02:02 josevalim