-
Notifications
You must be signed in to change notification settings - Fork 22
/
CmdLineOptions.hs
101 lines (94 loc) · 3.55 KB
/
CmdLineOptions.hs
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
module CmdLineOptions ( Flag(..)
, parseCmdLineOpt
, defPort
, defPollInterval
) where
import System.Console.GetOpt
import System.Environment
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class
import Data.Monoid
-- Command line flag / option parsing, defaults
data Flag = FlagHelp
| FlagTraceLevel String
| FlagTraceFile String
| FlagTraceAppend
| FlagTraceNoEcho
| FlagTraceDisableColor
| FlagPort String
| FlagLocalhost
| FlagTraceHTTP
| FlagPollInterval String
| FlagBridgeIP String
deriving (Eq, Show)
defPort, defPollInterval :: Int
defPort = 8001
defPollInterval = 1
parseCmdLineOpt :: MonadIO m => m [Flag]
parseCmdLineOpt = liftIO $ do
name <- getProgName
args <- getArgs
let header = "Usage: " <> name <> " [OPTION...]"
usage = usageInfo header options
flags <- case getOpt Permute options args of
(f , [], [] ) -> return f
(_ , _ , err) -> do
void . throwIO . userError $ "\n" <> concat err <> usage
return []
when (FlagHelp `elem` flags) $ do
putStrLn usage
throwIO . userError $ "Done, exiting"
return flags
where
options :: [OptDescr Flag]
options = [ Option ['p']
["port"]
(ReqArg FlagPort "PORT")
("network port (default: " <> show defPort <> ")")
, Option []
["localhost"]
(NoArg FlagLocalhost)
"only bind to localhost"
, Option ['i']
["poll-interval"]
(ReqArg FlagPollInterval "SECONDS")
("bridge poll interval (default: " <> show defPollInterval <> ")")
, Option ['b']
["bridge-ip"]
(ReqArg FlagBridgeIP "IP")
"manually specify Hue bridge IP address"
, Option ['t']
["trace-level"]
(ReqArg FlagTraceLevel "LEVEL")
( "execution trace level (default: i)\n"
<> " n = none\n"
<> " e = errors only\n"
<> " w = warnings and errors\n"
<> " i = infos, warnings and errors"
)
, Option []
["trace-file"]
(ReqArg FlagTraceFile "FILE")
"output file for execution trace (default: none)"
, Option []
["trace-no-color"]
(NoArg FlagTraceDisableColor)
"no ANSI colors for trace output"
, Option ['e']
["trace-no-echo"]
(NoArg FlagTraceNoEcho)
"disable echo execution trace to stdout"
, Option []
["trace-append"]
(NoArg FlagTraceAppend)
"append execution trace file instead of overwriting"
, Option []
["trace-http"]
(NoArg FlagTraceHTTP)
"trace web server events"
, Option ['h']
["help"]
(NoArg FlagHelp)
"print usage information"
]