Simplify hcompta-lib.
[comptalang.git] / cli / Hcompta / CLI / Command.hs
index 0774357fe0418988c12974cdcefb998ce4cbb767..b857e17f90a49edea9eb6a5151770d81272acba8 100644 (file)
@@ -19,108 +19,91 @@ import           System.Console.GetOpt
 import           System.Environment (getProgName)
 import           System.Exit (exitSuccess)
 import qualified System.IO as IO
+import qualified Text.WalderLeijen.ANSI.Text as W
 
 import qualified Hcompta.CLI.Args as Args
-import qualified Hcompta.CLI.Command.Balance as Command.Balance
-import qualified Hcompta.CLI.Command.GL as Command.GL
-import qualified Hcompta.CLI.Command.Journal as Command.Journal
+-- import qualified Hcompta.CLI.Command.Balance as Command.Balance
+-- import qualified Hcompta.CLI.Command.GL as Command.GL
+-- import qualified Hcompta.CLI.Command.Journal as Command.Journal
 import qualified Hcompta.CLI.Command.Journals as Command.Journals
-import qualified Hcompta.CLI.Command.Stats as Command.Stats
-import qualified Hcompta.CLI.Command.Tags as Command.Tags
-import           Hcompta.CLI.Context (Context)
-import qualified Hcompta.CLI.Context as Context
+-- import qualified Hcompta.CLI.Command.Stats as Command.Stats
+-- import qualified Hcompta.CLI.Command.Tags as Command.Tags
+import qualified Hcompta.CLI.Context as C
 import qualified Hcompta.CLI.Lang as Lang
 import qualified Hcompta.CLI.Write as Write
-import qualified Hcompta.Lib.Leijen as W
 
-usage :: IO String
-usage = do
+usage :: C.Context -> IO String
+usage = do
        bin <- getProgName
-       return $ unlines $
-               [ "SYNTAX "
-               , "  "++bin++" [option..] <command> [arguments]"
-               , ""
-               , usageInfo "OPTIONS" options
-               , "COMMANDS (use "++bin++" <command> --help for specific help)"
-               , "  balance  [-i FILE_JOURNAL]"
-               , "           [-b FILTER_BALANCE]"
-               , "           [-p FILTER_POSTING]"
-               , "           [-t FILTER_TRANSACTION]"
-               , "           [FILE_JOURNAL] [...]"
-               , "  gl       [-i FILE_JOURNAL]"
-               , "           [-g FILTER_GL]"
-               , "           [-p FILTER_POSTING]"
-               , "           [-t FILTER_TRANSACTION]"
-               , "           [FILE_JOURNAL] [...]"
-               , "  journal  [-i FILE_JOURNAL]"
-               , "           [-t FILTER_TRANSACTION]"
-               , "           [FILE_JOURNAL] [...]"
-               , "  journals [-i FILE_JOURNAL]"
-               , "           [FILE_JOURNAL] [...]"
-               , "  stats    [-i FILE_JOURNAL]"
-               , "           [-t FILTER_TRANSACTION]"
-               , "           [FILE_JOURNAL] [...]"
-               , "  tags     [-i FILE_JOURNAL]"
-               , "           [-t FILTER_TRANSACTION]"
-               , "           [-T FILTER_TAG]"
-               , "           [FILE_JOURNAL] [...]"
-               ]
+       return $ unlines
+        [ C.translate c Lang.Section_Syntax
+        , "  "++bin++" "++C.translate c Lang.Help_Synopsis
+        , ""
+        , usageInfo (C.translate c Lang.Section_Options) (options c)
+        , C.translate c Lang.Section_Commands
+        , "  [bal|balance]       "++C.translate c Lang.Help_Command_Balance
+        , "  [gl|general_ledger] "++C.translate c Lang.Help_Command_General_Ledger
+        , "  [j|journal]         "++C.translate c Lang.Help_Command_Journal
+        , "  [js|journals]       "++C.translate c Lang.Help_Command_Journals
+        , "  stats               "++C.translate c Lang.Help_Command_Stats
+        , "  tags                "++C.translate c Lang.Help_Command_Tags
+        ]
 
-options :: Args.Options Context
-options =
+options :: C.Context -> Args.Options C.Context
+options ctx =
        [ Option "h" ["help"]
-        (NoArg (\_opts _context -> do
-               usage >>= IO.hPutStr IO.stderr
-               exitSuccess))
-        "show this help"
+        (NoArg (\_opts -> do
+               usage ctx >>= IO.hPutStr IO.stderr
+               exitSuccess)) $
+               C.translate ctx Lang.Help_Option_Help
        , Option "v" ["verbosity"]
-        (OptArg (\arg _context context ->
+        (OptArg (\arg c ->
                case arg of
-                Nothing      ->
-                       case Context.verbosity context of
-                        v | v < maxBound ->
-                               return $ context{Context.verbosity=succ v}
-                        _ -> return $ context
-                Just "error" -> return $ context{Context.verbosity=Context.Verbosity_Error}
-                Just "warn"  -> return $ context{Context.verbosity=Context.Verbosity_Warn}
-                Just "info"  -> return $ context{Context.verbosity=Context.Verbosity_Info}
-                Just "debug" -> return $ context{Context.verbosity=Context.Verbosity_Debug}
-                Just _       -> Write.fatal context $
-                       W.text "--verbosity option expects \"error\", \"warn\", \"info\", or \"debug\" as value")
-               "error|warn|info|debug"
-               )
-        "increment or set verbosity level, can be use multiple times"
+                Nothing ->
+                       case C.verbosity c of
+                        v | v < maxBound -> return $ c{C.verbosity=succ v}
+                        _                -> return $ c
+                Just "error" -> return $ c{C.verbosity=C.Verbosity_Error}
+                Just "warn"  -> return $ c{C.verbosity=C.Verbosity_Warn}
+                Just "info"  -> return $ c{C.verbosity=C.Verbosity_Info}
+                Just "debug" -> return $ c{C.verbosity=C.Verbosity_Debug}
+                Just _       -> Write.fatal c Lang.Error_Option_Verbosity)
+               "error|warn|info|debug") $
+               C.translate ctx Lang.Help_Option_Verbosity
        , Option "" ["color"]
-        (OptArg (\arg _context context -> do
+        (OptArg (\arg c -> do
                color <- case arg of
                 Nothing     -> return $ Just True
                 Just "yes"  -> return $ Just True
                 Just "no"   -> return $ Just False
                 Just "auto" -> return $ Nothing
-                Just _      -> Write.fatal context $
-                       W.text "--color option expects \"auto\" (default), \"yes\", or \"no\" as value"
-               return $ context{Context.color})
-         "[auto|yes|no]")
-        "colorize output"
+                Just _      -> Write.fatal c Lang.Error_Option_Color
+               return $ c{C.color})
+         "[auto|yes|no]") $
+               C.translate ctx Lang.Help_Option_Color
        , Option "" ["lang"]
-        (ReqArg (\lang _context context -> do
-               return $ context{Context.lang =
-                       fromMaybe (Context.lang context) $
-                       Lang.lang_of_strings [lang]
+        (ReqArg (\lang c ->
+               return $ c{C.lang =
+                       fromMaybe (C.lang c) $
+                       Lang.from_Strings [lang]
                 })
-         "[xx|xx-XX]")
-        "RFC1766 / ISO 639-1 language code (eg, fr, en-GB, etc.)"
+         "[xx|xx-XX]") $
+               C.translate ctx Lang.Help_Option_Lang
        ]
 
-run :: Context -> String -> [String] -> IO ()
-run context cmd args =
+run :: C.Context -> String -> [String] -> IO ()
+run c cmd args =
        case cmd of
-        "balance"  -> Command.Balance.run  context args
-        "gl"       -> Command.GL.run       context args
-        "journal"  -> Command.Journal.run  context args
-        "journals" -> Command.Journals.run context args
-        "stats"    -> Command.Stats.run    context args
-        "tags"     -> Command.Tags.run     context args
-        _ -> usage >>= Write.fatal context .
-               ((W.text "unknown command: " <> (W.text $ TL.pack cmd) <> W.line) <>) .
+        -- "bal"            -> Command.Balance.run  c args
+        -- "balance"        -> Command.Balance.run  c args
+        -- "gl"             -> Command.GL.run       c args
+        -- "general_ledger" -> Command.GL.run       c args
+        -- "j"              -> Command.Journal.run  c args
+        -- "journal"        -> Command.Journal.run  c args
+        -- "js"             -> Command.Journals.run c args
+        "journals"       -> Command.Journals.run c args
+        -- "stats"          -> Command.Stats.run    c args
+        -- "tags"           -> Command.Tags.run     c args
+        _ -> usage c >>= Write.fatal c .
+               ((C.translate c (Lang.Error_Unkown_command cmd) <> W.line) <>) .
                W.text . TL.pack