hflags 0.1.1 → 0.1.2
raw patch · 3 files changed
+38/−11 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ HFlags: arguments :: [String]
Files
- HFlags.hs +29/−6
- examples/SimpleExample.hs +3/−1
- hflags.cabal +6/−4
HFlags.hs view
@@ -1,4 +1,5 @@ -- Copyright 2012 Google Inc. All Rights Reserved.+-- Copyright 2013 and onwards, Gergely Risko -- -- Licensed under the Apache License, Version 2.0 (the "License"); -- you may not use this file except in compliance with the License.@@ -12,7 +13,7 @@ -- See the License for the specific language governing permissions and -- limitations under the License. ----- Authors: Mihaly Barasz <klao@google.com>, Gergely Risko <errge@google.com>+-- Authors: Mihaly Barasz <klao@google.com>, Gergely Risko <gergely@risko.hu> {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeSynonymInstances #-}@@ -54,6 +55,8 @@ -- > -- > main = do s <- $(initHFlags "Simple program v0.1") -- > sequence_ $ replicate flags_repeat greet+-- > putStrLn $ "Your additional arguments were: " ++ show s+-- > putStrLn $ "Which is the same as: " ++ show HFlags.arguments -- > where -- > greet = putStrLn $ "Hello " ++ flags_name ++ ", very nice to meet you!" --@@ -68,7 +71,10 @@ defineEQFlag, FlagType(..), -- * Initialization of flags at runtime- initHFlags) where+ initHFlags,+ -- * For easy access to arguments, after initHFlags has been called+ arguments+ ) where -- TODOs: -- ?--no* for bools?@@ -140,8 +146,8 @@ | name' !! 1 == ':' -> return (drop 2 name', Just $ head name') | otherwise -> return (name', Nothing) defE <- defQ- case defE of- SigE _ _ -> return ()+ flagType <- case defE of+ SigE _ flagType -> return flagType _ -> fail "Default value for defineCustomFlag has to be an explicitly typed expression, like (12 :: Int)" moduleName <- fmap loc_module location let accessorName = mkName $ "flags_" ++ name@@ -162,10 +168,11 @@ (evaluate $(varE accessorName) >> return ()) |]) []]] flagPragmaDec <- return $ PragmaD $ InlineP accessorName NoInline FunLike AllPhases+ flagSig <- return $ SigD accessorName flagType flagDec <- funD accessorName [clause [] (normalB [| case True of True -> $(appE readQ [| lookupFlag name moduleName |]) False -> $(defQ) |]) []]- return [dataDec, instanceDec, flagPragmaDec, flagDec]+ return [dataDec, instanceDec, flagPragmaDec, flagSig, flagDec] -- | This just forwards to 'defineCustomFlag' with @[| read |]@ and -- @[| show |]@. Useful for flags where the type is not an instance@@ -234,11 +241,26 @@ 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.+-- @flags_*@. This is a map between flag name and current value. {-# NOINLINE globalHFlags #-} globalHFlags :: IORef (Maybe (Map String String)) globalHFlags = unsafePerformIO $ newIORef Nothing +-- | A global "IORef" for the easy access to the arguments.+{-# NOINLINE globalArguments #-}+globalArguments :: IORef (Maybe [String])+globalArguments = unsafePerformIO $ newIORef Nothing++-- | Contains the non-parsed, non-option parts of the command line,+-- the arguments. Can only be used after @initHFlags@ has been called.+{-# NOINLINE arguments #-}+arguments :: [String]+arguments = unsafePerformIO $ do+ margs <- readIORef globalArguments+ case margs of+ Just args -> return $ args+ Nothing -> error $ "HFlags.arguments used before calling initHFlags."+ lookupFlag :: String -> String -> String lookupFlag fName fModuleName = unsafePerformIO $ do flags <- readIORef globalHFlags@@ -259,6 +281,7 @@ env <- getEnvironment let envDefaults = map (mapFst (fromJust . stripPrefix "HFLAGS_")) $ filter ((isPrefixOf "HFLAGS_") . fst) env writeIORef globalHFlags $ Just $ Map.fromList $ defaults ++ envDefaults ++ opts+ writeIORef globalArguments $ Just nonopts mapM_ forceFlag flags return nonopts where
examples/SimpleExample.hs view
@@ -1,5 +1,5 @@ #!/usr/bin/env runhaskell- + {-# LANGUAGE TemplateHaskell #-} import HFlags@@ -9,5 +9,7 @@ main = do s <- $(initHFlags "Simple program v0.1") sequence_ $ replicate flags_repeat greet+ putStrLn $ "Your additional arguments were: " ++ show s+ putStrLn $ "Which is the same as: " ++ show HFlags.arguments where greet = putStrLn $ "Hello " ++ flags_name ++ ", very nice to meet you!"
hflags.cabal view
@@ -1,15 +1,15 @@ name: hflags-version: 0.1.1+version: 0.1.2 license: OtherLicense license-file: COPYING-author: Mihaly Barasz <klao@google.com>, Gergely Risko <errge@google.com>-maintainer: Gergely Risko <errge@google.com>+author: Mihaly Barasz <klao@google.com>, Gergely Risko <gergely@risko.hu>+maintainer: Gergely Risko <gergely@risko.hu> build-type: Simple cabal-version: >= 1.6 category: Console stability: provisional homepage: http://github.com/errge/hflags-bug-reports: mailto:errge@google.com+bug-reports: http://github.com/errge/hflags/issues synopsis: Command line flag parser, very similar to Google's gflags description:@@ -44,6 +44,8 @@ . main = do s <- $(initHFlags \"Simple program v0.1\")   sequence_ $ replicate flags_repeat greet+   putStrLn $ \"Your additional arguments were: \" ++ show s+   putStrLn $ \"Which is the same as: \" ++ show HFlags.arguments   where   greet = putStrLn $ \"Hello \" ++ flags_name ++ \", very nice to meet you!\" @