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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
module CPM.Query.Options
( CurryEntity(..), Options(..), defaultOptions, getDefaultOptions
, processOptions, usageText
, printWhenStatus, printWhenIntermediate, printWhenAll
)
where
import Control.Monad ( when, unless )
import Data.Char ( toLower )
import Data.List ( findIndices, isPrefixOf, splitOn )
import Numeric ( readNat )
import System.Console.GetOpt
import System.Process ( exitWith )
import CPM.Query.Configuration
import CPM.Query.RCFile ( readRC, rcValue )
data Options = Options
{ optVerb :: Int
, optHelp :: Bool
, optPackage :: String
, optVersion :: String
, optModule :: String
, optEName :: String
, optEntity :: CurryEntity
, optCLS :: String
, optAll :: Bool
, optColor :: Bool
, optDryRun :: Bool
, optForce :: Int
, optGenerate :: Bool
, optGenFrom :: String
, optCRequests :: [String]
, optTRequests :: [String]
, optORequests :: [String]
, optRequest :: [String]
, optOutFormat :: String
, optShowAll :: Bool
, optRemote :: Bool
, optRemoteURL :: String
}
defaultOptions :: Options
defaultOptions =
Options 1 False "" "" "" "" Operation "" False False False 0 False ""
[] [] [] [] "Text" False True ""
getDefaultOptions :: IO Options
getDefaultOptions = do
rcprops <- readRC
return $
defaultOptions
{ optCRequests = readReqs (rcValue rcprops "classrequests")
, optTRequests = readReqs (rcValue rcprops "typerequests")
, optORequests = readReqs (rcValue rcprops "operationrequests")
, optShowAll = if rcValue rcprops "showall"== "yes" then True else False
, optRemote = if rcValue rcprops "remote" == "yes" then True else False
, optRemoteURL = let rcurl = rcValue rcprops "curryinfourl"
in if null rcurl then curryInfoURL else rcurl
}
where
readReqs s = if null s then [] else splitOn "," s
processOptions :: String -> [String] -> IO (Options,[String])
processOptions banner argv = do
dfltoptions <- getDefaultOptions
let (funopts, args, opterrors) = getOpt Permute options argv
opts = foldl (flip id) dfltoptions funopts
unless (null opterrors)
(putStr (unlines opterrors) >> printUsage >> exitWith 1)
when (optHelp opts) (printUsage >> exitWith 0)
when (not (null (optGenFrom opts)) && not (optGenerate opts))
(putStrLn "Superfluous file with generate data!" >> exitWith 1)
let opts1 =
if optGenerate opts && "--remote" `notElem` argv
then opts { optRemote = False }
else opts
return (opts1, args)
where
printUsage = putStrLn (banner ++ "\n" ++ usageText)
usageText :: String
usageText =
usageInfo ("Usage: cpm-query [options] <module name> <entity name>\n" ++
" cpm-query [options] <module name>\n" ++
" cpm-query [options]\n" ++
" cpm-query [options] --generate <package> <version>\n" ++
" cpm-query [options] --generate <package> <version> <mod>\n")
options
options :: [OptDescr (Options -> Options)]
options =
[ Option "h?" ["help"]
(NoArg (\opts -> opts { optHelp = True }))
"print help and exit"
, Option "q" ["quiet"]
(NoArg (\opts -> opts { optVerb = 0 }))
"run quietly (no output beside info result)"
, Option "v" ["verbosity"]
(OptArg (maybe (checkVerb 2) (safeReadNat checkVerb)) "<n>")
"verbosity level:\n0: quiet (same as `-q')\n1: show status messages (default)\n2: show more details (same as `-v')\n3: show all details"
, Option "p" ["package"]
(ReqArg (\arg opts -> opts { optPackage = arg }) "<pkg>")
"requested package"
, Option "x" ["version"]
(ReqArg (\arg opts -> opts { optVersion = arg }) "<vsn>")
"requested version"
, Option "m" ["module"]
(ReqArg (\arg opts -> opts { optModule = arg }) "<mod>")
"requested module"
, Option "t" ["type"]
(NoArg (\opts -> opts { optEntity = Type }))
"show information about a type"
, Option "c" ["class"]
(NoArg (\opts -> opts { optEntity = Class }))
"show information about a type class"
, Option "o" ["operation"]
(NoArg (\opts -> opts { optEntity = Operation }))
"show information about an operation (default)"
, Option "" ["clskind"]
(ReqArg checkKind "<k>")
"entity kind provided by the Curry language server\n(ValueFunction|TypeData|Class|...)"
, Option "" ["all"]
(NoArg (\opts -> opts { optAll = True }))
"show information of all entities in a module"
, Option "" ["color"]
(NoArg (\opts -> opts { optColor = True }))
"use colors in text output"
, Option "d" ["dry"]
(NoArg (\opts -> opts { optDryRun = True }))
"dry run, i.e., do not run `curry-info` analyses"
, Option "" ["force"]
(NoArg (\opts -> opts { optForce = 1 }))
"force generation of properties"
, Option "" ["generate"]
(NoArg (\opts -> opts { optGenerate = True }))
"generate analysis infos for a package version"
, Option "" ["from"]
(ReqArg (\f opts -> opts { optGenFrom = f }) "<f>")
"file with generate data"
, Option "" ["request"]
(ReqArg (\r opts -> opts { optRequest = optRequest opts ++
splitOn "," r })
"<r>")
"specific request (e.g., definition)\n(separate multiple requests by comma)"
, Option "" ["format"]
(ReqArg checkFormat "<f>")
"output format: Text (default), JSON, CurryTerm"
, Option "" ["showall"]
(NoArg (\opts -> opts { optShowAll = True }))
"show all available information (no generation)"
, Option "" ["local"]
(NoArg (\opts -> opts { optRemote = False }))
"use local installation of 'curry-info'"
, Option "" ["remote"]
(NoArg (\opts -> opts { optRemote = True }))
"use 'curry-info' web service to fetch information\n(default)"
]
where
safeReadNat opttrans s opts = case readNat s of
[(n,"")] -> opttrans n opts
_ -> error "Illegal number argument (try `-h' for help)"
checkVerb n opts = if n >= 0 && n <= 3
then opts { optVerb = n }
else error "Illegal verbosity level (try `-h' for help)"
checkKind k opts
| k == "ValueFunction" = opts' { optEntity = Operation }
| k == "TypeData" = opts' { optEntity = Type }
| k == "TypeAlias" = opts' { optEntity = Type }
| k == "TypeClass" = opts' { optEntity = Class }
| otherwise = opts' { optEntity = Unknown }
where opts' = opts { optCLS = k }
checkFormat f opts =
case findIndices (map toLower f `isPrefixOf`)
(map (map toLower) outputFormats) of
[] -> error "Illegal output format (try `-h' for help)"
[i] -> opts { optOutFormat = outputFormats !! i }
_ -> error "Output format ambiguous (try `-h' for help)"
outputFormats :: [String]
outputFormats = ["Text", "JSON", "CurryTerm"]
printWhenStatus :: Options -> String -> IO ()
printWhenStatus opts s = when (optVerb opts > 0) (putStrLn s)
printWhenIntermediate :: Options -> String -> IO ()
printWhenIntermediate opts s =
when (optVerb opts > 1) (putStrLn s)
printWhenAll :: Options -> String -> IO ()
printWhenAll opts s =
when (optVerb opts > 2) (putStrLn s)
|