]> Git — Sourcephile - comptalang.git/blob - cli/Hcompta/CLI/Args.hs
Polissage : n'utilise pas TypeSynonymInstances.
[comptalang.git] / cli / Hcompta / CLI / Args.hs
1 {-# LANGUAGE TupleSections #-}
2 module Hcompta.CLI.Args where
3
4 import Control.Monad (liftM)
5 import qualified Data.List
6 import qualified Data.Text.Lazy as TL
7 import System.Console.GetOpt
8 ( getOpt
9 , ArgOrder(..)
10 , OptDescr(..)
11 )
12
13 import Hcompta.CLI.Context (Context)
14 import qualified Hcompta.CLI.Write as Write
15 import Hcompta.Lib.Leijen ((<>))
16 import qualified Hcompta.Lib.Leijen as W
17
18 type Options context
19 = [OptDescr (Context -> context -> IO context)]
20
21 parse
22 :: Context
23 -> (IO String)
24 -> Options context
25 -> (context, [String])
26 -> IO (context, [String])
27 parse context usage options (ctx, args) =
28 case getOpt RequireOrder options args of
29 (parsers, cmds, []) -> do
30 liftM (, cmds) $
31 Data.List.foldl' (\acc parser -> acc >>= parser context) (return ctx) parsers
32 (_, _, errs) -> do
33 usage >>= Write.fatal context .
34 (W.vsep (map (W.text .TL.pack) errs) <>) .
35 W.text . TL.pack