Skip to content

Commit

Permalink
Improve the error message on bad option values
Browse files Browse the repository at this point in the history
  • Loading branch information
adetokunbo committed Feb 6, 2024
1 parent 40cef76 commit 29dead0
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
autoNicerError >>= 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 $ readEither' toTitle'


autoNicerError :: (Read a) => ReadM a
autoNicerError = eitherReader $ readEither' id


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

0 comments on commit 29dead0

Please sign in to comment.