[hs] Make cli fancier

This commit is contained in:
Joscha 2020-12-06 11:06:51 +00:00
parent 75fa851ff5
commit 55bc09948b
9 changed files with 56 additions and 36 deletions

View file

@ -4,18 +4,32 @@ import Control.Monad
import Options.Applicative
import Aoc.Day (Day (..))
import qualified Aoc.Y2019 as Y2019
import Aoc.Day (Day (..), Year (..))
import qualified Aoc.Y2020 as Y2020
toCommand :: Day -> Mod CommandFields (IO ())
toCommand (DayPure name f) = command name $ info (helper <*> pure f) mempty
toCommand (DayFile name f) = command name $ info (helper <*> p) mempty
where
p = f <$> strArgument (metavar "INPUTFILE")
lpad :: a -> Int -> [a] -> [a]
lpad a n as = replicate (n - length as) a ++ as
yearsToParser :: [Year] -> Parser (IO ())
yearsToParser = hsubparser . mconcat . map yearToCommand
yearToCommand :: Year -> Mod CommandFields (IO ())
yearToCommand y = command (show $ yNum y) $ flip info mempty $ daysToParser $ yDays y
daysToParser :: [(Int, Day)] -> Parser (IO ())
daysToParser = hsubparser . mconcat . map (uncurry dayToCommand)
dayToCommand :: Int -> Day -> Mod CommandFields (IO ())
dayToCommand dNum = command (lpad '0' 2 $ show dNum) . flip info mempty . dayToParser
dayToParser :: Day -> Parser (IO ())
dayToParser (DayPure f) = pure f
dayToParser (DayFile f) = f <$> strArgument (metavar "INPUTFILE")
parser :: Parser (IO ())
parser = subparser $ mconcat $ map toCommand $ Y2019.days ++ Y2020.days
parser = yearsToParser
[ Y2020.year
]
opts :: ParserInfo (IO ())
opts = info (helper <*> parser) $ fullDesc <> failureCode 1