-- |
-- Module      : CmdLineParser
-- Description : Parser for command line options.
-- Copyright   : (c) Maximilian Nitsch, 2015
--
-- License     : MIT
-- Maintainer  : maximilian.nitsch@gausus.de
-- Stability   : experimental
-- Portability : portable
--
-- This module enables to parse command line option and put them in a
-- suitable data structure.

module Spellchecker.CmdLineParser
       ( -- * Configuration type
         Configuration (..)
         -- * Run parser
       , parseConfig
       ) where

import Options.Applicative hiding (empty, value)
import Data.Maybe (fromMaybe)

-- | Repository for all command line options
data Configuration = Configuration
                     { inFile  :: FilePath
                     , outFile :: FilePath
                     , corpus  :: FilePath
                     , limit   :: Int
                     , quiet   :: Bool
                     } deriving (Show)


-- | Parse command line options, use default on unset options
cmdLine :: Parser Configuration
cmdLine = Configuration
          <$> strOption (short 'i'
                         <> long "input"
                         <> metavar "FILE"
                         <> help "Specifies the input file.")
          <*> (fromMaybe "out.txt" -- Default output file
               <$> optional (strOption $  short 'o'
                                       <> long "output"
                                       <> metavar "FILE"
                                       <> help "Specifies the output file."))
          <*> (fromMaybe "data/corpus_de.dict" -- Default dictionary file
               <$> optional (strOption $ short 'c'
                                       <> long "corpus"
                                       <> metavar "FILE"
                                       <> help "Specifies the word corpus."))
          <*> (fromMaybe 5 -- Default limit
               <$> optional (option auto $ short 'l'
                                         <> long "limit"
                                         <> metavar "NUMBER"
                                         <> help "Edit distance limit."))
          <*> switch (short 'q'
                      <> long "quite"
                      <> help "Don't ask, take always the best match.")


-- | Execute the command line parser and return the configuration of the
-- spellchecker.
parseConfig :: IO Configuration
parseConfig = execParser $ info (helper <*> cmdLine) fullDesc
