{-# LANGUAGE TupleSections #-} module Hcompta.CLI.Args where import System.Console.GetOpt ( getOpt , ArgOrder(..) , OptDescr(..) ) import Hcompta.CLI.Context (Context) import qualified Hcompta.CLI.Write as Write type Options context = [OptDescr (Context -> context -> IO context)] parse :: Context -> (IO String) -> Options context -> (context, [String]) -> IO (context, [String]) parse context usage options (ctx, args) = case getOpt RequireOrder options args of (parsers, cmds, []) -> do foldl (\acc parser -> acc >>= parser context) (return ctx) parsers >>= return . (, cmds) (_, _, errs) -> do usage >>= Write.fatal context . (concat errs ++)