Thanks NobbZ for the explanation about the atoms, that makes sense, and I will write my code in that way:
Testing again on the iex shell:
iex(1)> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"])
# parse:got here ["--source-path", "lib", "test/enum_test.exs", "--verbose"] opts: []
{[verbose: true], ["test/enum_test.exs"], [{"--source-path", "lib"}]}
iex(2)> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--no-verbose"])
# parse:got here ["--source-path", "lib", "test/enum_test.exs", "--no-verbose"] opts: []
{[], ["test/enum_test.exs"], [{"--source-path", "lib"}, {"--no-verbose", nil}]}
iex(3)> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--no-verbose"], switches: [{:verbose, :boolean}, {:"source_path", :string}])
# parse:got here ["--source-path", "lib", "test/enum_test.exs", "--no-verbose"] opts: [switches: [verbose: :boolean, source_path: :string]]
{[source_path: "lib", verbose: false], ["test/enum_test.exs"], []}
iex(4)> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"])
# parse:got here ["--source-path", "lib", "test/enum_test.exs", "--verbose"] opts: []
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
- but it still doesnât explain the other thing - which is why the doctests still pass? No prior declaration of
:"source_path"
atom anywhere beforehand
1b. The example in the doctests, which canât be reproduced at first use on the iex shell, is propagated into generated docs.
- From my test in the iex shell, it seems that repeated function calls with the same arguments will yield different results (I suppose, because I âaccidentallyâ had ended up declaring the atom
:"source_path"
in the shell).
This seems surprising to the idea of functional programming as youâd expect function calls with same arguments to return the same result.
To reiterate, to fulfill my current usage, if I declare the atoms beforehand with switches
param it looks like Iâd be ok, or if I pick up the âstringâ values from the âinvalidâ part of parse
functionâs return value.
Apart from that, the behaviour, doctests and the docs is confusing and requires one to know something outside from the docs beforehand.