envparse 0.2.1 → 0.2.2
raw patch · 7 files changed
+52/−25 lines, 7 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Env: helpDoc :: Parser a -> String
Files
- LICENSE +1/−1
- envparse.cabal +5/−3
- src/Env.hs +8/−3
- src/Env/Free.hs +7/−1
- src/Env/Help.hs +22/−15
- src/Env/Parse.hs +3/−0
- src/Env/Val.hs +6/−2
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2014, Matvey Aksenov+Copyright (c) 2014-2015, Matvey Aksenov All rights reserved.
envparse.cabal view
@@ -1,5 +1,5 @@ name: envparse-version: 0.2.1+version: 0.2.2 synopsis: Parse environment variables description: Here's a simple example of a program that uses @envparse@'s parser:@@ -49,7 +49,7 @@ license-file: LICENSE author: Matvey Aksenov maintainer: matvey.aksenov@gmail.com-copyright: 2014 Matvey Aksenov+copyright: 2015 Matvey Aksenov category: System build-type: Simple cabal-version: >= 1.10@@ -65,7 +65,7 @@ source-repository this type: git location: https://github.com/supki/envparse- tag: 0.2.1+ tag: 0.2.2 library default-language:@@ -99,3 +99,5 @@ test main-is: Spec.hs+ ghc-options:+ -Wall
src/Env.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} -- | Here's a simple example of a program that uses @envparse@'s parser: --@@ -64,6 +65,7 @@ , Flag , HasHelp , help+ , helpDoc -- * Re-exports -- $re-exports , pure, (<$>), (<*>), (*>), (<*), optional@@ -79,12 +81,15 @@ import Control.Applicative import Control.Monad ((>=>), (<=<)) import Data.Foldable (asum)-import Data.Monoid (Monoid(..), (<>))+#if __GLASGOW_HASKELL__ < 710+import Data.Monoid (Monoid(..))+#endif+import Data.Monoid ((<>)) import System.Environment (getEnvironment) import System.Exit (exitFailure) import qualified System.IO as IO -import Env.Help (helpDoc)+import Env.Help (helpInfo, helpDoc) import Env.Parse -- $re-exports@@ -116,7 +121,7 @@ -- | Try to parse a pure environment parsePure :: Mod Info a -> Parser a -> [(String, String)] -> Either String a-parsePure (Mod f) p = mapLeft (helpDoc (f defaultInfo) p) . static p+parsePure (Mod f) p = mapLeft (helpInfo (f defaultInfo) p) . static p mapLeft :: (a -> b) -> Either a t -> Either b t mapLeft f = either (Left . f) Right
src/Env/Free.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-}@@ -12,8 +13,13 @@ , inspect ) where -import Control.Applicative (Applicative(..), Alternative(..))+#if __GLASGOW_HASKELL__ < 710+import Control.Applicative (Applicative(..))+#endif+import Control.Applicative (Alternative(..))+#if __GLASGOW_HASKELL__ < 710 import Data.Monoid (Monoid(..))+#endif data Alt f a where
src/Env/Help.hs view
@@ -1,28 +1,32 @@ {-# LANGUAGE NamedFieldPuns #-} module Env.Help- ( helpDoc+ ( helpInfo+ , helpDoc ) where import qualified Data.List as List import qualified Data.Map as Map import Data.Maybe (catMaybes)-import Data.Monoid ((<>)) import Data.Ord (comparing) import Env.Free import Env.Parse -helpDoc :: Info a -> Parser b -> [Error] -> String-helpDoc Info { infoHeader, infoDesc, infoFooter } p fs =- List.intercalate "\n\n" . catMaybes $+helpInfo :: Info a -> Parser b -> [Error] -> String+helpInfo Info { infoHeader, infoDesc, infoFooter } p errors =+ List.intercalate "\n\n" $ catMaybes [ infoHeader , fmap (List.intercalate "\n" . splitWords 50) infoDesc- , Just "Available environment variables:"- , Just (List.intercalate "\n" (helpParserDoc p))+ , Just (helpDoc p) , fmap (List.intercalate "\n" . splitWords 50) infoFooter- ] ++ map Just (helpFailuresDoc fs)+ ] ++ helpErrors errors +-- | A pretty-printed list of recognized environment variables suitable for usage messages.+helpDoc :: Parser a -> String+helpDoc p =+ List.intercalate "\n" ("Available environment variables:\n" : helpParserDoc p)+ helpParserDoc :: Parser a -> [String] helpParserDoc = concat . Map.elems . foldAlt (\v -> Map.singleton (varfName v) (helpVarfDoc v)) . unParser @@ -39,13 +43,16 @@ where k = length varfName t = maybe h (\s -> h ++ " (default: " ++ s ++")") varfHelpDef -helpFailuresDoc :: [Error] -> [String]-helpFailuresDoc [] = []-helpFailuresDoc fs = ["Parsing errors:", List.intercalate "\n" (map helpFailureDoc (List.sortBy (comparing varName) fs))]+helpErrors :: [Error] -> [String]+helpErrors [] = []+helpErrors fs =+ [ "Parsing errors:"+ , List.intercalate "\n" (map helpError (List.sortBy (comparing varName) fs))+ ] -helpFailureDoc :: Error -> String-helpFailureDoc (ParseError n e) = " " ++ n ++ " cannot be parsed: " ++ e-helpFailureDoc (ENoExistError n) = " " ++ n ++ " is unset"+helpError :: Error -> String+helpError (ParseError n e) = " " ++ n ++ " cannot be parsed: " ++ e+helpError (ENoExistError n) = " " ++ n ++ " is unset" varName :: Error -> String varName (ParseError n _) = n@@ -66,4 +73,4 @@ prep acc = [unwords (reverse acc)] indent :: Int -> String -> String-indent n s = replicate n ' ' <> s+indent n s = replicate n ' ' ++ s
src/Env/Parse.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ViewPatterns #-}@@ -32,7 +33,9 @@ import Control.Applicative import Data.Map (Map) import qualified Data.Map as Map+#if __GLASGOW_HASKELL__ < 710 import Data.Monoid (Monoid(..))+#endif import Data.String (IsString(..)) import Env.Free
src/Env/Val.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveFunctor #-} module Env.Val ( Val(..)@@ -6,10 +7,13 @@ ) where import Control.Applicative-import Data.Monoid (Monoid(..), (<>))+#if __GLASGOW_HASKELL__ < 710+import Data.Monoid (Monoid(..))+#endif+import Data.Monoid ((<>)) --- | An isomorphic to 'Either' type with the accumulating 'Applicative' instance+-- | A type isomorphic to 'Either' with the accumulating 'Applicative' instance. data Val e a = Err e | Ok a