From 737bf192f4b74104b7e8f78ed9732e70aa44a5da Mon Sep 17 00:00:00 2001 From: Fabian Kirchner Date: Mon, 2 Oct 2023 15:03:09 +0200 Subject: add command line argument parsing --- anissue.cabal | 3 ++- app/Main.hs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/anissue.cabal b/anissue.cabal index c9cf518..39aed05 100644 --- a/anissue.cabal +++ b/anissue.cabal @@ -71,7 +71,8 @@ executable anissue -- other-extensions: -- Other library packages from which modules are imported. - build-depends: base ^>=4.16.4.0 + build-depends: base ^>=4.16.4.0, + optparse-applicative -- Directories containing source files. hs-source-dirs: app diff --git a/app/Main.hs b/app/Main.hs index 65ae4a0..e3c1a28 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,4 +1,39 @@ module Main where +import Options.Applicative ((<**>)) +import Options.Applicative qualified as O + +data Options = Options + { optCommand :: Command + } + deriving (Show) + +data Command + = List + | Show + deriving (Show) + +optionsParser :: O.Parser Options +optionsParser = + Options + <$> commandParser + +commandParser :: O.Parser Command +commandParser = + O.subparser + ( O.command "list" (O.info listCommandParser (O.progDesc "List all issues")) + <> O.command "show" (O.info showCommandParser (O.progDesc "Show details of all issues")) + ) + +listCommandParser :: O.Parser Command +listCommandParser = + pure List + +showCommandParser :: O.Parser Command +showCommandParser = + pure Show + main :: IO () -main = putStrLn "Hello, Haskell!" +main = do + options <- O.execParser (O.info (commandParser <**> O.helper) O.idm) + print options -- cgit v1.2.3