Skip to content

Commit

Permalink
Merge pull request #9 from adetokunbo/improve-option-parse-error-message
Browse files Browse the repository at this point in the history
Improve the error message on bad option values
  • Loading branch information
adetokunbo authored Feb 6, 2024
2 parents 40cef76 + 386c3c2 commit 5cff0c3
Showing 1 changed file with 14 additions and 3 deletions.
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

0 comments on commit 5cff0c3

Please sign in to comment.