Skip to content

Instantly share code, notes, and snippets.

@gdeest
Created March 1, 2019 12:45
Show Gist options
  • Save gdeest/64f6a51d7c938d6092223ddc375095cc to your computer and use it in GitHub Desktop.
Save gdeest/64f6a51d7c938d6092223ddc375095cc 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 usageLines)
where usageLines = [
"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 usageLines)
where {
usageLines = [
"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