1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE TupleSections #-}
4 module Hcompta.CLI.Command where
6 import Control.Monad (Monad(..))
8 import Data.List ((++))
9 import Data.Maybe (Maybe(..), fromMaybe)
10 import Data.Monoid ((<>))
11 import Data.Ord (Ord(..))
12 import Data.String (String)
13 import qualified Data.Text.Lazy as TL
14 import Prelude (($), (.), Bounded(..), Enum(..), IO, unlines)
15 import System.Console.GetOpt
19 import System.Environment (getProgName)
20 import System.Exit (exitSuccess)
21 import qualified System.IO as IO
23 import qualified Hcompta.CLI.Args as Args
24 import qualified Hcompta.CLI.Command.Balance as Command.Balance
25 import qualified Hcompta.CLI.Command.GL as Command.GL
26 import qualified Hcompta.CLI.Command.Journal as Command.Journal
27 import qualified Hcompta.CLI.Command.Journals as Command.Journals
28 import qualified Hcompta.CLI.Command.Stats as Command.Stats
29 import qualified Hcompta.CLI.Command.Tags as Command.Tags
30 import Hcompta.CLI.Context (Context)
31 import qualified Hcompta.CLI.Context as Context
32 import qualified Hcompta.CLI.Lang as Lang
33 import qualified Hcompta.CLI.Write as Write
34 import qualified Hcompta.Lib.Leijen as W
41 , " "++bin++" [option..] <command> [arguments]"
43 , usageInfo "OPTIONS" options
44 , "COMMANDS (use "++bin++" <command> --help for specific help)"
45 , " balance [-i JOURNAL_FILE]"
46 , " [-b BALANCE_FILTER]"
47 , " [-p POSTING_FILTER]"
48 , " [-t TRANSACTION_FILTER]"
49 , " [JOURNAL_FILE] [...]"
50 , " gl [-i JOURNAL_FILE]"
52 , " [-p POSTING_FILTER]"
53 , " [-t TRANSACTION_FILTER]"
54 , " [JOURNAL_FILE] [...]"
55 , " journal [-i JOURNAL_FILE]"
56 , " [-t TRANSACTION_FILTER]"
57 , " [JOURNAL_FILE] [...]"
58 , " journals [-i JOURNAL_FILE]"
59 , " [JOURNAL_FILE] [...]"
60 , " stats [-i JOURNAL_FILE]"
61 , " [-t TRANSACTION_FILTER]"
62 , " [JOURNAL_FILE] [...]"
63 , " tags [-i JOURNAL_FILE]"
64 , " [-t TRANSACTION_FILTER]"
65 , " [JOURNAL_FILE] [...]"
68 options :: Args.Options Context
71 (NoArg (\_opts _context -> do
72 usage >>= IO.hPutStr IO.stderr
75 , Option "v" ["verbosity"]
76 (OptArg (\arg _context context ->
79 case Context.verbosity context of
81 return $ context{Context.verbosity=succ v}
83 Just "error" -> return $ context{Context.verbosity=Context.Verbosity_Error}
84 Just "warn" -> return $ context{Context.verbosity=Context.Verbosity_Warn}
85 Just "info" -> return $ context{Context.verbosity=Context.Verbosity_Info}
86 Just "debug" -> return $ context{Context.verbosity=Context.Verbosity_Debug}
87 Just _ -> Write.fatal context $
88 W.text "--verbosity option expects \"error\", \"warn\", \"info\", or \"debug\" as value")
89 "error|warn|info|debug"
91 "increment or set verbosity level, can be use multiple times"
93 (OptArg (\arg _context context -> do
95 Nothing -> return $ Just True
96 Just "yes" -> return $ Just True
97 Just "no" -> return $ Just False
98 Just "auto" -> return $ Nothing
99 Just _ -> Write.fatal context $
100 W.text "--color option expects \"auto\" (default), \"yes\", or \"no\" as value"
101 return $ context{Context.color})
105 (ReqArg (\lang _context context -> do
106 return $ context{Context.lang =
107 fromMaybe (Context.lang context) $
108 Lang.lang_of_strings [lang]
111 "RFC1766 / ISO 639-1 language code (eg, fr, en-GB, etc.)"
114 run :: Context -> String -> [String] -> IO ()
115 run context cmd args =
117 "balance" -> Command.Balance.run context args
118 "gl" -> Command.GL.run context args
119 "journal" -> Command.Journal.run context args
120 "journals" -> Command.Journals.run context args
121 "stats" -> Command.Stats.run context args
122 "tags" -> Command.Tags.run context args
123 _ -> usage >>= Write.fatal context .
124 ((W.text "unknown command: " <> (W.text $ TL.pack cmd) <> W.line) <>) .