From e1462aecda91b990fe9d2e4531f6986234b84fb4 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 20 Aug 2017 20:04:24 +0200 Subject: CliOptions: Parse the `language` option into a `Locale` Instead of parsing `--language` as a string, parse it as a real `Locale` type. This means the parsing is taken care of for us at the CLI option handling stage instead of later on. We can thus use the language value more quickly and easily, passing it to the `translate` function, which takes a `Locale`. --- src/CliOptions.hs | 15 ++++++++++++--- src/I18n.hs | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/CliOptions.hs b/src/CliOptions.hs index 60f05ad..9d8baf6 100644 --- a/src/CliOptions.hs +++ b/src/CliOptions.hs @@ -7,11 +7,20 @@ module CliOptions import Data.Semigroup ((<>)) import Options.Applicative +import I18n (Locale(EN, FR)) + data Options = Options { slackApiToken :: String - , language :: String + , language :: Locale } +-- | Parse the language command line option string into a `Locale` type +parseLanguage :: ReadM Locale +parseLanguage = eitherReader $ \s -> case s of + "en" -> Right EN + "fr" -> Right FR + _ -> Left "Unrecognised language code" + options :: Parser Options options = Options <$> strOption @@ -19,11 +28,11 @@ options = Options <> metavar "TOKEN" <> value "" <> help "Token to access Slack's real-time messaging API" ) - <*> strOption + <*> option parseLanguage ( long "language" <> short 'l' <> metavar "en" - <> value "en" + <> value EN <> help "Set the language Sorbot will speak in (en | fr)" ) parseOptions :: IO Options diff --git a/src/I18n.hs b/src/I18n.hs index 5a74d80..e50ba5b 100644 --- a/src/I18n.hs +++ b/src/I18n.hs @@ -9,7 +9,7 @@ module I18n import qualified Data.Text as T -data Locale = EN | FR +data Locale = EN | FR deriving (Show) data Message = GitHubCommitRepoURLNotFound -- cgit v1.2.3