2018-09-04 08:53:34 +00:00
|
|
|
module Main where
|
2018-09-02 11:02:08 +00:00
|
|
|
|
2018-09-04 08:53:34 +00:00
|
|
|
import Protolude hiding (FilePath, die)
|
|
|
|
|
|
|
|
import Turtle
|
2018-09-04 12:35:30 +00:00
|
|
|
import qualified Data.Text as Text
|
|
|
|
|
2018-09-04 08:53:34 +00:00
|
|
|
|
|
|
|
import qualified HFIG.Dictionary as Dict
|
|
|
|
import qualified HFIG.Lovecraftian as Lovecraftian
|
|
|
|
import qualified HFIG.Short as Short
|
2018-09-02 11:02:08 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
2018-09-04 08:53:34 +00:00
|
|
|
main = do
|
|
|
|
opts <- options "Generate Human Friendly identifiers" optParser
|
|
|
|
case genMode opts of
|
2018-09-04 12:21:25 +00:00
|
|
|
Short -> Short.idgen (fromMaybe 4 (optLen opts)) >>= putText
|
2018-09-04 12:35:30 +00:00
|
|
|
Lovecraftian -> Lovecraftian.idgen (fromMaybe 1 (optLen opts)) >>= putText
|
2018-09-04 08:53:34 +00:00
|
|
|
Dict -> do
|
2018-09-04 13:20:20 +00:00
|
|
|
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"
|
2018-09-04 08:53:34 +00:00
|
|
|
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
|
2018-09-13 10:13:11 +00:00
|
|
|
<$> (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)")
|
2018-09-04 08:53:34 +00:00
|
|
|
<*> optional (optInt "len" 'n' "complexity depends on the gen chosen")
|
|
|
|
|
|
|
|
toGenMode :: Text -> Maybe GenMode
|
2018-09-04 12:35:30 +00:00
|
|
|
toGenMode "" = Nothing
|
|
|
|
toGenMode str
|
|
|
|
| str `Text.isPrefixOf` "short" = Just Short
|
|
|
|
| str `Text.isPrefixOf` "dictionary" = Just Dict
|
|
|
|
| str `Text.isPrefixOf` "lovecraftian" = Just Lovecraftian
|
|
|
|
| otherwise = Nothing
|