human-friendly-id-gen/src-exe/Main.hs
Yann Esposito (Yogsototh) 1372c2470e
fixed the doctests
2018-09-13 12:13:11 +02:00

45 lines
1.7 KiB
Haskell

module Main where
import Protolude hiding (FilePath, die)
import Turtle
import qualified Data.Text as Text
import qualified HFIG.Dictionary as Dict
import qualified HFIG.Lovecraftian as Lovecraftian
import qualified HFIG.Short as Short
main :: IO ()
main = do
opts <- options "Generate Human Friendly identifiers" optParser
case genMode opts of
Short -> Short.idgen (fromMaybe 4 (optLen opts)) >>= putText
Lovecraftian -> Lovecraftian.idgen (fromMaybe 1 (optLen opts)) >>= putText
Dict -> do
file <- case optDict opts of
Just filepath -> return $ toS (format fp filepath)
Nothing -> die "Please select a dictionary file with the -d or --dict options"
dict <- Dict.dictionaryFromFile file
Dict.idgen dict (fromMaybe 3 (optLen opts)) >>= putText
-- Option parsing
data GenMode = Short | Lovecraftian | Dict deriving (Eq)
data AppOptions = AppOptions { genMode :: GenMode
, optDict :: Maybe FilePath
, optLen :: Maybe Int
}
optParser :: Parser AppOptions
optParser = AppOptions
<$> (fromMaybe Short <$> optional (opt toGenMode "gen" 'g' "Possible values: short, lovecraftian, dict"))
<*> optional (optPath "dict" 'd' "dictionary file path (the file should contain one word per line)")
<*> optional (optInt "len" 'n' "complexity depends on the gen chosen")
toGenMode :: Text -> Maybe GenMode
toGenMode "" = Nothing
toGenMode str
| str `Text.isPrefixOf` "short" = Just Short
| str `Text.isPrefixOf` "dictionary" = Just Dict
| str `Text.isPrefixOf` "lovecraftian" = Just Lovecraftian
| otherwise = Nothing