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
|
module CPM.Query.Options
( CurryEntity(..), Options(..), defaultOptions, processOptions
, usageText
, printWhenStatus, printWhenIntermediate, printWhenAll
)
where
import Control.Monad ( when, unless )
import Data.Char ( toLower )
import Data.List ( findIndices, isPrefixOf )
import Numeric ( readNat )
import System.Console.GetOpt
import System.Process ( exitWith )
data CurryEntity = Operation | Type | TypeClass | Unknown
deriving (Eq, Show)
data Options = Options
{ optVerb :: Int
, optHelp :: Bool
, optEntity :: CurryEntity
, optCLS :: String
, optDryRun :: Bool
, optForce :: Bool
, optGenerate :: Bool
, optRequest :: [String]
, optOutFormat :: String
}
defaultOptions :: Options
defaultOptions = Options 1 False Operation "" False False False [] "Text"
processOptions :: String -> [String] -> IO (Options,[String])
processOptions banner argv = do
let (funopts, args, opterrors) = getOpt Permute options argv
opts = foldl (flip id) defaultOptions funopts
unless (null opterrors)
(putStr (unlines opterrors) >> printUsage >> exitWith 1)
when (optHelp opts) (printUsage >> exitWith 0)
return (opts, args)
where
printUsage = putStrLn (banner ++ "\n" ++ usageText)
usageText :: String
usageText =
usageInfo ("Usage: cpm-query [options] <module names> <entity name>\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 "f" ["function"]
(NoArg (\opts -> opts { optEntity = Operation }))
"show information about an operation (default)"
, Option "t" ["type"]
(NoArg (\opts -> opts { optEntity = Type }))
"show information about a type"
, Option "c" ["typeclass"]
(NoArg (\opts -> opts { optEntity = TypeClass }))
"show information about a type class"
, Option "" ["clskind"]
(ReqArg checkKind "<k>")
"entity kind provided by the Curry language server\n(ValueFunction|TypeData|TypeClass|...)"
, Option "" ["force"]
(NoArg (\opts -> opts { optForce = True }))
"force computation of properties"
, Option "d" ["dry"]
(NoArg (\opts -> opts { optDryRun = True }))
"dry run, i.e., do not run `curry-info` analyses"
, Option "" ["generate"]
(NoArg (\opts -> opts { optGenerate = True }))
"generate analysis infos for a package version"
, Option "" ["request"]
(ReqArg (\r opts -> opts { optRequest = optRequest opts ++ [r] })
"<r>")
"specific request (e.g., definition)\n(multiple options allowed)"
, Option "" ["format"]
(ReqArg checkFormat "<f>")
"output format: Text (default), JSON, CurryTerm"
]
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 = TypeClass }
| 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)
|