packages feed

postgresql-simple-opts 0.1.0.2 → 0.1.0.3

raw patch · 3 files changed

+16/−4 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

postgresql-simple-opts.cabal view
@@ -1,5 +1,5 @@ name:                postgresql-simple-opts-version:             0.1.0.2+version:             0.1.0.3 synopsis:            An optparse-applicative parser for postgresql-simple's connection options description:         This package exports a optparse-applicative parser and type for postgresql-simple's ConnectInfo and connection string. homepage:            https://github.com/jfischoff/postgresql-simple-opts#readme
src/Database/PostgreSQL/Simple/Options.lhs view
@@ -2,7 +2,7 @@  There are many solutions for parsing command line arguments in Haskell. Personally I like [`optparse-applicative`](https://hackage.haskell.org/package/optparse-applicative-0.12.1.0/), because, like the title suggests, you can compose parsers out of smaller pieces. -I have written command line parsers for the database connection info for [`postgresql-simple`](https://hackage.haskell.org/package/postgresql-simple-0.5.2.1/) many times and faced with the prospect of doing it again I opted to make a library. This way I could reuse it in web servers, db migrators, db job runners ... those are all the examples I could think of ... just trust me, it's worth it.+I have written command line parsers for the database connection info for [`postgresql-simple`](https://hackage.haskell.org/package/postgresql-simple-0.5.2.1/) many times and faced with the prospect of doing it again I opted to make this library, which is also this literate Haskell file. This way I could reuse it in web servers, db migrators, db job runners ... those are all the examples I could think of ... just trust me, it's worth it.  ### Outline - [The "Partial" Option Types](#partial)@@ -16,6 +16,9 @@ ### Standard Intro Statements to Ignore  ```haskell+{-| A resuable optparse-applicative parser for creating a postgresql-simple+   'Connection'  +|-} {-# LANGUAGE RecordWildCards, LambdaCase, DeriveGeneric, DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving, CPP #-} module Database.PostgreSQL.Simple.Options where@@ -29,16 +32,17 @@ import Data.Typeable import Data.String #if !MIN_VERSION_base(4,8,0)-import Control.Applicative import Data.Monoid #endif ```  ### <a name="partial"> The "Partial" Option Types -In general, options types are built from many optional fields. Additionally, multiple options sets can be combined (e.g. command line options, config file, environment vars, defaults, etc). The easiest way to handle this is to create a "partial" option family that can be monoidally composed and is "completed" with a default option value.+In general, options types are built from many optional fields. Additionally, multiple options sets can be combined (i.e. command line options, config file, environment vars, defaults, etc). The easiest way to handle this is to create a "partial" option family that can be monoidally composed and is "completed" with a default option value.  ```haskell+-- | An optional version of 'ConnectInfo'. This includes an instance of+-- | 'ParseRecord' which provides the optparse-applicative Parser. data PartialConnectInfo = PartialConnectInfo   { host     :: Last String   , port     :: Last Int@@ -128,6 +132,7 @@ We can use `PartialOptions` as the type of a field in a larger options record defined elsewhere. When defining this more complicated parser, we reuse the work we did here by calling `parseRecord`. To make it even clearer we create an alias called `parser` so clients will know what to use.  ```haskell+-- | The main parser to reuse. parser :: Parser PartialOptions parser = parseRecord ```@@ -179,6 +184,8 @@ Completing a `PartialOptions` to get an `Options` follows straightforwardly ... if you've done this a bunch I suppose.  ```haskell+-- | mappend with 'defaultPartialConnectInfo' if necessary to create all+--   options completeOptions :: PartialOptions -> Options completeOptions = \case   POConnectString   (ConnectString x) -> OConnectString x@@ -190,6 +197,7 @@ Parse a `PartialOptions` and then complete it. This is **not** composable but is convient for testing and if you only need a `Option` type  ```haskell+-- | Useful for testing or if only Options are needed. completeParser :: Parser Options completeParser = fmap completeOptions parseRecord ```@@ -199,6 +207,7 @@ As a convenience, we export the primary use of parsing connection options ... making a connection.  ```haskell+-- | Create a connection with an 'Option' run :: Options -> IO Connection run = \case   OConnectString connString -> connectPostgreSQL connString
test/Spec.hs view
@@ -5,6 +5,9 @@ import System.Environment import Options.Applicative import System.Exit+#if !MIN_VERSION_base(4,8,0)+import Data.Monoid+#endif  testParser :: IO Options testParser = execParser $ info completeParser mempty