NEW: Basic command system

This commit is contained in:
Jean-Claude 2021-06-21 23:43:23 +02:00
commit d64be75602
Signed by: jeanclaude
GPG Key ID: 8A300F57CBB9F63E
5 changed files with 127 additions and 0 deletions

79
Command.hs Normal file
View File

@ -0,0 +1,79 @@
module Command where
import Data.Semigroup ((<>))
import Options.Applicative
--- General
data Options = Options
{ optGlobalFlag :: Bool
, optCommand :: Command
} deriving (Eq, Show)
data Command
= Import ImportOptions
| Test
| NotImplemented
deriving (Eq, Show)
--- Import
data ImportOptions
= New String
| Activate String
| List Bool
deriving (Eq, Show)
importCommand :: Mod CommandFields Command
importCommand =
command "import" (info importParser (progDesc "Import to archive"))
importParser :: Parser Command
importParser =
Import <$>
( New
<$> strOption
( long "new"
<> short 'n'
<> metavar "NAME"
<> help "Name of the new import"
)
<|> Activate
<$> strOption
( long "activate"
<> short 'a'
<> metavar "IMPORT"
<> help "Name of the import to activate"
)
<|> List
<$> switch
( long "list"
<> short 'l'
<> help "List all imports"
)
)
--- Test
--data TestOptions = TestOptions
testCommand :: Mod CommandFields Command
testCommand =
command "test" (info (pure NotImplemented) (progDesc "Test something"))
--testParser :: Parser TestOptions
--testParser =
-- TestOptions
--- Parser
parseOptions :: Parser Options
parseOptions =
Options
<$> switch (long "global-flag" <> help "Set a global flag")
<*> hsubparser (importCommand <> testCommand)
mainParser :: ParserInfo Options
mainParser =
info
(helper <*> parseOptions)
(fullDesc
<> progDesc "myProg Description"
<> header "myProg Header")

9
Command/Import.hs Normal file
View File

@ -0,0 +1,9 @@
module Command.Import where
import Command (ImportOptions)
runImport :: ImportOptions -> IO ()
runImport opts = do
putStrLn "runImport"
return ()

6
Command/Test.hs Normal file
View File

@ -0,0 +1,6 @@
module Command.Test where
runTestCmd :: IO ()
runTestCmd = do
putStrLn "runTestCmd"
return ()

16
Main.hs Normal file
View File

@ -0,0 +1,16 @@
import Data.Semigroup ((<>))
import Options.Applicative
import Command
import Command.Import (runImport)
import Command.Test
main :: IO ()
main = do
options <- execParser mainParser
case optCommand options of
(Import opts) -> runImport opts
Test -> putStrLn "Test Stuff!"
putStrLn ("global flag: " ++ show (optGlobalFlag options))

17
Makefile Normal file
View File

@ -0,0 +1,17 @@
COMPILER = ghc -Wall -dynamic --make
DIR = src/
MAIN = main
MAIN_CAP = Main
IMPORT = Import
all: target clean
target:
$(COMPILER) $(MAIN_CAP).hs -o $(MAIN)
clean:
find . -name "*.o" -delete -o -name "*.hi" -delete
start:
./$(MAIN)