module Data.Text.WordCount.Exec where import Data.Monoidimport qualified Data.Text.IO as TLIO -- .Lazy.IO as TLIOimport Paths_wordchoiceimport Options.Applicativeimport Data.Maybeimport Data.Versionimport Data.Text.WordCountimport Data.Text.WordCount.FileRead-- | Program datatype to be parseddata Program = Program { file :: FilePath , num :: Maybe Int , output :: Maybe FilePath } -- TODO add option for separators-- | Command line argument parserprogram :: Parser Programprogram = Program <$> (argument str (metavar "FILEPATH" <> help "File to analyze")) <*> (optional (read <$> strOption (short 'n' <> long "number" <> metavar "NUM" <> help "Top NUM words will be listed"))) <*> (optional (strOption (short 'o' <> long "output" <> metavar "OUTPUT" <> help "Filepath for output graph")))-- | Parse for version infoversionInfo :: Parser (a -> a)versionInfo = infoOption ("wordchoice version: " ++ showVersion version) (short 'v' <> long "version" <> help "Show version")-- | Wraps parser with help parserwrapper :: ParserInfo Programwrapper = info (helper <*> versionInfo <*> program) (fullDesc <> progDesc "Word choice is a command-line meant to help you improve your writing. Simply point it to a file containing text and it will list your most frequently used words and their frequencies." <> header "Word choice command-line utility")-- | Actual executableexec :: IO ()exec = execParser wrapper >>= pick-- | Run parsed recordpick :: Program -> IO ()pick rec = let n = fromMaybe 25 (num rec) in do contents <- processFile (file rec) TLIO.putStrLn . displayWords . topN n $ contents case output rec of (Just out) -> flip makeFile out . topN n $ contents _ -> pure ()