我目前正在做一个需要解析命令行参数的项目。到目前为止,我一直在遵循this tutorial,这非常有帮助,但是我无法弄清如何在参数中返回一个变量(--author=example)。我也不知道为什么
以下是我的代码:
parse [] = getContents
会导致错误(我必须取消注释它)。以下是我的代码:
module Main where
import qualified System.Environment as SE
import qualified System.Exit as E
import qualified Lib as Lib
main = do
args <- SE.getArgs
rem <- parse args
Lib.someFunc
putStrLn rem
putStrLn "Hello"
tac = unlines . reverse . lines
parse ["--help"] = usage >> exit
parse ["--version"] = version >> exit
parse ["--author=xyz"] = return "xyz"
-- parse ["--author=?"] = ?
{-
this is the code I am trying to figure out... how do I get parse the passed in variable name?
-}
-- parse [] = getContents
{-
the above line generates this error when I run 'main' in GHCi:
*Main> <stdin>: hIsEOF: illegal operation (handle is semi-closed)
Process intero exited abnormally with code 1
-}
parse fs = concat `fmap` mapM readFile fs
usage = putStrLn "Usage: gc2"
version = putStrLn "gc2 -- git-cal in Haskell2010 - 0.1"
exit = E.exitWith E.ExitSuccess
die = E.exitWith (E.ExitFailure 1)
getContents
获取所有输入,并要求在此之后不要从输入中读取任何内容。您是否满足该约定?例如,请检查Lib.someFunc
。 - chi