簡體   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