1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE TupleSections #-}
4 module Hcompta.CLI.Command where
6 import qualified Data.Text.Lazy as TL
7 import System.Console.GetOpt
11 import System.Environment (getProgName)
12 import System.Exit (exitWith, ExitCode(..))
13 import qualified System.IO as IO
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 ((<>))
29 , " "++bin++" [option..] <command> [arguments]"
31 , usageInfo "OPTIONS" options
32 , "COMMANDS (use "++bin++" <command> --help for specific help)"
33 , " balance [-i FILE]"
37 options :: Args.Options Context
40 (NoArg (\_opts _context -> do
41 usage >>= IO.hPutStr IO.stderr
42 exitWith ExitSuccess))
44 , Option "v" ["verbosity"]
45 (OptArg (\arg _context context ->
48 case Context.verbosity context of
50 return $ context{Context.verbosity=succ v}
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"
60 "increment or set verbosity level, can be use multiple times"
62 (OptArg (\arg _context context -> do
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]")
74 (ReqArg (\lang _context context -> do
75 return $ context{Context.langs=
76 lang:Context.langs context})
78 "RFC1766 / ISO 639-1 language code (eg, fr, en-GB, etc.)"
81 run :: Context -> String -> [String] -> IO ()
82 run context cmd args =
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) <>) .