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.
This commit is contained in:
parent
ef40622a87
commit
b1c403f6b9
3 changed files with 56 additions and 0 deletions
2
scratch/brilliant/.ghci
Normal file
2
scratch/brilliant/.ghci
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
:set prompt "> "
|
||||||
|
:set -Wall
|
45
scratch/brilliant/Main.hs
Normal file
45
scratch/brilliant/Main.hs
Normal file
|
@ -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 '!'
|
9
scratch/brilliant/shell.nix
Normal file
9
scratch/brilliant/shell.nix
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
let
|
||||||
|
pkgs = import /home/wpcarro/nixpkgs {};
|
||||||
|
in pkgs.mkShell {
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
(haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [
|
||||||
|
optparse-applicative
|
||||||
|
]))
|
||||||
|
];
|
||||||
|
}
|
Loading…
Reference in a new issue