使用getOpt解析Haskell中的命令行参数 [英] Parsing command line arguments in Haskell using getOpt
问题描述
我正试图自学Haskell.作为示例程序,我正在编写蜘蛛纸牌播放器.
我正在尝试使用 System.Console.GetOpt
编写命令行解析器.我知道可以为该程序进行参数解析的方法更简单,但是我想学习如何使用 GetOpt
模块,因为我预计稍后将在我将要编写的其他程序中使用它.>
我正在尝试添加一个"--help"选项,该选项仅显示用法消息,然后退出.如果"--games"选项或"--suits"选项的任何一个参数都不是有效的整数(游戏> = 1和< = 1000,suits == 1,2,我也想打印使用情况消息),或4).我将把生成的 Options
数据类型传递给程序的其他部分.
我也收到错误消息,指出 progName
不在范围内.是不是 do
块范围内的 parseArgs
中的 case
语句?
这是我的代码,从真实世界Haskell" 和 Haskell Wiki :
模块Main(主),其中导入System.Console.GetOpt导入System.Environment(getArgs,getProgName)数据选项=选项{optGames :: Int,optSuits ::整数,optVerbose ::布尔派生显示defaultOptions =选项{optGames = 1,optSuits = 4,optVerbose = False}选项:: [OptDescr(选项->选项)]选项=[选项['g'] [游戏"](ReqArg(\ g opts-> opts {optGames =(read g)})"GAMES")游戏数",选项['s'] [西装"](ReqArg(\ s opts-> opts {optSuits =(read s)})"SUITS")西装数量",选项['v'] [详细"](NoArg(\ opts-> opts {optVerbose = True}))详细输出"]parseArgs :: IO选项parseArgs =做argv<-getArgsprogName<-getProgName的情况下的getOpt RequireOrder选项argv(opts,[],[])->return(foldl(flip id)defaultOptions opts)(_,_,错误)->ioError(userError(concat errs ++ helpMessage))在哪里header =用法:" ++ progName ++"[OPTION ...]"helpMessage = UsageInfo标头选项main :: IO()主=做选项<-parseArgsputStrLn $显示选项
这是我想出的解决方案:
模块Main(主),其中导入Control.Monad导入Control.Monad.Error导入System.Console.GetOpt导入System.Environment(getArgs,getProgName)数据选项=选项{optGames :: Int,optSuits ::整数,optVerbose ::布尔派生显示defaultOptions =选项{optGames = 1,optSuits = 4,optVerbose = False}选项:: [OptDescr(选项->任一字符串选项)]选项=[选项['g'] [游戏"](ReqArg(\ g选择->大小写为g[(游戏,")] |游戏> = 1&&游戏< = 1000->正确的选择{optGames =游戏}_->左侧为-游戏必须为1到1000之间的数字")游戏")游戏数",选项['s'] [西装"](ReqArg(\ s opts->例读的[(suits,")] |适合`elem` [1、2、4]->正确的选择{optSuits =西服}_->左西装必须是1、2或4") 西装")西装数量",选项['v'] [详细"](NoArg(\ opts->右选项{optVerbose = True}))详细输出"]parseArgs :: IO选项parseArgs =做argv<-getArgsprogName<-getProgNamelet标头=用法:" ++ progName ++"[OPTION ...]"让helpMessage = usageInfo标头选项的情况下的getOpt RequireOrder选项argv(opts,[],[])->case foldM(flip id)defaultOptions选择正确选择->退货左错误消息->ioError(userError(errorMessage ++"\ n" ++ helpMessage)(_,_,错误)->ioError(userError(concat errs ++ helpMessage))main :: IO()主=做选项<-parseArgsputStrLn $显示选项
我该如何改善呢?
I am trying to teach myself Haskell. As a sample program, I am writing a Spider solitaire player.
I am trying to write a command line parser using System.Console.GetOpt
. I know there are easier ways to do argument parsing for this program, but I want to learn how to use the GetOpt
module because I anticipate needing its sophistication later in other programs that I will be writing.
I am trying to add a "--help" option that just prints a usage message and then exits. I would also like to print usage messages if either of the arguments to the "--games" option or the "--suits" option are not valid integers (games >= 1 and <= 1000, suits == 1, 2, or 4). I will be passing the resulting Options
data type to other parts of my program.
I am also getting an error that progName
is not in scope. Isn't the case
statement in parseArgs
in the scope of the do
block?
Here is my code, patched together from the examples in "Real World Haskell" and the Haskell wiki:
module Main (main) where
import System.Console.GetOpt
import System.Environment(getArgs, getProgName)
data Options = Options {
optGames :: Int
, optSuits :: Int
, optVerbose :: Bool
} deriving Show
defaultOptions = Options {
optGames = 1
, optSuits = 4
, optVerbose = False
}
options :: [OptDescr (Options -> Options)]
options =
[ Option ['g'] ["games"]
(ReqArg (\g opts -> opts { optGames = (read g) }) "GAMES")
"number of games"
, Option ['s'] ["suits"]
(ReqArg (\s opts -> opts { optSuits = (read s) }) "SUITS")
"number of suits"
, Option ['v'] ["verbose"]
(NoArg (\opts -> opts { optVerbose = True }))
"verbose output"
]
parseArgs :: IO Options
parseArgs = do
argv <- getArgs
progName <- getProgName
case getOpt RequireOrder options argv of
(opts, [], []) -> return (foldl (flip id) defaultOptions opts)
(_, _, errs) -> ioError (userError (concat errs ++ helpMessage))
where
header = "Usage: " ++ progName ++ " [OPTION...]"
helpMessage = usageInfo header options
main :: IO ()
main = do
options <- parseArgs
putStrLn $ show options
Here is the solution that I came up with:
module Main (main) where
import Control.Monad
import Control.Monad.Error
import System.Console.GetOpt
import System.Environment(getArgs, getProgName)
data Options = Options {
optGames :: Int
, optSuits :: Int
, optVerbose :: Bool
} deriving Show
defaultOptions = Options {
optGames = 1
, optSuits = 4
, optVerbose = False
}
options :: [OptDescr (Options -> Either String Options)]
options =
[ Option ['g'] ["games"]
(ReqArg (\g opts ->
case reads g of
[(games, "")] | games >= 1 && games <= 1000 -> Right opts { optGames = games }
_ -> Left "--games must be a number between 1 and 1000"
) "GAMES")
"number of games"
, Option ['s'] ["suits"]
(ReqArg (\s opts ->
case reads s of
[(suits, "")] | suits `elem` [1, 2, 4] -> Right opts { optSuits = suits }
_ -> Left "--suits must be 1, 2, or 4"
) "SUITS")
"number of suits"
, Option ['v'] ["verbose"]
(NoArg (\opts -> Right opts { optVerbose = True }))
"verbose output"
]
parseArgs :: IO Options
parseArgs = do
argv <- getArgs
progName <- getProgName
let header = "Usage: " ++ progName ++ " [OPTION...]"
let helpMessage = usageInfo header options
case getOpt RequireOrder options argv of
(opts, [], []) ->
case foldM (flip id) defaultOptions opts of
Right opts -> return opts
Left errorMessage -> ioError (userError (errorMessage ++ "\n" ++ helpMessage))
(_, _, errs) -> ioError (userError (concat errs ++ helpMessage))
main :: IO ()
main = do
options <- parseArgs
putStrLn $ show options
How can I improve this?
这篇关于使用getOpt解析Haskell中的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!