Choose word length with command line argument

This commit is contained in:
Joscha 2018-09-20 23:38:05 +00:00
parent 4e1eeb2545
commit 31d83323c8

View file

@ -140,11 +140,11 @@ loop = do
runLoop :: GuessState -> IO GuessState runLoop :: GuessState -> IO GuessState
runLoop = execStateT (H.runInputT H.defaultSettings loop) runLoop = execStateT (H.runInputT H.defaultSettings loop)
loadAndRun :: [FilePath] -> IO () loadAndRun :: Int -> [FilePath] -> IO ()
loadAndRun files = do loadAndRun n files = do
wordlists <- mapM readFile files wordlists <- mapM readFile files
let wordlist = concatMap lines wordlists let wordlist = concatMap lines wordlists
cmap = buildClassMap (Just 5) wordlist cmap = buildClassMap (Just n) wordlist
g = guessState cmap g = guessState cmap
let wordCount = numberjust $ show $ length wordlist let wordCount = numberjust $ show $ length wordlist
@ -179,13 +179,19 @@ loadAndRun file = do
-- - quit -> quit the program (alias: q) -- - quit -> quit the program (alias: q)
-} -}
wrongArgs :: IO ()
wrongArgs = do
name <- getProgName
putStrLn " USAGE:"
putStrLn $ name ++ " <word length> <dictionary files>"
die "Error: No word length or dictionary given."
main :: IO () main :: IO ()
main = do main = do
args <- getArgs args <- getArgs
case args of case args of
[] -> do [] -> wrongArgs
name <- getProgName [_] -> wrongArgs
putStrLn " USAGE:" (n:files) -> case readMaybe n of
putStrLn $ name ++ " <dictionary>" Nothing -> wrongArgs
die "Error: No dictionary given." Just number -> loadAndRun number files
files -> loadAndRun files