module OptparseApplicative.Simple.Parser
(
Parser,
argument,
showableArgument,
lenientArgument,
showableLenientArgument,
)
where
import BasePrelude
import Data.Text (Text)
import qualified Options.Applicative as A
import qualified Data.Attoparsec.Text as B
import qualified Data.Text as C
import qualified Attoparsec.Data as D
type Parser =
A.Parser
argument
:: Text
-> Maybe Char
-> Maybe Text
-> Maybe (a, Text)
-> B.Parser a
-> A.Parser a
argument longName shortName description defaultValue parser =
A.option readM mods
where
readM =
A.eitherReader (B.parseOnly parser . C.pack)
mods =
A.long (C.unpack longName) <>
foldMap A.short shortName <>
foldMap (A.help . C.unpack) description <>
foldMap (\(value, text) -> A.value value <> A.showDefaultWith (const (C.unpack text))) defaultValue
showableArgument
:: Show a
=> Text
-> Maybe Char
-> Maybe Text
-> Maybe a
-> B.Parser a
-> A.Parser a
showableArgument longName shortName description defaultValue parser =
argument longName shortName description (fmap (\ x -> (x, fromString (show x))) defaultValue) parser
lenientArgument :: D.LenientParser a => Text -> Maybe Char -> Maybe Text -> Maybe (a, Text) -> A.Parser a
lenientArgument longName shortName description defaultValue =
A.option readM mods
where
readM =
A.eitherReader (B.parseOnly D.lenientParser . C.pack)
mods =
A.long (C.unpack longName) <>
foldMap A.short shortName <>
foldMap (A.help . C.unpack) description <>
foldMap (\(value, text) -> A.value value <> A.showDefaultWith (const (C.unpack text))) defaultValue
showableLenientArgument :: (D.LenientParser a, Show a) => Text -> Maybe Char -> Maybe Text -> Maybe a -> A.Parser a
showableLenientArgument longName shortName description defaultValue =
lenientArgument longName shortName description (fmap (\ x -> (x, fromString (show x))) defaultValue)