1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE TupleSections #-}
3 module Hcompta.CLI.Command where
5 import System.Console.GetOpt
9 import System.Environment (getProgName)
10 import System.Exit (exitWith, ExitCode(..))
11 import qualified System.IO as IO
13 import Hcompta.CLI.Context (Context)
14 import qualified Hcompta.CLI.Args as Args
15 import qualified Hcompta.CLI.Command.Balance as Command.Balance
16 import qualified Hcompta.CLI.Command.Print as Command.Print
17 import qualified Hcompta.CLI.Context as Context
18 import qualified Hcompta.CLI.Write as Write
25 , " "++bin++" [option..] <command> [arguments]"
27 , usageInfo "OPTIONS" options
28 , "COMMANDS (use "++bin++" <command> --help for specific help)"
29 , " balance [-i FILE]"
33 options :: Args.Options Context
37 usage >>= IO.hPutStr IO.stderr
38 exitWith ExitSuccess))
40 , Option "v" ["verbosity"]
41 (OptArg (\arg context ->
44 case Context.verbosity context of
46 return $ context{Context.verbosity=succ v}
48 Just "error" -> return $ context{Context.verbosity=Context.Verbosity_Error}
49 Just "warn" -> return $ context{Context.verbosity=Context.Verbosity_Warn}
50 Just "info" -> return $ context{Context.verbosity=Context.Verbosity_Info}
51 Just "debug" -> return $ context{Context.verbosity=Context.Verbosity_Debug}
52 Just _ -> Write.fatal context
53 "--verbosity option expects \"error\", \"warn\", \"info\", or \"debug\" as value")
54 "error|warn|info|debug"
56 "increment or set verbosity level, can be use multiple times"
58 (OptArg (\arg context -> do
60 Nothing -> return $ Just True
61 Just "always" -> return $ Just True
62 Just "never" -> return $ Just False
63 Just "auto" -> return $ Nothing
64 Just _ -> Write.fatal context
65 "--color option expects \"always\", \"auto\", or \"never\" as value"
66 return $ context{Context.color})
67 "[always|auto|never]")
71 run :: Context -> String -> [String] -> IO ()
72 run context cmd args =
74 "print" -> Command.Print.run context args
75 "balance" -> Command.Balance.run context args
76 _ -> usage >>= Write.fatal context . (("unknown command: " ++ cmd ++ "\n") ++)