hflags 0.1 → 0.1.1
raw patch · 3 files changed
+31/−24 lines, 3 filesdep +textdep ~basedep ~template-haskellPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: text
Dependency ranges changed: base, template-haskell
API changes (from Hackage documentation)
- HFlags: defineQQFlag :: String -> ExpQ -> String -> String -> Q [Dec]
+ HFlags: defineEQFlag :: String -> ExpQ -> String -> String -> Q [Dec]
+ HFlags: instance FlagType Text
Files
- HFlags.hs +19/−13
- examples/ComplexExample.hs +8/−8
- hflags.cabal +4/−3
HFlags.hs view
@@ -65,7 +65,7 @@ module HFlags ( -- * Definition of flags defineCustomFlag,- defineQQFlag,+ defineEQFlag, FlagType(..), -- * Initialization of flags at runtime initHFlags) where@@ -83,6 +83,7 @@ import Data.Maybe import qualified Data.Map as Map import Data.Map (Map, (!))+import qualified Data.Text import Language.Haskell.TH import System.Console.GetOpt import System.Environment@@ -90,7 +91,7 @@ import System.IO.Unsafe import System.Exit -import Prelude hiding (catch)+import Prelude -- | Data type for storing every property of a flag. data FlagData = FlagData@@ -121,13 +122,13 @@ -- -- * name of the flag (@l:long@ syntax if you want to have the short option @l@ for this flag), ----- * quasiquoted and type signed default value,+-- * expression quoted and type signed default value, -- -- * help string for the argument, ----- * read function, quasiquoted,+-- * read function, expression quoted, ----- * show function, quasiquoted,+-- * show function, expression quoted, -- -- * help string for the flag. defineCustomFlag :: String -> ExpQ -> String -> ExpQ -> ExpQ -> String -> Q [Dec]@@ -160,7 +161,7 @@ moduleName (evaluate $(varE accessorName) >> return ()) |]) []]]- flagPragmaDec <- return $ PragmaD $ InlineP accessorName $ InlineSpec False False Nothing+ flagPragmaDec <- return $ PragmaD $ InlineP accessorName NoInline FunLike AllPhases flagDec <- funD accessorName [clause [] (normalB [| case True of True -> $(appE readQ [| lookupFlag name moduleName |]) False -> $(defQ) |]) []]@@ -173,15 +174,15 @@ -- -- The parameters: ----- * name of the flag (@l:long@ syntax if you want to have the short option @l@ for this flag),,+-- * name of the flag (@l:long@ syntax if you want to have the short option @l@ for this flag), ----- * quasiquoted and type signed default value,+-- * expression quoted and type signed default value, -- -- * help string for the argument, -- -- * help string for the flag.-defineQQFlag :: String -> ExpQ -> String -> String -> Q [Dec]-defineQQFlag name defQ argHelp description =+defineEQFlag :: String -> ExpQ -> String -> String -> Q [Dec]+defineEQFlag name defQ argHelp description = defineCustomFlag name defQ argHelp [| read |] [| show |] description -- | Class of types for which the easy 'defineFlag' syntax is supported.@@ -216,16 +217,21 @@ defineFlag n v = defineCustomFlag n [| v :: Bool |] "BOOL" [| boolRead |] [| boolShow |] instance FlagType Int where- defineFlag n v = defineQQFlag n [| v :: Int |] "INT"+ defineFlag n v = defineEQFlag n [| v :: Int |] "INT" instance FlagType Integer where- defineFlag n v = defineQQFlag n [| v :: Integer |] "INTEGER"+ defineFlag n v = defineEQFlag n [| v :: Integer |] "INTEGER" instance FlagType String where defineFlag n v = defineCustomFlag n [| v :: String |] "STRING" [| id |] [| id |] instance FlagType Double where- defineFlag n v = defineQQFlag n (sigE (litE (RationalL (toRational v))) [t| Double |] ) "DOUBLE"+ defineFlag n v = defineEQFlag n (sigE (litE (RationalL (toRational v))) [t| Double |] ) "DOUBLE"++instance FlagType Data.Text.Text where+ defineFlag n v =+ let s = Data.Text.unpack v+ in defineCustomFlag n [| Data.Text.pack s :: Data.Text.Text |] "TEXT" [| Data.Text.pack |] [| Data.Text.unpack |] -- | A global "IORef" for the communication between @initHFlags@ and -- @flag_*@. This is a map between flag name and current value.
examples/ComplexExample.hs view
@@ -1,5 +1,5 @@ #!/usr/bin/env runhaskell- + {-# LANGUAGE TemplateHaskell #-} import Control.Monad@@ -21,27 +21,27 @@ -- For flags with a more complex type, where the existing Read and -- Show is suitable for parsing of the argument value, you can use the--- defineQQFlag syntax. Here, the QQ is mnemonic for QuasiQuoter,--- because you have to quasiquote the default value, and type sign it--- inside the quasiquote. Also, you have to provide the name of the+-- defineEQFlag syntax. Here, the EQ is mnemonic for expression+-- quotes, because you have to expression quote the default value, and+-- type sign it inside. Also, you have to provide the name of the -- type for --help generation.-defineQQFlag "numbers_to_sum" [| [1,2,3,4,5] :: [Int] |] "INT_LIST" "Print the sum of these numbers."+defineEQFlag "numbers_to_sum" [| [1,2,3,4,5] :: [Int] |] "INT_LIST" "Print the sum of these numbers." -- This works quite well for simple enums too! data Color = Red | Yellow | Green deriving (Show, Read)-defineQQFlag "favorite_color" [| Yellow :: Color |] "COLOR" "Your favorite color."+defineEQFlag "favorite_color" [| Yellow :: Color |] "COLOR" "Your favorite color." -- Sometimes the default read or show instance is not good enough for -- your needs. In that case you can use defineCustomFlag to override -- those functions. data Language = English | Hungarian defineCustomFlag "language" [| English :: Language |] "en|hu"- [| \s -> case s of + [| \s -> case s of "en" -> English "hu" -> Hungarian _ -> error $ "Unknown language: " ++ s |]- [| \l -> case l of + [| \l -> case l of English -> "en" Hungarian -> "hu" |]
hflags.cabal view
@@ -1,5 +1,5 @@ name: hflags-version: 0.1+version: 0.1.1 license: OtherLicense license-file: COPYING author: Mihaly Barasz <klao@google.com>, Gergely Risko <errge@google.com>@@ -68,9 +68,10 @@ ghc-options: -Wall build-depends:- base >= 4.5 && < 5+ base >= 4.6 && < 5 , containers >= 0.4- , template-haskell >= 2.7+ , template-haskell >= 2.8+ , text >= 0.11 exposed-modules: HFlags