From d8066d6a72a8acd542a296e3fdffc5dd1d8d8b84 Mon Sep 17 00:00:00 2001 From: Wilmer Zwietering Date: Sat, 2 Apr 2022 14:07:15 +0200 Subject: [PATCH 1/5] Replace mode --- backend/team-proj-abbr/data/test_file.txt | 1 + .../team-proj-abbr/data/test_file_target.txt | 1 + backend/team-proj-abbr/lib-cli/LibCli/Main.hs | 23 ++++++++++++++++--- .../lib-core/LibCore/Decoder.hs | 7 +++++- .../lib-core/LibCore/KnowledgeBase.hs | 4 ++-- .../lib-core/LibCore/OutputInterface.hs | 6 +++-- 6 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 backend/team-proj-abbr/data/test_file.txt create mode 100644 backend/team-proj-abbr/data/test_file_target.txt diff --git a/backend/team-proj-abbr/data/test_file.txt b/backend/team-proj-abbr/data/test_file.txt new file mode 100644 index 00000000..e23ff85f --- /dev/null +++ b/backend/team-proj-abbr/data/test_file.txt @@ -0,0 +1 @@ +Hello world. This file is written to test the expansions of kb_example.csv. We should see that @@hl is expanded to hello. This is a @@thm, which we cannot @@prf. No @@lmm or @@ax can be used, because this is not a scientific theorem. \ No newline at end of file diff --git a/backend/team-proj-abbr/data/test_file_target.txt b/backend/team-proj-abbr/data/test_file_target.txt new file mode 100644 index 00000000..9507e9d3 --- /dev/null +++ b/backend/team-proj-abbr/data/test_file_target.txt @@ -0,0 +1 @@ +Hello world. This file is written to test the expansions of kb_example.csv. We should see that hello is expanded to hello. This is a theorem, which we cannot proof. No lemma or axiom can be used, because this is not a scientific theorem. \ No newline at end of file diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs index 2528c82a..9a42f9d5 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs @@ -9,8 +9,14 @@ Stability : experimental module LibCli.Main where -import qualified LibCli.Spec as CS (ShortHndr (..), cliModes) -import qualified System.Console.CmdArgs as CMD +import LibCli.Spec (ShortHndr (input, out)) +import qualified LibCli.Spec as CS (ShortHndr (..), cliModes) +import LibCore.Decoder (decode) +import LibCore.KnowledgeBase (getKnowledgeBase) +import LibCore.Mapper (mapParseStructure) +import LibCore.OutputInterface (returnOutput) +import LibCore.Parser (doParse) +import qualified System.Console.CmdArgs as CMD ----------------------- -- Command Handlers: -- @@ -19,13 +25,24 @@ import qualified System.Console.CmdArgs as CMD -- TODO(tech-debt): define a typeclass for the modes instead of the pattern matching -- TODO: (future task) implement the actual handlers with the business logic. mockCliHandler :: CS.ShortHndr -> IO () -mockCliHandler c@CS.Replace{} = print $ "replacing! --> " ++ show c +-- mockCliHandler c@CS.Replace{} = print $ "replacing! --> " ++ show c +mockCliHandler c@CS.Replace{} = replaceMode c mockCliHandler c@CS.Expand{} = print $ "expanding! --> " ++ show c mockCliHandler c@CS.List{} = print $ "listing! --> " ++ show c mockCliHandler c@CS.Add{} = print $ "adding! --> " ++ show c mockCliHandler c@CS.Update{} = print $ "updating! --> " ++ show c mockCliHandler c@CS.Delete{} = print $ "deleting! --> " ++ show c +replaceMode :: ShortHndr -> IO () +replaceMode c@CS.Replace {} = do + case input c of + Nothing -> error "No input file was found" + Just f -> do + s <- readFile f + returnOutput (out c) (decode $ mapParseStructure getKnowledgeBase $ doParse s) +-- Impossible case because of the mockCliHandler +replaceMode _ = undefined + ---------------------------- -- Executable entrypoiny: -- ---------------------------- diff --git a/backend/team-proj-abbr/lib-core/LibCore/Decoder.hs b/backend/team-proj-abbr/lib-core/LibCore/Decoder.hs index 41270eb2..7ceeb6c4 100644 --- a/backend/team-proj-abbr/lib-core/LibCore/Decoder.hs +++ b/backend/team-proj-abbr/lib-core/LibCore/Decoder.hs @@ -8,7 +8,12 @@ Stability : experimental module LibCore.Decoder where +import LibCore.Models (Keyword (Keyword), Token (DoMap, NoToken)) import LibCore.Parser (ParseStructure) decode :: ParseStructure -> String -decode = undefined +decode s = unwords $ map tokenToString s + +tokenToString :: Token -> String +tokenToString (NoToken s) = s +tokenToString (DoMap (Keyword k _)) = k diff --git a/backend/team-proj-abbr/lib-core/LibCore/KnowledgeBase.hs b/backend/team-proj-abbr/lib-core/LibCore/KnowledgeBase.hs index 7494fcb2..da180a9f 100644 --- a/backend/team-proj-abbr/lib-core/LibCore/KnowledgeBase.hs +++ b/backend/team-proj-abbr/lib-core/LibCore/KnowledgeBase.hs @@ -8,10 +8,10 @@ Stability : experimental module LibCore.KnowledgeBase where -import Data.Map (Map) +import Data.Map (Map, empty) import LibCore.Models (Keyword) type KnowledgeBaseStructure = Map Keyword Keyword getKnowledgeBase :: KnowledgeBaseStructure -getKnowledgeBase = undefined +getKnowledgeBase = empty diff --git a/backend/team-proj-abbr/lib-core/LibCore/OutputInterface.hs b/backend/team-proj-abbr/lib-core/LibCore/OutputInterface.hs index 6632fd21..26e94a67 100644 --- a/backend/team-proj-abbr/lib-core/LibCore/OutputInterface.hs +++ b/backend/team-proj-abbr/lib-core/LibCore/OutputInterface.hs @@ -8,5 +8,7 @@ Stability : experimental module LibCore.OutputInterface where -returnOutput :: String -> IO () -returnOutput = undefined +returnOutput :: Maybe FilePath -> String -> IO () +returnOutput f = case f of + Nothing -> error "No output file found" + Just s -> writeFile s From b871fb60165cf3720484c8f723f77beb2958042b Mon Sep 17 00:00:00 2001 From: Wilmer Zwietering Date: Sat, 2 Apr 2022 15:20:49 +0200 Subject: [PATCH 2/5] Does not work. GADT for modes --- backend/team-proj-abbr/lib-cli/LibCli/Main.hs | 33 ++-- backend/team-proj-abbr/lib-cli/LibCli/Spec.hs | 162 ++++++++++-------- 2 files changed, 111 insertions(+), 84 deletions(-) diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs index 9a42f9d5..8e28f2c9 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs @@ -9,8 +9,7 @@ Stability : experimental module LibCli.Main where -import LibCli.Spec (ShortHndr (input, out)) -import qualified LibCli.Spec as CS (ShortHndr (..), cliModes) +import LibCli.Spec import LibCore.Decoder (decode) import LibCore.KnowledgeBase (getKnowledgeBase) import LibCore.Mapper (mapParseStructure) @@ -22,26 +21,28 @@ import qualified System.Console.CmdArgs as CMD -- Command Handlers: -- ----------------------- --- TODO(tech-debt): define a typeclass for the modes instead of the pattern matching -- TODO: (future task) implement the actual handlers with the business logic. -mockCliHandler :: CS.ShortHndr -> IO () --- mockCliHandler c@CS.Replace{} = print $ "replacing! --> " ++ show c -mockCliHandler c@CS.Replace{} = replaceMode c -mockCliHandler c@CS.Expand{} = print $ "expanding! --> " ++ show c -mockCliHandler c@CS.List{} = print $ "listing! --> " ++ show c -mockCliHandler c@CS.Add{} = print $ "adding! --> " ++ show c -mockCliHandler c@CS.Update{} = print $ "updating! --> " ++ show c -mockCliHandler c@CS.Delete{} = print $ "deleting! --> " ++ show c +mockCliHandler :: ShortHndrModes -> IO () +mockCliHandler (Exp e) = handleExpMode e +mockCliHandler (Kbt k) = handleKbtMode k -replaceMode :: ShortHndr -> IO () -replaceMode c@CS.Replace {} = do +handleExpMode :: Expansion -> IO () +handleExpMode (Re r) = replaceMode r +handleExpMode (Ex c) = print $ "expanding! --> " ++ show c + +handleKbtMode :: KnowledgeBaseTypes -> IO () +handleKbtMode (Lst c) = print $ "listing! --> " ++ show c +handleKbtMode (Ad c) = print $ "adding! --> " ++ show c +handleKbtMode (Up c) = print $ "updating! --> " ++ show c +handleKbtMode (Del c) = print $ "deleting! --> " ++ show c + +replaceMode :: Replace -> IO () +replaceMode c = do case input c of Nothing -> error "No input file was found" Just f -> do s <- readFile f returnOutput (out c) (decode $ mapParseStructure getKnowledgeBase $ doParse s) --- Impossible case because of the mockCliHandler -replaceMode _ = undefined ---------------------------- -- Executable entrypoiny: -- @@ -67,4 +68,4 @@ replaceMode _ = undefined -- -- * See 'LibCli.Spec' for more information about the CLI endpoints. cliMain :: IO () -cliMain = mockCliHandler =<< CMD.cmdArgs (CMD.modes CS.cliModes) +cliMain = mockCliHandler =<< CMD.cmdArgs (CMD.modes cliModes) diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs index 33177d7e..0e832260 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs @@ -1,5 +1,5 @@ {-# LANGUAGE DeriveDataTypeable #-} - +{-# LANGUAGE GADTs #-} {-| Description : Command Line Interface - Specification Copyright : Copyright (c) 2022 Pier Carlo Cadoppi, Dmitrii Orlov, Wilmer Zwietering @@ -8,10 +8,7 @@ Maintainer : p.c.cadoppi@students.uu.nl; d.orlov@student.tue.nl; w.j.zwietering Stability : experimental -} -module LibCli.Spec - ( cliModes - , ShortHndr(..) - ) where +module LibCli.Spec where import qualified System.Console.CmdArgs as CMD @@ -19,40 +16,73 @@ import qualified System.Console.CmdArgs as CMD -- CLI interface specificaitons: -- ----------------------------------- +data Expansion where + Re :: Replace -> Expansion + Ex :: Expand -> Expansion + deriving (CMD.Data, CMD.Typeable, Show) + +data KnowledgeBaseTypes where + Lst :: List -> KnowledgeBaseTypes + Ad :: Add -> KnowledgeBaseTypes + Up :: Update -> KnowledgeBaseTypes + Del :: Delete -> KnowledgeBaseTypes + deriving (CMD.Data, CMD.Typeable, Show) + +data ShortHndrModes where + Exp :: Expansion -> ShortHndrModes + Kbt :: KnowledgeBaseTypes -> ShortHndrModes + deriving (CMD.Data, CMD.Typeable, Show) + + -- |ShortHndr CLI interface specification. -data ShortHndr - -- |Defines the arguments for the replace command +-- |Defines the arguments for the replace command +data Replace = Replace - { input :: Maybe FilePath - , out :: Maybe FilePath - , inplace :: Maybe Bool - , kb :: Maybe FilePath + { input :: Maybe FilePath + , out :: Maybe FilePath + , inplace :: Maybe Bool + , replace_kb :: Maybe FilePath } - -- |Defines the arguments for the expand command - | Expand - { abbreviation :: String - , kb :: Maybe FilePath + deriving (CMD.Data, CMD.Typeable, Show) + +-- |Defines the arguments for the expand command +data Expand + = Expand + { expand_abbr :: String + , expand_kb :: Maybe FilePath } - -- |Defines the arguments for the list command - | List - { kb :: Maybe FilePath + deriving (CMD.Data, CMD.Typeable, Show) + +-- |Defines the arguments for the list command +data List + = List + { list_kb :: Maybe FilePath } - -- |Defines the arguments for the add command - | Add - { abbreviation :: String - , expansion :: String - , kb :: Maybe FilePath + deriving (CMD.Data, CMD.Typeable, Show) + +-- |Defines the arguments for the add command +data Add + = Add + { add_abbr :: String + , add_expansion :: String + , add_kb :: Maybe FilePath } - -- |Defines the arguments for the update command - | Update - { abbreviation :: String - , expansion :: String - , kb :: Maybe FilePath + deriving (CMD.Data, CMD.Typeable, Show) + +-- |Defines the arguments for the update command +data Update + = Update + { update_abbr :: String + , update_expansion :: String + , update_kb :: Maybe FilePath } - -- |Defines the arguments for the delete command - | Delete - { abbreviation :: String - , kb :: Maybe FilePath + deriving (CMD.Data, CMD.Typeable, Show) + +-- |Defines the arguments for the delete command +data Delete + = Delete + { delete_abbr :: String + , delete_kb :: Maybe FilePath } deriving (CMD.Data, CMD.Typeable, Show) @@ -64,75 +94,71 @@ fileFlags h f = f CMD.&= CMD.help h CMD.&= CMD.typFile -- Expansion commands: -- ------------------------- -replace :: ShortHndr -replace = +replace :: ShortHndrModes +replace = Exp ( Re $ Replace { input = fileFlags "Source file" (pure "shorthndr-input.txt") , out = fileFlags "Output file" (pure "shorthndr--out.txt") - , kb = fileFlags "Knowledge Base source file" + , replace_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") , inplace = CMD.def - } + }) CMD.&= CMD.help "Replace all abreviations in the provided file with their expansions" -expand :: ShortHndr -expand = +expand :: ShortHndrModes +expand = Exp ( Ex $ Expand - { abbreviation = CMD.def - , kb = fileFlags "Knowledge Base source file" + { expand_abbr = CMD.def + , expand_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") - } + }) CMD.&= CMD.help "Expand a provided abbreviation abbreviation if one is found" -expansionModes :: [ShortHndr] -expansionModes = [replace, expand] ------------------------------ -- Knowledge Base commands: -- ------------------------------ -list :: ShortHndr -list = - List { kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") - } +list :: ShortHndrModes +list = Kbt ( Lst $ + List { list_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") + }) CMD.&= CMD.help "List all records of the Knowledge Base" -add :: ShortHndr -add = - Add { abbreviation = CMD.def - , expansion = CMD.def - , kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") - } +add :: ShortHndrModes +add = Kbt ( Ad $ + Add { add_abbr = CMD.def + , add_expansion = CMD.def + , add_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") + }) CMD.&= CMD.help "Add a new abbreviation record to the Knowledge Base" -update :: ShortHndr -update = +update :: ShortHndrModes +update = Kbt ( Up $ Update - { abbreviation = CMD.def - , expansion = CMD.def - , kb = fileFlags "Knowledge Base source file" + { update_abbr = CMD.def + , update_expansion = CMD.def + , update_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") - } + }) CMD.&= CMD.help "Update an existing abbreviation record in the Knowledge Base" -delete :: ShortHndr -delete = +delete :: ShortHndrModes +delete = Kbt (Del $ Delete - { abbreviation = CMD.def - , kb = fileFlags "Knowledge Base source file" + { delete_abbr = CMD.def + , delete_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") - } + }) CMD.&= CMD.help "Delete an abbreviation record from the Knowledge Base" -kbModes :: [ShortHndr] -kbModes = [list, add, update, delete] ----------------------------- -- All exported CLI modes: -- ----------------------------- -cliModes :: [ShortHndr] -cliModes = expansionModes ++ kbModes +cliModes :: [ShortHndrModes] +cliModes = [replace, expand, list, add, update, delete] From a8297f749e4531419b5bacac40bbc520d936cced Mon Sep 17 00:00:00 2001 From: Wilmer Zwietering Date: Sat, 2 Apr 2022 15:28:46 +0200 Subject: [PATCH 3/5] Apply hint --- backend/team-proj-abbr/lib-cli/LibCli/Spec.hs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs index 0e832260..dc9e11ab 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs @@ -54,10 +54,8 @@ data Expand deriving (CMD.Data, CMD.Typeable, Show) -- |Defines the arguments for the list command -data List - = List - { list_kb :: Maybe FilePath - } +newtype List + = List { list_kb :: Maybe FilePath } deriving (CMD.Data, CMD.Typeable, Show) -- |Defines the arguments for the add command From 41cccc9bece40a7d7013e3e8ee87fc6fd211eee2 Mon Sep 17 00:00:00 2001 From: Wilmer Zwietering Date: Sun, 3 Apr 2022 10:19:47 +0200 Subject: [PATCH 4/5] Replace mode using new type system --- backend/team-proj-abbr/lib-cli/LibCli/Main.hs | 24 ++++++--- backend/team-proj-abbr/lib-cli/LibCli/Spec.hs | 49 ++++++++++++++++++- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs index 8e28f2c9..ce9a2d3a 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs @@ -10,12 +10,16 @@ Stability : experimental module LibCli.Main where import LibCli.Spec -import LibCore.Decoder (decode) -import LibCore.KnowledgeBase (getKnowledgeBase) -import LibCore.Mapper (mapParseStructure) -import LibCore.OutputInterface (returnOutput) -import LibCore.Parser (doParse) -import qualified System.Console.CmdArgs as CMD +import LibCore.Decoder (decode) +import LibCore.KnowledgeBase (getKnowledgeBase) +import LibCore.Mapper (mapParseStructure) +import LibCore.OutputInterface (returnOutput) +import LibCore.Parser (doParse) +import System.Console.CmdArgs.Explicit + ( HelpFormat (HelpFormatDefault) + , helpText + , processArgs + ) ----------------------- -- Command Handlers: -- @@ -25,6 +29,7 @@ import qualified System.Console.CmdArgs as CMD mockCliHandler :: ShortHndrModes -> IO () mockCliHandler (Exp e) = handleExpMode e mockCliHandler (Kbt k) = handleKbtMode k +mockCliHandler Hlp = print $ helpText [] HelpFormatDefault arguments handleExpMode :: Expansion -> IO () handleExpMode (Re r) = replaceMode r @@ -68,4 +73,9 @@ replaceMode c = do -- -- * See 'LibCli.Spec' for more information about the CLI endpoints. cliMain :: IO () -cliMain = mockCliHandler =<< CMD.cmdArgs (CMD.modes cliModes) +cliMain = do + xs <- processArgs arguments + case xs of + Exp ex -> handleExpMode ex + Kbt kbt -> print kbt + Hlp -> print $ helpText [] HelpFormatDefault arguments diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs index dc9e11ab..769e3704 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs @@ -1,5 +1,6 @@ {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GADTs #-} + {-| Description : Command Line Interface - Specification Copyright : Copyright (c) 2022 Pier Carlo Cadoppi, Dmitrii Orlov, Wilmer Zwietering @@ -10,7 +11,16 @@ Stability : experimental module LibCli.Spec where -import qualified System.Console.CmdArgs as CMD +import qualified System.Console.CmdArgs as CMD +import System.Console.CmdArgs.Explicit + ( Mode + , flagArg + , flagHelpSimple + , flagNone + , flagReq + , mode + , modes + ) ----------------------------------- -- CLI interface specificaitons: -- @@ -31,6 +41,7 @@ data KnowledgeBaseTypes where data ShortHndrModes where Exp :: Expansion -> ShortHndrModes Kbt :: KnowledgeBaseTypes -> ShortHndrModes + Hlp :: ShortHndrModes deriving (CMD.Data, CMD.Typeable, Show) @@ -160,3 +171,39 @@ delete = Kbt (Del $ cliModes :: [ShortHndrModes] cliModes = [replace, expand, list, add, update, delete] + +defaultMode :: ShortHndrModes +defaultMode = Hlp + +helpMode :: ShortHndrModes -> ShortHndrModes +helpMode _ = Hlp + +-- Explicit arguments +arguments :: Mode ShortHndrModes +arguments = modes "ShortHandr" defaultMode "Use 'replace' to enter replace mode" [replaceArgs] + +replaceArgs :: Mode ShortHndrModes +replaceArgs = mode "replace" initial + "Replace all abreviations in the provided file with their expansions" + (flagArg (updateMode "") "yes") + [ + flagReq ["input", "i"] (updateMode "input") "FILENAME" "Input filename" + ,flagReq ["out", "o"] (updateMode "out") "FILENAME" "Output filename" + ,flagReq ["k"] (updateMode "replace_kb") "FILENAME" "Knowledgebase filename" + ,flagNone ["inplace"] (setInplace True) "inplace" + ,flagHelpSimple helpMode + ] + where initial = Exp $ Re $ Replace { input = Just "", out = Just "", replace_kb = Just "", inplace = Just False } + +setInplace :: Bool -> ShortHndrModes -> ShortHndrModes +setInplace b (Exp (Re r)) = Exp $ Re $ r {inplace = Just b} +setInplace _ r = r + +updateMode :: String -> String -> ShortHndrModes -> Either String ShortHndrModes +updateMode "input" s (Exp (Re r)) = Right $ Exp $ Re $ r {input = Just s} +updateMode "out" s (Exp (Re r)) = Right $ Exp $ Re $ r {out = Just s} +updateMode "replace_kb" s (Exp (Re r)) = Right $ Exp $ Re $ r {replace_kb = Just s} +updateMode _ _ e@(Exp (Re _)) = Right e +updateMode _ _ (Exp (Ex _)) = undefined +updateMode _ _ (Kbt _) = undefined +updateMode _ _ Hlp = undefined From ca66ac542a0b4a1787b162f18ecb6feed88f5d92 Mon Sep 17 00:00:00 2001 From: Wilmer Zwietering Date: Sun, 3 Apr 2022 10:45:03 +0200 Subject: [PATCH 5/5] Added back all modes using CmdArgs.Explicit --- backend/team-proj-abbr/lib-cli/LibCli/Main.hs | 11 +- backend/team-proj-abbr/lib-cli/LibCli/Spec.hs | 169 ++++++++---------- 2 files changed, 82 insertions(+), 98 deletions(-) diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs index ce9a2d3a..c4173c80 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Main.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Main.hs @@ -16,7 +16,7 @@ import LibCore.Mapper (mapParseStructure) import LibCore.OutputInterface (returnOutput) import LibCore.Parser (doParse) import System.Console.CmdArgs.Explicit - ( HelpFormat (HelpFormatDefault) + ( HelpFormat (HelpFormatAll) , helpText , processArgs ) @@ -26,11 +26,6 @@ import System.Console.CmdArgs.Explicit ----------------------- -- TODO: (future task) implement the actual handlers with the business logic. -mockCliHandler :: ShortHndrModes -> IO () -mockCliHandler (Exp e) = handleExpMode e -mockCliHandler (Kbt k) = handleKbtMode k -mockCliHandler Hlp = print $ helpText [] HelpFormatDefault arguments - handleExpMode :: Expansion -> IO () handleExpMode (Re r) = replaceMode r handleExpMode (Ex c) = print $ "expanding! --> " ++ show c @@ -77,5 +72,5 @@ cliMain = do xs <- processArgs arguments case xs of Exp ex -> handleExpMode ex - Kbt kbt -> print kbt - Hlp -> print $ helpText [] HelpFormatDefault arguments + Kbt kbt -> handleKbtMode kbt + Hlp -> print $ helpText [] HelpFormatAll arguments diff --git a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs index 769e3704..a6160678 100644 --- a/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs +++ b/backend/team-proj-abbr/lib-cli/LibCli/Spec.hs @@ -1,5 +1,4 @@ -{-# LANGUAGE DeriveDataTypeable #-} -{-# LANGUAGE GADTs #-} +{-# LANGUAGE GADTs #-} {-| Description : Command Line Interface - Specification @@ -11,7 +10,6 @@ Stability : experimental module LibCli.Spec where -import qualified System.Console.CmdArgs as CMD import System.Console.CmdArgs.Explicit ( Mode , flagArg @@ -29,20 +27,20 @@ import System.Console.CmdArgs.Explicit data Expansion where Re :: Replace -> Expansion Ex :: Expand -> Expansion - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) data KnowledgeBaseTypes where Lst :: List -> KnowledgeBaseTypes Ad :: Add -> KnowledgeBaseTypes Up :: Update -> KnowledgeBaseTypes Del :: Delete -> KnowledgeBaseTypes - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) data ShortHndrModes where Exp :: Expansion -> ShortHndrModes Kbt :: KnowledgeBaseTypes -> ShortHndrModes Hlp :: ShortHndrModes - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) -- |ShortHndr CLI interface specification. @@ -54,7 +52,7 @@ data Replace , inplace :: Maybe Bool , replace_kb :: Maybe FilePath } - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) -- |Defines the arguments for the expand command data Expand @@ -62,12 +60,12 @@ data Expand { expand_abbr :: String , expand_kb :: Maybe FilePath } - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) -- |Defines the arguments for the list command newtype List = List { list_kb :: Maybe FilePath } - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) -- |Defines the arguments for the add command data Add @@ -76,7 +74,7 @@ data Add , add_expansion :: String , add_kb :: Maybe FilePath } - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) -- |Defines the arguments for the update command data Update @@ -85,7 +83,7 @@ data Update , update_expansion :: String , update_kb :: Maybe FilePath } - deriving (CMD.Data, CMD.Typeable, Show) + deriving (Show) -- |Defines the arguments for the delete command data Delete @@ -93,85 +91,13 @@ data Delete { delete_abbr :: String , delete_kb :: Maybe FilePath } - deriving (CMD.Data, CMD.Typeable, Show) - --- |Utility function to provide help for the file type arguments. -fileFlags :: String -> Maybe FilePath -> Maybe FilePath -fileFlags h f = f CMD.&= CMD.help h CMD.&= CMD.typFile - -------------------------- --- Expansion commands: -- -------------------------- - -replace :: ShortHndrModes -replace = Exp ( Re $ - Replace - { input = fileFlags "Source file" (pure "shorthndr-input.txt") - , out = fileFlags "Output file" (pure "shorthndr--out.txt") - , replace_kb = fileFlags "Knowledge Base source file" - (pure "shorthndr-kb.csv") - , inplace = CMD.def - }) - CMD.&= CMD.help - "Replace all abreviations in the provided file with their expansions" - -expand :: ShortHndrModes -expand = Exp ( Ex $ - Expand - { expand_abbr = CMD.def - , expand_kb = fileFlags "Knowledge Base source file" - (pure "shorthndr-kb.csv") - }) - CMD.&= CMD.help - "Expand a provided abbreviation abbreviation if one is found" - - ------------------------------- --- Knowledge Base commands: -- ------------------------------- - -list :: ShortHndrModes -list = Kbt ( Lst $ - List { list_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") - }) - CMD.&= CMD.help "List all records of the Knowledge Base" - -add :: ShortHndrModes -add = Kbt ( Ad $ - Add { add_abbr = CMD.def - , add_expansion = CMD.def - , add_kb = fileFlags "Knowledge Base source file" (pure "shorthndr-kb.csv") - }) - CMD.&= CMD.help "Add a new abbreviation record to the Knowledge Base" - -update :: ShortHndrModes -update = Kbt ( Up $ - Update - { update_abbr = CMD.def - , update_expansion = CMD.def - , update_kb = fileFlags "Knowledge Base source file" - (pure "shorthndr-kb.csv") - }) - CMD.&= CMD.help - "Update an existing abbreviation record in the Knowledge Base" - -delete :: ShortHndrModes -delete = Kbt (Del $ - Delete - { delete_abbr = CMD.def - , delete_kb = fileFlags "Knowledge Base source file" - (pure "shorthndr-kb.csv") - }) - CMD.&= CMD.help "Delete an abbreviation record from the Knowledge Base" + deriving (Show) ----------------------------- -- All exported CLI modes: -- ----------------------------- -cliModes :: [ShortHndrModes] -cliModes = [replace, expand, list, add, update, delete] - defaultMode :: ShortHndrModes defaultMode = Hlp @@ -180,12 +106,12 @@ helpMode _ = Hlp -- Explicit arguments arguments :: Mode ShortHndrModes -arguments = modes "ShortHandr" defaultMode "Use 'replace' to enter replace mode" [replaceArgs] +arguments = modes "ShortHandr" defaultMode "Use 'replace' to enter replace mode" [replaceArgs, expandArgs, listArgs, addArgs, updateArgs, deleteArgs] replaceArgs :: Mode ShortHndrModes replaceArgs = mode "replace" initial "Replace all abreviations in the provided file with their expansions" - (flagArg (updateMode "") "yes") + (flagArg (updateMode "") "replace") [ flagReq ["input", "i"] (updateMode "input") "FILENAME" "Input filename" ,flagReq ["out", "o"] (updateMode "out") "FILENAME" "Output filename" @@ -199,11 +125,74 @@ setInplace :: Bool -> ShortHndrModes -> ShortHndrModes setInplace b (Exp (Re r)) = Exp $ Re $ r {inplace = Just b} setInplace _ r = r +expandArgs :: Mode ShortHndrModes +expandArgs = mode "expand" initial + "Expand a provided abbreviation abbreviation if one is found" + (flagArg (updateMode "abbr") "abbreviation") + [ + flagReq ["k"] (updateMode "expand_kb") "FILENAME" "Knowledgebase filename" + ,flagHelpSimple helpMode + ] + where initial = Exp $ Ex $ Expand { expand_abbr = "", expand_kb = Just "" } + +listArgs :: Mode ShortHndrModes +listArgs = mode "list" initial + "List all records of the Knowledge Base" + (flagArg (updateMode "") "list") + [ + flagReq ["k"] (updateMode "list_kb") "FILENAME" "Knowledgebase filename" + ,flagHelpSimple helpMode + ] + where initial = Kbt $ Lst $ List { list_kb = Just "" } + +addArgs :: Mode ShortHndrModes +addArgs = mode "add" initial + "Add a new abbreviation record to the Knowledge Base" + (flagArg (updateMode "") "add") + [ + flagReq ["k"] (updateMode "add_kb") "FILENAME" "Knowledgebase filename" + ,flagReq ["ex"] (updateMode "add_ex") "expansion" "Abbreviation expansion" + ,flagReq ["abbr"] (updateMode "add_abbr") "abbreviation" "Abbreviation" + ,flagHelpSimple helpMode + ] + where initial = Kbt $ Ad $ Add { add_abbr = "", add_expansion = "", add_kb = Just "" } + +updateArgs :: Mode ShortHndrModes +updateArgs = mode "update" initial + "Update an existing abbreviation record in the Knowledge Base" + (flagArg (updateMode "") "update") + [ + flagReq ["k"] (updateMode "update_kb") "FILENAME" "Knowledgebase filename" + ,flagReq ["ex"] (updateMode "update_ex") "expansion" "Abbreviation expansion" + ,flagReq ["abbr"] (updateMode "update_abbr") "abbreviation" "Abbreviation" + ,flagHelpSimple helpMode + ] + where initial = Kbt $ Up $ Update { update_abbr = "", update_expansion = "", update_kb = Just "" } + +deleteArgs :: Mode ShortHndrModes +deleteArgs = mode "delete" initial + "Delete an abbreviation record from the Knowledge Base" + (flagArg (updateMode "") "delete") + [ + flagReq ["k"] (updateMode "delete_kb") "FILENAME" "Knowledgebase filename" + ,flagReq ["abbr"] (updateMode "delete_abbr") "abbreviation" "Abbreviation" + ,flagHelpSimple helpMode + ] + where initial = Kbt $ Del $ Delete { delete_abbr = "", delete_kb = Just "" } + updateMode :: String -> String -> ShortHndrModes -> Either String ShortHndrModes updateMode "input" s (Exp (Re r)) = Right $ Exp $ Re $ r {input = Just s} updateMode "out" s (Exp (Re r)) = Right $ Exp $ Re $ r {out = Just s} updateMode "replace_kb" s (Exp (Re r)) = Right $ Exp $ Re $ r {replace_kb = Just s} -updateMode _ _ e@(Exp (Re _)) = Right e -updateMode _ _ (Exp (Ex _)) = undefined -updateMode _ _ (Kbt _) = undefined -updateMode _ _ Hlp = undefined +updateMode "expand_kb" s (Exp (Ex r)) = Right $ Exp $ Ex $ r { expand_kb = Just s} +updateMode "abbr" s (Exp (Ex r)) = Right $ Exp $ Ex $ r { expand_abbr = s } +updateMode "list_kb" s (Kbt (Lst r)) = Right $ Kbt $ Lst $ r { list_kb = Just s} +updateMode "add_kb" s (Kbt (Ad r)) = Right $ Kbt $ Ad $ r { add_kb = Just s} +updateMode "add_ex" s (Kbt (Ad r)) = Right $ Kbt $ Ad $ r { add_expansion = s} +updateMode "add_abbr" s (Kbt (Ad r)) = Right $ Kbt $ Ad $ r { add_abbr = s} +updateMode "update_kb" s (Kbt (Up r)) = Right $ Kbt $ Up $ r { update_kb = Just s} +updateMode "update_ex" s (Kbt (Up r)) = Right $ Kbt $ Up $ r { update_expansion = s} +updateMode "update_abbr" s (Kbt (Up r)) = Right $ Kbt $ Up $ r { update_abbr = s} +updateMode "delete_kb" s (Kbt (Del r)) = Right $ Kbt $ Del $ r { delete_kb = Just s} +updateMode "delete_abbr" s (Kbt (Del r)) = Right $ Kbt $ Del $ r { delete_abbr = s} +updateMode _ _ e = Right e