mr-env: A simple way to read environment variables in Haskell

[ library, mit, system ] [ Propose Tags ]

A simple way to read environment variables with fallback values in Haskell.


[Skip to Readme]
Versions [faq] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4
Change log CHANGELOG.md
Dependencies base (==4.*) [details]
License MIT
Copyright 2020 Christian Rocha
Author Christian Rocha
Maintainer christian@rocha.is
Category System
Home page https://github.com/meowgorithm/mr-env#readme
Bug tracker https://github.com/meowgorithm/mr-env/issues
Source repo head: git clone https://github.com/meowgorithm/mr-env
Uploaded by meowgorithm at 2020-07-09T13:42:46Z
Distributions NixOS:0.1.0.4
Downloads 564 total (35 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2020-07-09 [all 1 reports]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for mr-env-0.1.0.4

[back to package description]

Mr. Env

build Hackage

A simple way to read environment variables in Haskell.

-- Read environment variables, with defaults
host <- envAsString "HOST" "localhost"
port <- envAsInt "PORT" 8000

Simple Example

Read environment variables with do notation:

import System.Environment.MrEnv ( envAsBool, envAsInt, envAsInteger, envAsString )

main :: IO ()
main = do

    -- Get a string, with a fallback value if nothing is set.
    host <- envAsString "HOST" "localhost"

    -- Get an int. If you need an integer instead you could also use envAsInteger.
    port <- envAsInt "PORT" 8000

    -- Get a boolean. Here we're expecting the environment variable to read
    -- something along the lines of "true", "TRUE", "True", "truE" and so on.
    debug <- envAsBool "DEBUG" False

    putStrLn $
        "Let's connect to "
        ++ host
        ++ " on port "
        ++ show port
        ++ ". Debug mode is "
        ++ if debug then "on" else "off"
        ++ "."

Fancy Example

Read environment variables into a record:

import System.Environment.MrEnv ( envAsBool, envAsInt, envAsInteger, envAsString )

data Config =
    Config { host  :: String
           , port  :: Int
           , debug :: Bool
           }

getConfig :: IO Config
getConfig = Config
    <$> envAsString "HOST" "localhost"
    <*> envAsInt "PORT" 8000
    <*> envAsBool "DEBUG" False

main :: IO ()
main =
    getConfig >>= \conf ->
        putStrLn $
            "Let's connect to "
            ++ host conf
            ++ " on port "
            ++ show $ port conf
            ++ ". Debug mode is "
            ++ if debug conf then "on" else "off"
            ++ "."

We suggest pronouncing <*> brackety-splat (as opposed to ap). In that vein, <$> is brackety-cash.

Authors

License

MIT