繁体   English   中英

如何修复简单的Haskell CLI程序以显示文件内容?

[英]How can I fix my simple Haskell CLI program to show the contents of a file?

因此,我将遍历CmdArgs文档中的示例,并尝试构建Hello World程序,以制作一个简单的CLI程序,该程序以文件名作为输入,并仅显示该文件的内容(例如cat )。 但是我是Haskell的初学者,几乎不知道我在做什么。 这是我到目前为止的内容:

{-# LANGUAGE DeriveDataTypeable #-}
module ShowFile where
import System.Console.CmdArgs

data ShowFile = ShowFile {file :: Maybe FilePath}
              deriving (Show, Data, Typeable)

showFile = ShowFile
  {file = def &= typ "FILE" &= argPos 0}

main = print =<< cmdArgs showFile

因此,运行runhaskell mycat.hs test.txt显示:

ShowFile {file = Just "test.txt"}

所以有些事情在起作用! 现在如何获取它来显示test.txt的内容呢? 我一直在尝试类似的事情:

main = getContents showFile

但还没有偶然发现正确的事情。

选项上的图案匹配。

main = do
    options <- cmdArgs showFile
    contents <- case options of
        ShowFile { file = Nothing } -> getContents
        ShowFile { file = Just f  } -> readFile f
    putStr contents

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM