Skip to content

Instantly share code, notes, and snippets.

@gdeest
Created March 1, 2019 12:36
Show Gist options
  • Save gdeest/3c8c9cf01998c00443bf4bd7d99f887c to your computer and use it in GitHub Desktop.
Save gdeest/3c8c9cf01998c00443bf4bd7d99f887c to your computer and use it in GitHub Desktop.
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)
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