Created
March 1, 2019 12:36
-
-
Save gdeest/3c8c9cf01998c00443bf4bd7d99f887c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import System.Environment | |
import System.Exit | |
type Filename = String | |
data Command = Foo Filename | Bar | |
parseCommand :: [String] -> Maybe Command | |
parseCommand ["--foo", file] = Just (Foo file) | |
parseCommand ["--bar"] = Just Bar | |
parseCommand _ = Nothing | |
printUsage :: IO () | |
printUsage = putStrLn ( | |
unlines ([ | |
"Usage: ", | |
"myProgram [--foo file | --bar]" | |
]) | |
) | |
runCommand :: Command -> IO () | |
runCommand (Foo file) = | |
putStrLn ("Running Foo on: " ++ file) | |
runCommand Bar = | |
putStrLn "Running Bar" | |
main :: IO () | |
main = do | |
args <- getArgs | |
let mbCmd = parseCommand args | |
case mbCmd of | |
Just cmd -> runCommand cmd | |
Nothing -> do | |
printUsage | |
exitWith (ExitFailure 84) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where { | |
import System.Environment; | |
import System.Exit; | |
type Filename = String; | |
data Command = Foo Filename | Bar; | |
parseCommand :: [String] -> Maybe Command; | |
parseCommand ["--foo", file] = Just (Foo file); | |
parseCommand ["--bar"] = Just Bar; | |
parseCommand _ = Nothing; | |
printUsage :: IO (); | |
printUsage = putStrLn ( | |
unlines ([ | |
"Usage: ", | |
"myProgram [--foo file | --bar]" | |
]) | |
); | |
runCommand :: Command -> IO (); | |
runCommand (Foo file) = | |
putStrLn ("Running Foo on: " ++ file); | |
runCommand Bar = | |
putStrLn "Running Bar"; | |
main :: IO (); | |
main = do { | |
args <- getArgs; | |
let { | |
mbCmd = parseCommand args; | |
}; | |
case mbCmd of { | |
Just cmd -> runCommand cmd; | |
Nothing -> do { | |
printUsage; | |
exitWith (ExitFailure 84); | |
}; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment