Removing CmdArgs
This commit is contained in:
parent
aa953f9e4a
commit
04d4119b7a
51
main.hs
51
main.hs
@ -1,58 +1,39 @@
|
|||||||
{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
|
||||||
import System.IO
|
import System.IO
|
||||||
import System.Console.CmdArgs
|
|
||||||
import Control.Monad (when)
|
import Control.Monad (when)
|
||||||
|
|
||||||
import Paths_alea (getDataFileName)
|
import Paths_alea (getDataFileName)
|
||||||
import Alea.Diceware
|
import Alea.Diceware
|
||||||
import Alea.Random
|
import Alea.Random
|
||||||
|
|
||||||
_NAME = "Alea"
|
data ProgArgs = ProgArgs
|
||||||
_VERSION = "0.3.0"
|
|
||||||
_INFO = _NAME ++ " version " ++ _VERSION
|
|
||||||
_ABOUT = "a diceware passphrase generator"
|
|
||||||
_COPYRIGHT = "(C) Michele Guerini Rocco 2014"
|
|
||||||
|
|
||||||
data Args = Args
|
|
||||||
{ interactive :: Bool
|
{ interactive :: Bool
|
||||||
, dictionary :: FilePath
|
, dictionary :: FilePath
|
||||||
, phraseLength :: Int
|
, phraseLength :: Int
|
||||||
, phrases :: Int
|
, phrases :: Int
|
||||||
} deriving (Data, Typeable, Show, Eq)
|
} deriving (Show)
|
||||||
|
|
||||||
progArgs :: Args
|
parser :: IO (ParserSpec ProgArgs)
|
||||||
progArgs = Args
|
parser = path >>= \path -> return $ ProgArgs
|
||||||
{ interactive = def &= help "Manually insert numbers"
|
`parsedBy` boolFlag "interactive" `Descr` "Manually insert numbers"
|
||||||
, dictionary = def &= help "Specify dictionary file path"
|
`andBy` optFlag path "dictionary" `Descr` "Specify dictionary file path"
|
||||||
, phraseLength = def &= help "Number of words in a passphrase"
|
`andBy` optFlag 6 "lenght" `Descr` "Number of words in a passphrase"
|
||||||
, phrases = def &= help "Number of passphrases to generate"
|
`andBy` optFlag 1 "phrases" `Descr` "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
|
|
||||||
|
|
||||||
|
interface :: IO (CmdLnInterface ProgArgs)
|
||||||
|
interface =
|
||||||
|
(`setAppDescr` "A diceware passphrase generator") <$>
|
||||||
|
(`setAppEpilog` "Alea iacta est.") <$>
|
||||||
|
(mkApp =<< parser)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = getProgArgs >>= defaults >>= exec
|
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
|
-- Main function
|
||||||
exec :: Args -> IO ()
|
exec :: ProgArgs -> IO ()
|
||||||
exec args@Args{..} =
|
exec args@ProgArgs{..} =
|
||||||
if interactive
|
if interactive
|
||||||
then interact (unlines . map dice . lines)
|
then interact (unlines . map dice . lines)
|
||||||
else do
|
else do
|
||||||
|
Loading…
Reference in New Issue
Block a user