[ new ] Support possible errors in options parsing #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TLDR: This PR adds a backward-compatible ability to parse particular option's bodies in the current getOpt with errors being recorded into the current Result type, rather than external aftermath errors parsing.
Existing simple options parsing facility is really nice, but since usually not every "body" of an option is acceptable, it is common to have the result of the option parsing to be Either , and after the parsing to analyse those Eithers. I've seen this pattern somewhere (i.e. in pack) and even implemented it in my code. After a while I thought of a helper function that would help with this pattern.
But this attempt showed that different errors of options parsing are present differently in the result -- the Result type already has an ability to represent some errors, but some other errors which are origined in those Eithers are passes differently.
But this seemed very strange to me, since these type of errors (basically, bad argument) do not seem to be more important than other (like lack of an arbitrary argument). So, I tried to integrate parsing checks into the main machinery.
This module is known to be used in several apps, so it seemed to be good to make a non-breaking change. Non-failing parsing functions are present in three places in the original module, in three data constructors. What I've done to embed parsing errors ability is that I added another three constructors (named with the original + the prime sign) that included String -> Either String a instead of original String -> a functions.
I thought of custom error data type to be supported by the parsing function, but this leads to a non-compatible change, so it was rejected internally.