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:
- 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

View file

@ -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

View file

@ -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