1 {-# LANGUAGE LambdaCase #-}
2 {-# LANGUAGE NamedFieldPuns #-}
3 {-# LANGUAGE TupleSections #-}
4 module Hcompta.CLI.Command where
6 import System.Console.GetOpt
10 import System.Environment (getProgName)
11 import System.Exit (exitWith, ExitCode(..))
12 import qualified System.IO as IO
14 import qualified Hcompta.CLI.Command.Balance as Command.Balance
15 import qualified Hcompta.CLI.Command.Print as Command.Print
16 import qualified Hcompta.CLI.Args as Args
17 import qualified Hcompta.CLI.Context as Context
18 import Hcompta.CLI.Context (Context)
19 import qualified Hcompta.CLI.Write as Write
26 , " "++bin++" [option..] <command> [arguments]"
28 , usageInfo "OPTIONS" options
34 options :: Args.Options Context
38 usage >>= IO.hPutStr IO.stderr
39 exitWith ExitSuccess))
41 , Option "v" ["verbose"]
42 (NoArg (\context -> return $ context{Context.verb=True}))
43 "show intermediate results"
45 (OptArg (\arg context -> do
47 Nothing -> return $ Just True
48 Just "always" -> return $ Just True
49 Just "never" -> return $ Just False
50 Just "auto" -> return $ Nothing
51 Just _ -> Write.fatal context "--color option expects \"always\", \"auto\", or \"never\" as value"
52 return $ context{Context.color})
53 "[always|auto|never]")
57 run :: Context -> String -> [String] -> IO ()
58 run context cmd args =
60 "print" -> Command.Print.run context args
61 "balance" -> Command.Balance.run context args
62 _ -> usage >>= Write.fatal context . (("unknown command: " ++ cmd ++ "\n") ++)