Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ new ] Support possible errors in options parsing #2

Merged
merged 1 commit into from
Oct 28, 2023

Conversation

buzden
Copy link
Member

@buzden buzden commented Oct 27, 2023

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.

This is a backward-compatible way, so all the original data constructors
left unchanged, and new ones with new semantics are added
@buzden buzden added the enhancement New feature or request label Oct 27, 2023
@stefan-hoeck stefan-hoeck merged commit 0d41b98 into main Oct 28, 2023
7 checks passed
@stefan-hoeck stefan-hoeck deleted the integrated-parsing-errors branch October 28, 2023 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants