-
Notifications
You must be signed in to change notification settings - Fork 2
/
Options.lhs
141 lines (126 loc) · 5.3 KB
/
Options.lhs
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
\lstset{firstnumber=1}
\subsection{Options}\label{Options}
The configuration for the driver is via
\link{http://www.haskell.org/haskellwiki/High-level\_option\_handling\_with\_GetOpt}{\texttt{GetOpt}}.
The primary options are:
\begin{enumerate}
\item \texttt{optLanguage} --- generated output language. Defaults to \emph{Typescript}.
\item \texttt{optIPC} --- IPC mechanism. Defaults to \emph{FreedomMessaging}.
\item \texttt{optInput} --- input, by default \texttt{stdin}.
\item \texttt{optPrefix} --- prefix for generated filenames. Defaults to \texttt{input}.
\item \texttt{optOutputDir} --- output directory. Defaults to current directory.
\item \texttt{optMakeOutputDir} --- whether to create the output
directory, or just fail if it doesn't exist. Defaults to false.
\item \texttt{optRestArgCount} --- when passing \emph{...rest} to an
API, how many arguments to present to IPC mechanisms that can't
handle variable numbers of arguments. Defaults to 10.
\item \texttt{optVerbose} --- verbosity of output. Default is 1, with
higher numbers being more verbose. 0 is the 'STFU' option.
\item \texttt{optMerge} --- merge changes in existing artifacts.
Defaults to False. Only useful for \texttt{.json} files.
\end{enumerate}
\begin{code}
module Options (Options(..), getOptions) where
import System.Console.GetOpt
import System.Directory
import System.Environment
import System.FilePath.Posix
import System.IO
import System.Exit
import Data.Either.Unwrap
import Generate.Types as T
data Options = Options { optLanguage :: T.Language
, optIPC :: T.IPCMechanism
, optInput :: IO String
, optPrefix :: String
, optOutputDir :: IO FilePath
, optMakeOutputDir :: Bool
, optRestArgCount :: Int
, optVerbose :: Int
, optMerge :: Bool
}
startOptions = Options { optLanguage = T.Typescript
, optIPC = T.FreedomMessaging
, optInput = return ""
, optPrefix = "input"
, optOutputDir = getCurrentDirectory
, optMakeOutputDir = False
, optRestArgCount = 10
, optVerbose = 1
, optMerge = False
}
options :: [ OptDescr (Options -> IO Options) ]
options =
[ Option "i" ["input"]
-- as a convenience, pull the filename and make it the prefix.
(ReqArg
(\arg opt -> do let prefix = takeBaseName arg
return $ opt { optInput = readFile arg, optPrefix = prefix })
"FILE")
"Input file, also setting the prefix to its directory."
, Option "d" ["outputdir"]
(ReqArg
(\arg opt -> return opt { optOutputDir = (return arg) })
"DIR")
"Output directory"
, Option "l" ["language"]
(ReqArg
(\arg opt -> do let lang = parseLang arg
if isLeft lang
then return opt { optLanguage = (fromRight lang) }
else do hPutStrLn stderr $ fromLeft lang
exitWith $ ExitFailure 1)
"LANG")
"Enable verbose messages"
, Option "c" ["ipc"]
(ReqArg
(\arg opt -> do let ipc = parseIPC arg
if isRight ipc
then return opt { optIPC = (fromRight ipc) }
else do hPutStrLn stderr $ fromLeft ipc
exitWith $ ExitFailure 1)
"IPC")
"IPC Mechanism to Use in Generated Code"
, Option "m" ["merge"]
(NoArg
(\opt -> return opt { optMerge = True }))
"Merge changes into existing artifacts"
, Option "p" ["prefix"]
(ReqArg
(\arg opt -> return opt { optPrefix = arg })
"PREFIX")
"Prefix for generated files."
, Option "V" ["version"]
(NoArg
(\_ -> do hPutStrLn stderr "Version 0.01"
exitWith ExitSuccess))
"Print version"
, Option "v" ["verbose"]
(OptArg (\n opt ->
case n of
Just s -> return opt { optVerbose = (read s) :: Int }
Nothing -> return opt { optVerbose = 2 })
"Verbosity")
"Output verbosity"
, Option "h" ["help"]
(NoArg
(\_ -> do prg <- getProgName
hPutStrLn stderr (usageInfo prg options)
exitWith ExitSuccess))
"Show help"
, Option "r" ["restargs"]
(ReqArg
(\arg opt -> return opt { optRestArgCount = read arg })
"NUMARGS")
"Number of arguments to expand ...rest args in signatures." ]
-- |Given the arguments to the command line, returns a filled Options and the
-- remaining arguments.
getOptions :: [String] -> IO (Options, [String])
getOptions args = do
let (actions, nonOptions, errors) = getOpt RequireOrder options args
if length errors > 0
then do mapM_ (hPutStrLn stderr) errors
exitWith $ ExitFailure 1
else do opts <- foldl (>>=) (return startOptions) actions
return (opts, nonOptions)
\end{code}