Created
August 16, 2020 15:22
-
-
Save NorfairKing/34dc62afa4dc6b02bcd56211c073bea1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( someFunc, | |
) | |
where | |
import qualified Data.Aeson as JSON | |
import qualified Data.Yaml as Yaml | |
import qualified Env | |
import Options.Applicative | |
import Text.Read | |
import qualified YamlParse.Applicative as YamlParse | |
someFunc :: IO () | |
someFunc = do | |
yamlparseTryout | |
-- optparseTryout | |
-- envparseTryout | |
yamlparseTryout :: IO () | |
yamlparseTryout = do | |
let tryItOut :: (Show a, Show i) => String -> YamlParse.Parser i a -> i -> IO () | |
tryItOut n p v = do | |
putStrLn $ unwords ["Parsing with", n <> ": ", show v] | |
case Yaml.parseMaybe (YamlParse.implementParser p) v of | |
Nothing -> putStrLn "Parsing failed" | |
Just r -> putStrLn $ unwords ["Parsing succeeded: ", show r] | |
let three n p = do | |
tryItOut n p Yaml.Null | |
tryItOut n p (JSON.toJSON "hello") | |
tryItOut n p (JSON.toJSON (5 :: Int)) | |
putStrLn "" | |
three "yamlInt" yamlInt | |
three "yamlInt'" yamlInt' | |
yamlInt :: YamlParse.Parser Yaml.Value Int | |
yamlInt = YamlParse.yamlSchema | |
yamlInt' :: YamlParse.Parser Yaml.Value (Maybe Int) | |
yamlInt' = YamlParse.ParseMaybe YamlParse.yamlSchema | |
-- In practice you use | |
-- | |
-- optionalField | |
-- | |
-- optionalFieldWithDefault | |
-- | |
-- requiredField | |
envparseTryout :: IO () | |
envparseTryout = do | |
let tryItOut :: Show a => String -> Env.Parser Env.Error a -> [(String, String)] -> IO () | |
tryItOut n p e = do | |
putStrLn $ unwords ["Parsing with", n <> ": ", show e] | |
case Env.parsePure p e of | |
Left f -> putStrLn $ unlines ["Failed: ", show f] | |
Right a -> putStrLn $ unwords ["Succeeded:", show a] | |
let three n p = do | |
tryItOut n p [] | |
tryItOut n p [("NUMBER", "hello")] | |
tryItOut n p [("NUMBER", "5")] | |
three "intVar" intVar | |
three "intVar'" intVar' | |
three "intVar''" intVar'' | |
intVar :: Env.Parser Env.Error Int | |
intVar = Env.var Env.auto "NUMBER" (Env.help "number") | |
-- WRONG: TODO make an issue about this | |
intVar' :: Env.Parser Env.Error (Maybe Int) | |
intVar' = optional $ Env.var Env.auto "NUMBER" (Env.help "number") | |
intVar'' :: Env.Parser Env.Error (Maybe Int) | |
intVar'' = Env.var (fmap Just . Env.auto) "NUMBER" (Env.help "number" <> Env.def Nothing) | |
optparseTryout :: IO () | |
optparseTryout = do | |
let tryItOut :: Show a => String -> Parser a -> [String] -> IO () | |
tryItOut n p s = do | |
let i = info p fullDesc | |
putStrLn $ unwords ["Parsing with", n <> ": ", show s] | |
let prefs = defaultPrefs {prefShowHelpOnError = True} | |
case execParserPure prefs i s of | |
Failure _ -> putStrLn "Parsing failed" | |
Success r -> putStrLn $ unwords ["Parsing succeeded:", show r] | |
_ -> error "completion" | |
putStrLn "" | |
let three n p = do | |
putStrLn $ concat ["---", n, "---"] | |
tryItOut n p [] | |
tryItOut n p ["--number", "hello"] | |
tryItOut n p ["--number", "5"] | |
three "intOption" intOption | |
three "intOption'" intOption'' | |
three "intOption''" intOption'' | |
three "intOption'''" intOption''' | |
-- Parse an int for the --int option. Fail if nothing was provided. | |
-- CORRECT. | |
intOption :: Parser Int | |
intOption = | |
option | |
auto | |
( mconcat [help "this is an int", long "number"] | |
) | |
-- Parse an int for the --int option. | |
-- CORRECT. | |
intOption' :: Parser (Maybe Int) | |
intOption' = | |
optional $ | |
option | |
auto | |
( mconcat [help "this is an int", long "number"] | |
) | |
-- Parse an int for the --int option. | |
-- CORRECT. | |
intOption'' :: Parser (Maybe Int) | |
intOption'' = | |
option | |
(Just <$> auto) | |
( mconcat [help "this is an int", long "number", value Nothing] | |
) | |
-- Parse an int for the --int option. | |
-- CORRECT. | |
intOption''' :: Parser (Maybe Int) | |
intOption''' = | |
option | |
(Just <$> (maybeReader readMaybe)) | |
( mconcat [help "this is an int", long "number"] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolver: lts-15.15 | |
packages: | |
- . | |
extra-deps: | |
- yamlparse-applicative-0.1.0.1 | |
- github: supki/envparse | |
commit: de5944fb09e9d941fafa35c0f05446af348e7b4d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment