Comment detail

起動オプションの解析 (Nested Flatten)

This comment is reply for 7613 M.Suzuki: 少々無理矢理な感じです。。。Parsec...(起動オプションの解析). Go to thread root.

System.Console.GetOpt を使う方法もあります。 ロングオプションにも対応しています。

でもあんまりすっきりとはいかない感じです。:<

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Main where

import System.Console.GetOpt
import System.Environment

main :: IO ()
main = getArgs >>= compilerOpts >>= cmdopt

data Options = Options 
    { optOutput :: Bool
    , optQuote  :: Bool
    , optDebug  :: Int
    }

defaultOptions = Options
    { optOutput = False
    , optQuote  = False
    , optDebug  = 0
    }

options :: [OptDescr (Options -> Options)]
options =
 [ Option ['o'] ["output"]
   (NoArg (\ opts -> opts { optOutput = True }))
   "Output option"
 , Option ['q'] ["quote"]
   (NoArg (\ opts -> opts { optQuote  = True }))
   "Quote option"
 , Option ['d'] ["debug"]
   (ReqArg (\ d opts -> opts { optDebug = read d }) "LEVEL")
   "debug LEVEL"
 ]

compilerOpts :: [String] -> IO (Options, [String])
compilerOpts argv
 = case getOpt Permute options argv of
     (o,n,[])  -> return (foldl (flip id) defaultOptions o, n)
     (_,_,ers) -> ioError (userError (concat ers ++ usageInfo usageHeader options))

usageHeader = "Usage: cmdopt -o [-q] [-d {0|1|2}] STR [STR ...]"

cmdopt :: (Options,[String]) -> IO ()
cmdopt (o,xs@(_:_))
 | optOutput o = putStr $ unlines 
               $ ["[Option Info]"
                 ,"o(output): "++"ON"
                 ,"q(quote) : "++if optQuote o then "ON" else "OFF"
                 ,"d(debug) : "++show (optDebug o)
                 ,"[Parameter Info]"
                 ,show len ++ " parameter"++if len >1 then "s " else " " ++"specified"
                 ] 
               ++ map showParam (zip [1..] xs)
    where len = length xs
cmdopt _       = ioError (userError (usageInfo usageHeader options))

showParam :: (Int,String) -> String
showParam (n,s) = show n ++": "++s

Index

Feed

Other

Link

Pathtraq

loading...