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