]> Git — Sourcephile - comptalang.git/blob - cli/Hcompta/CLI/Command.hs
Correction : Calc.Balance : utilise Typeable1 pour supporter GHC-7.6.
[comptalang.git] / cli / Hcompta / CLI / Command.hs
1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE TupleSections #-}
4 module Hcompta.CLI.Command where
5
6 import qualified Data.Text.Lazy as TL
7 import System.Console.GetOpt
8 ( ArgDescr(..)
9 , OptDescr(..)
10 , usageInfo )
11 import System.Environment (getProgName)
12 import System.Exit (exitWith, ExitCode(..))
13 import qualified System.IO as IO
14
15 import Hcompta.CLI.Context (Context)
16 import qualified Hcompta.CLI.Args as Args
17 import qualified Hcompta.CLI.Command.Balance as Command.Balance
18 import qualified Hcompta.CLI.Command.Print as Command.Print
19 import qualified Hcompta.CLI.Context as Context
20 import qualified Hcompta.CLI.Write as Write
21 import qualified Hcompta.Lib.Leijen as W
22 import Hcompta.Lib.Leijen ((<>))
23
24 usage :: IO String
25 usage = do
26 bin <- getProgName
27 return $ unlines $
28 [ "SYNTAX "
29 , " "++bin++" [option..] <command> [arguments]"
30 , ""
31 , usageInfo "OPTIONS" options
32 , "COMMANDS (use "++bin++" <command> --help for specific help)"
33 , " balance [-i FILE]"
34 , " print [-i FILE]"
35 ]
36
37 options :: Args.Options Context
38 options =
39 [ Option "h" ["help"]
40 (NoArg (\_opts _context -> do
41 usage >>= IO.hPutStr IO.stderr
42 exitWith ExitSuccess))
43 "show this help"
44 , Option "v" ["verbosity"]
45 (OptArg (\arg _context context ->
46 case arg of
47 Nothing ->
48 case Context.verbosity context of
49 v | v < maxBound ->
50 return $ context{Context.verbosity=succ v}
51 _ -> return $ context
52 Just "error" -> return $ context{Context.verbosity=Context.Verbosity_Error}
53 Just "warn" -> return $ context{Context.verbosity=Context.Verbosity_Warn}
54 Just "info" -> return $ context{Context.verbosity=Context.Verbosity_Info}
55 Just "debug" -> return $ context{Context.verbosity=Context.Verbosity_Debug}
56 Just _ -> Write.fatal context $
57 W.text "--verbosity option expects \"error\", \"warn\", \"info\", or \"debug\" as value")
58 "error|warn|info|debug"
59 )
60 "increment or set verbosity level, can be use multiple times"
61 , Option "" ["color"]
62 (OptArg (\arg _context context -> do
63 color <- case arg of
64 Nothing -> return $ Just True
65 Just "always" -> return $ Just True
66 Just "never" -> return $ Just False
67 Just "auto" -> return $ Nothing
68 Just _ -> Write.fatal context $
69 W.text "--color option expects \"always\", \"auto\", or \"never\" as value"
70 return $ context{Context.color})
71 "[always|auto|never]")
72 "colorize output"
73 , Option "" ["lang"]
74 (ReqArg (\lang _context context -> do
75 return $ context{Context.langs=
76 lang:Context.langs context})
77 "[xx|xx-XX]")
78 "RFC1766 / ISO 639-1 language code (eg, fr, en-GB, etc.)"
79 ]
80
81 run :: Context -> String -> [String] -> IO ()
82 run context cmd args =
83 case cmd of
84 "print" -> Command.Print.run context args
85 "balance" -> Command.Balance.run context args
86 _ -> usage >>= Write.fatal context .
87 ((W.text "unknown command: " <> (W.text $ TL.pack cmd) <> W.line) <>) .
88 W.text . TL.pack