Copyright | (c) Ben Hamlin 2017 |
---|---|
License | MIT |
Maintainer | protob3n@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | Safe |
Language | Haskell2010 |
This is yet another entry in Haskell's enourmous collection of config-file parsing libraries. It lacks many of the bells and whistles of other config-file parsing libraries, such as hierarchical sections and on-the-fly reloading. On the other hand, it has a combination of features I was unable to find in other libraries:
- Keys and values are parsed with configurable parsec parsers, resulting in flexible syntax and pretty error messages.
- Custom parsers can be created with parsec to handle values of any type.
- Keys that aren't explicitly handled result in parse errors.
If you don't need all of these features, there are probably better libraries out there for you. If you're free to use its idiosyncratic file format, the config-value library, in particular, is excelent.
Example:
By default, this library parses flat config like the following:
a_string = "blah, blah, blah\nmore blah" a_number = 9001 a_list = [1,2,3,4,5] # This is a comment
If you wanted to parse the above file, saved as ./config.txt
, you might do so
as follows:
import Text.ConfigParser cp :: ConfigParser (Maybe String, Maybe Integer, [Integer]) cp = configParser (Nothing, Nothing, []) [ ConfigOption { key = "a_string" , parser = string , action = \s (_,n,ns) -> (Just s, n, ns) } , ConfigOption { key = "a_number" , parser = integer , action = \n (s,_,ns) -> (s, Just n, ns) } , ConfigOption { key = "a_list" , parser = list integer , action = \ns (s,n,_) -> (s, n, ns) } ] main :: IO () main = parseFromFile cp "./config.txt" >>= print
- type Key = String
- data ConfigOption c = ConfigOption {}
- data ConfigParser c = ConfigParser {
- keyValue :: forall a. Parser Key -> Parser a -> Parser a
- lineCommentInit :: [String]
- keyIdentifier :: Parser Key
- defaults :: c
- options :: [ConfigOption c]
- optionalCO :: Key -> Parser a -> (a -> c -> c) -> ConfigOption c
- requiredCO :: Key -> Parser a -> (a -> c -> c) -> ConfigOption c
- configParser :: c -> [ConfigOption c] -> ConfigParser c
- defaultKeyValue :: Parser Key -> Parser a -> Parser a
- defaultLineCommentInit :: [String]
- config :: ConfigParser c -> Parser c
- string :: IsString s => Parser s
- integer :: Parser Integer
- boundedIntegral :: forall n. (Show n, Bounded n, Integral n) => Parser n
- bool :: Parser Bool
- list :: Parser a -> Parser [a]
- parseFromText :: ConfigParser c -> SourceName -> Text -> Either ParseError c
- parseFromFile :: ConfigParser c -> SourceName -> IO (Either ParseError c)
Documentation
data ConfigOption c Source #
data ConfigParser c Source #
Parameters for a parser that takes a config file and produces a c
. Use
the ConfigParser
constructor if you want to specify your own lineParser
or commentStart
. Otherwise, use the configParser
smart constructor.
ConfigParser | |
|
optionalCO :: Key -> Parser a -> (a -> c -> c) -> ConfigOption c Source #
requiredCO :: Key -> Parser a -> (a -> c -> c) -> ConfigOption c Source #
configParser :: c -> [ConfigOption c] -> ConfigParser c Source #
Smart constructor for a ConfigParser
that uses a default syntax like
key = value
and line comments starting with #
.
defaultLineCommentInit :: [String] Source #
Default line comment like # comment text
.
config :: ConfigParser c -> Parser c Source #
Parse a config file as specified by a ConfigParser
.
string :: IsString s => Parser s Source #
Parse a string surrounded by quotes. Quotes within the string must be escaped with backslashes.
boundedIntegral :: forall n. (Show n, Bounded n, Integral n) => Parser n Source #
Parse a bounded integer. Fail to parse with a descriptive message if the value is out of bounds.
Parse a boolean. Valid synonyms for True
are true
, yes
, Yes
, on
,
and On
. Valid synonyms for False
are false
, no
, No
, off
, and
Off
.
list :: Parser a -> Parser [a] Source #
Parse a list of values surrounded by [
and ]
, and separated by commas.
The list can contain whitespace and newlines.
parseFromText :: ConfigParser c -> SourceName -> Text -> Either ParseError c Source #
parseFromFile :: ConfigParser c -> SourceName -> IO (Either ParseError c) Source #