aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2017-08-20 20:04:24 +0200
committerTeddy Wing2017-08-20 20:04:24 +0200
commite1462aecda91b990fe9d2e4531f6986234b84fb4 (patch)
tree7707102130b13ad482b815f91994ff023443c5ac /src
parent5792fb1fc1439994a4877761820d8d034b66110d (diff)
downloadsorbot-e1462aecda91b990fe9d2e4531f6986234b84fb4.tar.bz2
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`.
Diffstat (limited to 'src')
-rw-r--r--src/CliOptions.hs15
-rw-r--r--src/I18n.hs2
2 files changed, 13 insertions, 4 deletions
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