From 79fcd2e76645df371e79c8fcf55f33d3aba9b720 Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Sat, 4 Jan 2025 23:16:18 +0100 Subject: [PATCH] feat: Parse command-line arguments --- package.yaml | 7 +++---- src/TK2MQTT.hs | 16 ++++++++++++++++ tk2mqtt.cabal | 19 ++++--------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/package.yaml b/package.yaml index 73e33c4..a4f5e35 100644 --- a/package.yaml +++ b/package.yaml @@ -11,9 +11,6 @@ extra-source-files: dependencies: - base >= 4.13 && < 5 - - time - - directory - - containers - net-mqtt - network-uri - req @@ -21,7 +18,7 @@ dependencies: - aeson - text - bytestring - - process + - optparse-applicative ghc-options: - -Wall @@ -66,3 +63,5 @@ tests: source-dirs: - test/doctest build-tools: doctest + dependencies: + - process diff --git a/src/TK2MQTT.hs b/src/TK2MQTT.hs index f8603ad..718bea2 100644 --- a/src/TK2MQTT.hs +++ b/src/TK2MQTT.hs @@ -10,9 +10,11 @@ import Control.Exception (catch) import Control.Monad (void) import Control.Monad.IO.Class import Data.Aeson +import Data.Text (Text) import Network.HTTP.Req import Network.MQTT.Client qualified as MQTT import Network.URI (URI, parseURI) +import Options.Applicative qualified as O import System.Environment qualified as Env import System.IO @@ -20,6 +22,20 @@ newtype APIKey = APIKey String newtype StationID = StationID String +data Config = Config + { apiKey :: Text, + stationId :: Text, + brokerUrl :: Text + } + deriving (Show) + +pConfig :: O.Parser Config +pConfig = + Config + <$> O.strOption (O.long "api-key" <> O.metavar "APIKEY" <> O.help "Tankerkönig API key") + <*> O.strOption (O.long "station-id" <> O.metavar "STATION" <> O.help "ID of the station to query the prices for") + <*> O.strOption (O.long "broker" <> O.metavar "URL" <> O.help "URL of the MQTT broker" <> O.showDefault <> O.value "mqtt://127.0.0.1:1883") + runTK2MQTT :: URI -> APIKey -> StationID -> IO () runTK2MQTT uri (APIKey apikey) (StationID stationId) = do mc <- MQTT.connectURI MQTT.mqttConfig uri diff --git a/tk2mqtt.cabal b/tk2mqtt.cabal index 0ad3370..d89273c 100644 --- a/tk2mqtt.cabal +++ b/tk2mqtt.cabal @@ -32,15 +32,12 @@ library aeson , base >=4.13 && <5 , bytestring - , containers - , directory , effectful , net-mqtt , network-uri - , process + , optparse-applicative , req , text - , time default-language: GHC2021 executable tk2mqtt @@ -58,15 +55,12 @@ executable tk2mqtt aeson , base >=4.13 && <5 , bytestring - , containers - , directory , effectful , net-mqtt , network-uri - , process + , optparse-applicative , req , text - , time , tk2mqtt default-language: GHC2021 @@ -88,15 +82,13 @@ test-suite doctest aeson , base >=4.13 && <5 , bytestring - , containers - , directory , effectful , net-mqtt , network-uri + , optparse-applicative , process , req , text - , time default-language: Haskell2010 test-suite spec @@ -120,16 +112,13 @@ test-suite spec , aeson , base >=4.13 && <5 , bytestring - , containers - , directory , effectful , hspec , net-mqtt , network-uri - , process + , optparse-applicative , quickcheck-instances , req , text - , time , tk2mqtt default-language: GHC2021