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