-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflickell.hs
More file actions
92 lines (75 loc) · 2.29 KB
/
flickell.hs
File metadata and controls
92 lines (75 loc) · 2.29 KB
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
-- Command line arguments/flags:
-- https://wiki.haskell.org/Command_line_option_parsers
-- Good GetOpt example
-- https://wiki.haskell.org/High-level_option_handling_with_GetOpt
--import qualified System.Console.GetOpt as O
import System.IO
import Control.Monad
import System.Exit
import System.Environment
import System.Console.GetOpt
data Flag
= Verbose -- no arguments
| Version -- no arguments
| APIKey -- mandatory argument
| APISecret -- mandatory argument
| SetID -- mandatory argument
data Options = Options
{ optVerbose :: Bool
, optInput :: IO String
, optOutput :: String -> IO ()
}
startOptions :: Options
startOptions = Options
{ optVerbose = False
, optInput = getContents
, optOutput = putStr
}
options :: [ OptDescr (Options -> IO Options) ]
options =
[ Option "i" ["input"]
(ReqArg
(\arg opt -> return opt { optInput = readFile arg })
"FILE")
"Input file"
, Option "o" ["output"]
(ReqArg
(\arg opt -> return opt { optOutput = writeFile arg })
"FILE")
"Output file"
, Option "s" ["string"]
(ReqArg
(\arg opt -> return opt { optInput = return arg })
"FILE")
"Input string"
, Option "v" ["verbose"]
(NoArg
(\opt -> return opt { optVerbose = True }))
"Enable verbose messages"
, Option "V" ["version"]
(NoArg
(\_ -> do
hPutStrLn stderr "Version 0.01"
exitWith ExitSuccess))
"Print version"
, Option "h" ["help"]
(NoArg
(\_ -> do
prg <- getProgName
hPutStrLn stderr (usageInfo prg options)
exitWith ExitSuccess))
"Show help"
]
main = do
args <- getArgs
putStrLn (show args)
-- Parse options, getting a list of option actions
let (actions, nonOptions, errors) = getOpt RequireOrder options args
-- Here we thread startOptions through all supplied option actions
opts <- foldl (>>=) (return startOptions) actions
let Options { optVerbose = verbose
, optInput = input
, optOutput = output } = opts
when verbose (hPutStrLn stderr "Hello!")
input >>= output