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

Improve the error message on bad option values #9

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/System/MemInfo/Choices.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import Options.Applicative (
Parser,
ParserInfo,
ReadM,
auto,
eitherReader,
execParser,
help,
Expand Down Expand Up @@ -149,7 +148,7 @@ positiveNum =
| i > 0 = pure i
| otherwise = readerError "Value must be greater than 0"
in
auto >>= checkPositive
autoOrNotAllowed >>= checkPositive


parsePrintOrder :: Parser PrintOrder
Expand Down Expand Up @@ -197,4 +196,16 @@ data Style


autoIgnoreCase :: (Read a) => ReadM a
autoIgnoreCase = eitherReader $ readEither . Text.unpack . Text.toTitle . Text.pack
autoIgnoreCase =
let toTitle' = Text.unpack . Text.toTitle . Text.pack
in eitherReader $ readOrNotAllowed toTitle'


autoOrNotAllowed :: (Read a) => ReadM a
autoOrNotAllowed = eitherReader $ readOrNotAllowed id


readOrNotAllowed :: (Read a) => (String -> String) -> String -> Either String a
readOrNotAllowed f x = case readEither $ f x of
Left _ -> Left $ "value '" ++ x ++ "' is not permitted"
right -> right
Loading