-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptions.hs
79 lines (66 loc) · 2.21 KB
/
Options.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
module Options
( Options( .. )
, defaultOptions
, optionDescriptions
)
where
import System.Console.GetOpt
import System.Exit
import Control.Monad
import System.IO
import Data.Char
import Data.Word
import Data.List
import Safe
import Debug.Trace
version :: String
version = "0.1.0"
data Options = Options { address :: Word32
, bit :: Word8
, value :: Bool
}
-- The defaults for the parameters
defaultOptions :: Options
defaultOptions = Options { address = 0xEE0
, bit = 2
, value = False
}
optionDescriptions :: [ OptDescr ( Options ->IO Options ) ]
optionDescriptions =
[ Option "a" [ "address" ]
( ReqArg readAddress "" )
"The address to read or write to"
, Option "b" [ "bit" ]
( ReqArg readBit "" )
"The bit to write"
, Option "v" [ "value" ]
( ReqArg readValue "" )
"The value to write"
, Option "V" [ "version" ]
( NoArg ( \_ -> ( putStrLn $ "This is portio v" ++ version ) >> exitWith ExitSuccess ) )
"Print version information"
, Option "h" [ "help" ]
( NoArg ( \_ -> ( putStrLn $ usageInfo "portio -a <address> -b <bit number> -v <on|off>" optionDescriptions ) >> exitWith ExitSuccess ) )
"Print this usage information"
]
readAddress :: String -> Options -> IO Options
readAddress arg opts =
case readMay arg of
Just addr -> trace ( "IO Port address: " ++ ( show addr ) ) return $ opts { address = addr }
Nothing -> do
putStrLn $ "Invalid address arg: " ++ arg
exitWith $ ExitFailure (-1)
readBit :: String -> Options -> IO Options
readBit arg opts =
case readMay arg of
Just b -> return $ opts { bit = b }
Nothing -> do
putStrLn $ "Invalid address arg: " ++ arg
exitWith $ ExitFailure (-1)
readValue :: String -> Options -> IO Options
readValue arg opts
| arg == "on" = return $ opts { value = True }
| arg == "high" = return $ opts { value = True }
| arg == "yes" = return $ opts { value = True }
| arg == "true" = return $ opts { value = True }
| otherwise = return $ opts { value = False }