feat: Parse command-line arguments

This commit is contained in:
Alexander Kobjolke 2025-01-04 23:16:18 +01:00
parent 6603e65131
commit 79fcd2e766
3 changed files with 23 additions and 19 deletions

View file

@ -11,9 +11,6 @@ extra-source-files:
dependencies: dependencies:
- base >= 4.13 && < 5 - base >= 4.13 && < 5
- time
- directory
- containers
- net-mqtt - net-mqtt
- network-uri - network-uri
- req - req
@ -21,7 +18,7 @@ dependencies:
- aeson - aeson
- text - text
- bytestring - bytestring
- process - optparse-applicative
ghc-options: ghc-options:
- -Wall - -Wall
@ -66,3 +63,5 @@ tests:
source-dirs: source-dirs:
- test/doctest - test/doctest
build-tools: doctest build-tools: doctest
dependencies:
- process

View file

@ -10,9 +10,11 @@ import Control.Exception (catch)
import Control.Monad (void) import Control.Monad (void)
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Data.Aeson import Data.Aeson
import Data.Text (Text)
import Network.HTTP.Req import Network.HTTP.Req
import Network.MQTT.Client qualified as MQTT import Network.MQTT.Client qualified as MQTT
import Network.URI (URI, parseURI) import Network.URI (URI, parseURI)
import Options.Applicative qualified as O
import System.Environment qualified as Env import System.Environment qualified as Env
import System.IO import System.IO
@ -20,6 +22,20 @@ newtype APIKey = APIKey String
newtype StationID = StationID 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 -> StationID -> IO ()
runTK2MQTT uri (APIKey apikey) (StationID stationId) = do runTK2MQTT uri (APIKey apikey) (StationID stationId) = do
mc <- MQTT.connectURI MQTT.mqttConfig uri mc <- MQTT.connectURI MQTT.mqttConfig uri

View file

@ -32,15 +32,12 @@ library
aeson aeson
, base >=4.13 && <5 , base >=4.13 && <5
, bytestring , bytestring
, containers
, directory
, effectful , effectful
, net-mqtt , net-mqtt
, network-uri , network-uri
, process , optparse-applicative
, req , req
, text , text
, time
default-language: GHC2021 default-language: GHC2021
executable tk2mqtt executable tk2mqtt
@ -58,15 +55,12 @@ executable tk2mqtt
aeson aeson
, base >=4.13 && <5 , base >=4.13 && <5
, bytestring , bytestring
, containers
, directory
, effectful , effectful
, net-mqtt , net-mqtt
, network-uri , network-uri
, process , optparse-applicative
, req , req
, text , text
, time
, tk2mqtt , tk2mqtt
default-language: GHC2021 default-language: GHC2021
@ -88,15 +82,13 @@ test-suite doctest
aeson aeson
, base >=4.13 && <5 , base >=4.13 && <5
, bytestring , bytestring
, containers
, directory
, effectful , effectful
, net-mqtt , net-mqtt
, network-uri , network-uri
, optparse-applicative
, process , process
, req , req
, text , text
, time
default-language: Haskell2010 default-language: Haskell2010
test-suite spec test-suite spec
@ -120,16 +112,13 @@ test-suite spec
, aeson , aeson
, base >=4.13 && <5 , base >=4.13 && <5
, bytestring , bytestring
, containers
, directory
, effectful , effectful
, hspec , hspec
, net-mqtt , net-mqtt
, network-uri , network-uri
, process , optparse-applicative
, quickcheck-instances , quickcheck-instances
, req , req
, text , text
, time
, tk2mqtt , tk2mqtt
default-language: GHC2021 default-language: GHC2021