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