human-friendly-id-gen/src-exe/Main.hs

46 lines
1.7 KiB
Haskell
Raw Permalink Normal View History

module Main where
2018-09-02 11:02:08 +00:00
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
2018-09-02 11:02:08 +00:00
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
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"
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)")
<*> 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