about summary refs log tree commit diff
path: root/scratch/brilliant/Main.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-08-04T15·36+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-08-04T15·36+0100
commitb1c403f6b913d0ac416efffba8e563ab6a24fa9b (patch)
treebe94457b46c00018f93b3ece1a941efbeb726891 /scratch/brilliant/Main.hs
parentef40622a87b9dad5781d0214b4f68b450b04b100 (diff)
Create small command line program that parses arguments
Before starting my take-home assignment, the instructions advised me to create a
"Hello, world" program in the language of my choice. Since I'm choosing Haskell,
I created this example as my starter boilerplate.
Diffstat (limited to 'scratch/brilliant/Main.hs')
-rw-r--r--scratch/brilliant/Main.hs45
1 files changed, 45 insertions, 0 deletions
diff --git a/scratch/brilliant/Main.hs b/scratch/brilliant/Main.hs
new file mode 100644
index 000000000000..999771f55836
--- /dev/null
+++ b/scratch/brilliant/Main.hs
@@ -0,0 +1,45 @@
+{-# LANGUAGE RecordWildCards #-}
+--------------------------------------------------------------------------------
+module Main where
+--------------------------------------------------------------------------------
+import Options.Applicative
+import Data.Semigroup ((<>))
+
+import qualified System.Environment as Env
+--------------------------------------------------------------------------------
+
+data CommandArgs = CommandArgs
+  { hello :: String
+  , quiet :: Bool
+  , enthusiasm :: Int
+  } deriving (Eq, Show)
+
+parseArgs :: Parser CommandArgs
+parseArgs =
+  CommandArgs <$> strOption
+                  ( long "hello"
+                 <> metavar "TARGET"
+                 <> help "Target for the greeting" )
+              <*> switch
+                  ( long "quiet"
+                 <> short 'q'
+                 <> help "Whether to be quiet" )
+              <*> option auto
+                  ( long "enthusiasm"
+                 <> help "How enthusiastic to greet"
+                 <> showDefault
+                 <> value 1
+                 <> metavar "INT" )
+
+main :: IO ()
+main = do
+  args <- execParser opts
+  greet args
+  where
+    opts = info (parseArgs <**> helper)
+      ( fullDesc
+     <> progDesc "Print a greeting for TARGET"
+     <> header "header - a test for optparse-applicative" )
+
+greet :: CommandArgs -> IO ()
+greet CommandArgs{..} = putStrLn $ "Hello, " ++ hello ++ replicate enthusiasm '!'