Removing CmdArgs

This commit is contained in:
Rnhmjoj 2014-12-12 22:13:55 +01:00
parent aa953f9e4a
commit 04d4119b7a

53
main.hs
View File

@ -1,58 +1,39 @@
{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
{-# LANGUAGE RecordWildCards #-}
import System.IO
import System.Console.CmdArgs
import Control.Monad (when)
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)
} deriving (Show)
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"
}
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
-- 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'}
-- Main function
exec :: Args -> IO ()
exec args@Args{..} =
exec :: ProgArgs -> IO ()
exec args@ProgArgs{..} =
if interactive
then interact (unlines . map dice . lines)
else do
@ -61,4 +42,4 @@ exec args@Args{..} =
where
(dict, dictSize) = (parseDiceware dictionary, length dict)
dice n = readDiceware dict (read n :: Int)
dice' n = readDiceware' dict n
dice' n = readDiceware' dict n