Refactor main
This commit is contained in:
parent
8a40514984
commit
99cc0a1093
@ -1,5 +1,5 @@
|
|||||||
name: alea
|
name: alea
|
||||||
version: 0.1.0.0
|
version: 0.1.1.0
|
||||||
synopsis: a diceware passphrase generator
|
synopsis: a diceware passphrase generator
|
||||||
description:
|
description:
|
||||||
|
|
||||||
|
39
main.hs
39
main.hs
@ -2,13 +2,11 @@
|
|||||||
|
|
||||||
import System.IO
|
import System.IO
|
||||||
import System.Console.CmdArgs
|
import System.Console.CmdArgs
|
||||||
import System.Environment (getArgs, withArgs)
|
|
||||||
import Control.Monad (when)
|
import Control.Monad (when)
|
||||||
|
|
||||||
import Paths_alea (getDataFileName)
|
import Paths_alea (getDataFileName)
|
||||||
|
import Alea.Diceware
|
||||||
import Diceware
|
import Alea.Random
|
||||||
import Random
|
|
||||||
|
|
||||||
_NAME = "Alea"
|
_NAME = "Alea"
|
||||||
_VERSION = "0.1.0"
|
_VERSION = "0.1.0"
|
||||||
@ -41,25 +39,26 @@ getProgArgs = cmdArgs $ progArgs
|
|||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = getProgArgs >>= defaults >>= exec
|
||||||
args <- getArgs
|
|
||||||
opts <- getProgArgs
|
-- Assign defaults value to unspecified args
|
||||||
exec opts
|
defaults :: Args -> IO Args
|
||||||
|
defaults args@Args{..} = do
|
||||||
|
dictionary <- getDataFileName "dict/diceware" >>= readFile
|
||||||
|
return args
|
||||||
|
{ dictionary = dictionary
|
||||||
|
, phraseLength = if phraseLength == 0 then 6 else phraseLength
|
||||||
|
}
|
||||||
|
|
||||||
exec :: Args -> IO ()
|
exec :: Args -> IO ()
|
||||||
exec opts@Args{..} = do
|
exec args@Args{..} =
|
||||||
opts <- getProgArgs
|
|
||||||
defaultDict <- getDataFileName "diceware"
|
|
||||||
file <- readFile (if null dictionary then defaultDict else dictionary)
|
|
||||||
if interactive
|
if interactive
|
||||||
then interact (unlines . map (dice file) . lines)
|
then interact (unlines . map dice . lines)
|
||||||
else do
|
else do
|
||||||
phrase <- randWords (size $ parseDiceware file) phraseLength'
|
randWords dictSize phraseLength >>= putStrLn . unwords . map dice'
|
||||||
putStrLn . unwords . map (dice' file) $ phrase
|
when (phrases > 1) $ exec args {phrases = phrases - 1}
|
||||||
when (phrases > 1) $ exec opts {phrases = phrases - 1}
|
|
||||||
where
|
where
|
||||||
-- helpers
|
-- helpers
|
||||||
dice x n = readDiceware (parseDiceware x) (read n :: Int)
|
dice n = readDiceware (parseDiceware dictionary) (read n :: Int)
|
||||||
dice' x n = readDiceware' (parseDiceware x) n
|
dice' n = readDiceware' (parseDiceware dictionary) n
|
||||||
-- default arguments
|
dictSize = size $ parseDiceware dictionary
|
||||||
phraseLength' = if phraseLength == 0 then 6 else phraseLength
|
|
||||||
|
Loading…
Reference in New Issue
Block a user