packages feed

alea 0.3.0.0 → 0.3.1.0

raw patch · 2 files changed

+37/−46 lines, 2 filesdep +argparserdep −cmdargs

Dependencies added: argparser

Dependencies removed: cmdargs

Files

Main.hs view
@@ -1,64 +1,55 @@-{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}+{-# LANGUAGE RecordWildCards #-}  import System.IO-import System.Console.CmdArgs-import Control.Monad (when)+import System.Console.ArgParser+import Control.Monad+import Control.Applicative  import Paths_alea (getDataFileName) import Alea.Diceware import Alea.Random -_NAME = "Alea"-_VERSION = "0.3.0"-_INFO = _NAME ++ " version " ++ _VERSION-_ABOUT = "a diceware passphrase generator"-_COPYRIGHT = "(C) Michele Guerini Rocco 2014"--data Args = Args+data ProgArgs = ProgArgs 	{ interactive  :: Bool 	, dictionary   :: FilePath 	, phraseLength :: Int 	, phrases      :: Int-	} deriving (Data, Typeable, Show, Eq)--progArgs :: Args-progArgs = Args-	{ interactive  = def &= help "Manually insert numbers"-	, dictionary   = def &= help "Specify dictionary file path"-	, phraseLength = def &= help "Number of words in a passphrase"-	, phrases      = def &= help "Number of passphrases to generate"-	}+	} deriving (Show) -getProgArgs :: IO Args-getProgArgs = cmdArgs $ progArgs-    &= versionArg [explicit, name "version", name "v", summary _INFO]-    &= summary (_INFO ++ ", " ++ _COPYRIGHT)-    &= help _ABOUT-    &= helpArg [explicit, name "help", name "h"]-    &= program _NAME+parser :: IO (ParserSpec ProgArgs)+parser = path >>= \path -> return $ ProgArgs+  `parsedBy` boolFlag      "interactive" `Descr` "Manually insert numbers"+  `andBy`    optFlag  path "dictionary"  `Descr` "Specify dictionary file path"+  `andBy`    optFlag  6    "lenght"      `Descr` "Number of words in a passphrase"+  `andBy`    optFlag  1    "phrases"     `Descr` "Number of passphrases to generate" +interface :: IO (CmdLnInterface ProgArgs)+interface = +  (`setAppDescr` "A diceware passphrase generator") <$>+  (`setAppEpilog` "Alea iacta est.") <$>+  (mkApp =<< parser)  main :: IO ()-main = getProgArgs >>= defaults >>= exec+main = interface >>= flip runApp (readDict >=> exec) --- Assign default values to unspecified args-defaults :: Args -> IO Args-defaults args@Args{..} = do-	dictionary' <- if null dictionary-					then getDataFileName "dict/diceware" >>= readFile-					else readFile dictionary-	let phraseLength' = if phraseLength == 0 then 6 else phraseLength-	return args { dictionary = dictionary', phraseLength = phraseLength'}+-- Default path of the dictionary+path :: IO FilePath+path = getDataFileName "dict/diceware" +-- Read dictionary file+readDict :: ProgArgs -> IO ProgArgs+readDict args@ProgArgs{..} =+  readFile dictionary >>= \x -> return args {dictionary = x}+ -- Main function-exec :: Args -> IO ()-exec args@Args{..} =+exec :: ProgArgs -> IO ()+exec args@ProgArgs{..} = 	if interactive-		then interact (unlines . map dice . lines)-		else do-			randWords dictSize phraseLength >>= putStrLn . unwords . map dice'-			when (phrases > 1) $ exec args {phrases = phrases - 1}+	  then interact (unlines . map dice . lines)+	  else do+			  randWords dictSize phraseLength >>= putStrLn . unwords . map dice'+			  when (phrases > 1) $ exec args {phrases = phrases - 1} 	where-		(dict, dictSize) = (parseDiceware dictionary, length dict)-		dice  n = readDiceware  dict (read n :: Int)-		dice' n = readDiceware' dict n+	  (dict, dictSize) = (parseDiceware dictionary, length dict)+	  dice  n = readDiceware  dict (read n :: Int)+	  dice' n = readDiceware' dict n
alea.cabal view
@@ -1,5 +1,5 @@ name:                alea-version:             0.3.0.0+version:             0.3.1.0 synopsis:            a diceware passphrase generator description: @@ -25,5 +25,5 @@   other-modules:       Paths_alea   other-extensions:    DeriveDataTypeable, RecordWildCards   build-depends:       base ==4.7.* , containers ==0.5.*,-                       cmdargs ==0.10.*, threefish == 0.2.*+                       argparser ==0.3.*, threefish == 0.2.*   ghc-options:         -O2