2015-10-18 36 views
0

這個問題值排序參數是主題的延伸:Ordered arguments with cmdargs在cmdargs

我設法把相同類型的參數列表。但是,諾貝爾,我想有一個值的選項列表。像這樣:

runhaskell ~/testargs.hs -a 5,6 -b 8,9 -c 4,2 -a 9,3 

我試着用

data Act = 
    ActA (Double,Double) 
    | ActB (Double,Double) 
    | ActC (Double,Double) 
    deriving (Show, Typeable, Data, Eq) 

宣佈我的數據,但我得到以下錯誤:

Couldn't match expected type `Act' 
    with actual type `(Double, Double) -> Act' 
In the expression: ActA 

是否有可能檢索參數與值的列表?

+1

該錯誤是由於表情,但是你有沒有向我們展示了在其中使用表達式的上下文,所以這是不可能的,以確定哪些你做錯了。 – user2407038

回答

1

您可能將不得不使用較低級別的Explicit api來執行此操作。

這裏是一個展示如何使用顯式的API的例子:

  • 手柄--help
  • 收集多個-w ...選項
  • 驗證標誌的參數
  • 收集所有未命名的參數

個使用示例:

prog --help 
prog -i=456 
prog -w this -w is -w a -w test 
prog -i=xyz 

計劃:

import System.Console.CmdArgs.Explicit 
import Data.Char 

data MyOptions = Opts { _help :: Bool 
         , _words :: [String] 
         , _opt1 :: String 
         , _opt2 :: Int 
         , _unnamed :: [ String ] 
         } 
    deriving (Show) 

myMode :: Mode MyOptions 
myMode = mode "programName" initialOpts description unnamedArg convertFlags 
    where 
    initialOpts = Opts False [] "abc" 3 [] 
    description = "This program does it all." 

    unnamedArg = Arg { argValue = updateUnnamed, argType = "<FILE>", argRequire = False } 
     where updateUnnamed str opts = Right $ opts { _unnamed = (str : _unnamed opts) } 

    updateWord str opts = Right $ opts { _words = (str: _words opts) } 
    updateA str opts = Right $ opts { _opt1 = str } 
    updateNum str opts 
     | not (null str) && all isDigit str = Right $ opts { _opt2 = read str } 
     | otherwise       = Left $ "-i option is not a number: " ++ str 

    convertFlags = 
     [ flagReq  ["w","word"] updateWord "<WORD>" "add a word" 
     , flagReq  ["a", "bc"] updateA "<STRING>" "a string option" 
     , flagOpt "0" ["i" ]  updateNum "<NUMBER>" "a numeric option" 
     , flagHelpSimple (\opts -> opts { _help = True }) 
     ] 

main = do 
    opts <- processArgs myMode 
    print opts 
    if _help opts then 
     print $ helpText [] HelpFormatDefault myMode 
    else 
     print opts