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 FILE_JOURNAL]"
46 , " [-b FILTER_BALANCE]"
47 , " [-p FILTER_POSTING]"
48 , " [-t FILTER_TRANSACTION]"
49 , " [FILE_JOURNAL] [...]"
50 , " gl [-i FILE_JOURNAL]"
52 , " [-p FILTER_POSTING]"
53 , " [-t FILTER_TRANSACTION]"
54 , " [FILE_JOURNAL] [...]"
55 , " journal [-i FILE_JOURNAL]"
56 , " [-t FILTER_TRANSACTION]"
57 , " [FILE_JOURNAL] [...]"
58 , " journals [-i FILE_JOURNAL]"
59 , " [FILE_JOURNAL] [...]"
60 , " stats [-i FILE_JOURNAL]"
61 , " [-t FILTER_TRANSACTION]"
62 , " [FILE_JOURNAL] [...]"
63 , " tags [-i FILE_JOURNAL]"
64 , " [-t FILTER_TRANSACTION]"
66 , " [FILE_JOURNAL] [...]"
69 options :: Args.Options Context
72 (NoArg (\_opts _context -> do
73 usage >>= IO.hPutStr IO.stderr
76 , Option "v" ["verbosity"]
77 (OptArg (\arg _context context ->
80 case Context.verbosity context of
82 return $ context{Context.verbosity=succ v}
84 Just "error" -> return $ context{Context.verbosity=Context.Verbosity_Error}
85 Just "warn" -> return $ context{Context.verbosity=Context.Verbosity_Warn}
86 Just "info" -> return $ context{Context.verbosity=Context.Verbosity_Info}
87 Just "debug" -> return $ context{Context.verbosity=Context.Verbosity_Debug}
88 Just _ -> Write.fatal context $
89 W.text "--verbosity option expects \"error\", \"warn\", \"info\", or \"debug\" as value")
90 "error|warn|info|debug"
92 "increment or set verbosity level, can be use multiple times"
94 (OptArg (\arg _context context -> do
96 Nothing -> return $ Just True
97 Just "yes" -> return $ Just True
98 Just "no" -> return $ Just False
99 Just "auto" -> return $ Nothing
100 Just _ -> Write.fatal context $
101 W.text "--color option expects \"auto\" (default), \"yes\", or \"no\" as value"
102 return $ context{Context.color})
106 (ReqArg (\lang _context context -> do
107 return $ context{Context.lang =
108 fromMaybe (Context.lang context) $
109 Lang.lang_of_strings [lang]
112 "RFC1766 / ISO 639-1 language code (eg, fr, en-GB, etc.)"
115 run :: Context -> String -> [String] -> IO ()
116 run context cmd args =
118 "balance" -> Command.Balance.run context args
119 "gl" -> Command.GL.run context args
120 "journal" -> Command.Journal.run context args
121 "journals" -> Command.Journals.run context args
122 "stats" -> Command.Stats.run context args
123 "tags" -> Command.Tags.run context args
124 _ -> usage >>= Write.fatal context .
125 ((W.text "unknown command: " <> (W.text $ TL.pack cmd) <> W.line) <>) .