From d5208cfa3485684fcee4c210db78b935d4cddbd5 Mon Sep 17 00:00:00 2001 From: Samae Date: Tue, 5 Dec 2023 22:00:32 +0200 Subject: [PATCH] Some cleanup and optparse --- .gitignore | 1 + Main.hs | 41 ++++++++++++++++++++++++++++++----------- package.yaml | 3 ++- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index d43d807..30801e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.cabal +result* diff --git a/Main.hs b/Main.hs index 038c20a..f63fd1e 100644 --- a/Main.hs +++ b/Main.hs @@ -3,20 +3,39 @@ module Main where import Data.Attoparsec.Text (parseOnly) +import Data.Either (fromRight) +import Options.Applicative import qualified Data.Text.IO as T import INAST import INSolver +data FCInsurersOpt = FCInsurersOpt + { path :: FilePath + , intInput :: Int + } + +fCInsurersOpt :: Parser FCInsurersOpt +fCInsurersOpt = FCInsurersOpt + <$> strOption + ( long "network" + <> metavar "PATH" + <> help "the path to a file that defines a network" ) + <*> option auto + ( long "input" + <> metavar "INT" + <> help "an integer input to the network" ) + main :: IO () -main = do - inputNetwork <- T.readFile "./input.network" - T.putStrLn "Using the following input network:" - T.putStrLn inputNetwork - T.putStrLn "Parsed AST:" - let Right insnet = parseOnly insuranceNetworkParser inputNetwork - print insnet - T.putStrLn "Problem solution, input=3:" - print $ solve (3::Int) insnet - T.putStrLn "Problem solution, input=6:" - print $ solve (6::Int) insnet +main = fcinsurers =<< execParser opts + where + opts = info (fCInsurersOpt <**> helper) + ( fullDesc + <> progDesc "A small command line program that outputs a list of the \ + \ fewest contributing insurers (by their integer ids)" + <> header "Fewest Contributing Insurers (in23)" ) + +fcinsurers :: FCInsurersOpt -> IO () +fcinsurers (FCInsurersOpt p i) = do + inputNetwork <- T.readFile p + print $ fromRight [] $ solve i <$> parseOnly insuranceNetworkParser inputNetwork diff --git a/package.yaml b/package.yaml index 17d3317..0e6ebd7 100644 --- a/package.yaml +++ b/package.yaml @@ -5,8 +5,9 @@ ghc-options: -Wall -threaded dependencies: - base == 4.* - attoparsec - - text - mtl + - optparse-applicative + - text executables: in23: