]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/CommandLine.hs
06c072e8715af292cb8a5d83ffa5ed0cd29b2aa4
[dead/halcyon.git] / src / CommandLine.hs
1 module CommandLine (
2 apply_args,
3 show_help
4 )
5 where
6
7 import System.Console.CmdArgs
8 import System.Console.CmdArgs.Explicit (process)
9 import System.Environment (getArgs, withArgs)
10 import System.Exit (ExitCode(..), exitWith)
11 import System.IO (hPutStrLn, stderr)
12
13 -- Get the version from Cabal.
14 import Paths_halcyon (version)
15 import Data.Version (showVersion)
16
17 import ExitCodes
18 import OptionalConfiguration
19
20 description :: String
21 description =
22 "Monitor a list of Twitter accounts and display or email any new tweets."
23
24 program_name :: String
25 program_name = "halcyon"
26
27 my_summary :: String
28 my_summary = program_name ++ "-" ++ (showVersion version)
29
30 consumer_key_help :: String
31 consumer_key_help = "Your Twitter API consumer key"
32
33 consumer_secret_help :: String
34 consumer_secret_help = "Your Twitter API consumer secret"
35
36 access_token_help :: String
37 access_token_help = "Your Twitter API access token"
38
39 access_secret_help :: String
40 access_secret_help = "Your Twitter API access secret"
41
42 heartbeat_help :: String
43 heartbeat_help = "How many seconds to wait between polling"
44
45 to_address_help :: String
46 to_address_help = "Send tweets to ADDRESS"
47
48 from_address_help :: String
49 from_address_help = "Send tweets from ADDRESS"
50
51 sendmail_path_help :: String
52 sendmail_path_help = "Use PATH to send mail"
53
54 ignore_replies_help :: String
55 ignore_replies_help = "Ignore replies to other tweets"
56
57 ignore_retweets_help :: String
58 ignore_retweets_help = "Ignore retweets from other users"
59
60 verbose_help :: String
61 verbose_help = "Be verbose about stuff"
62
63 arg_spec :: Mode (CmdArgs OptionalCfg)
64 arg_spec =
65 cmdArgsMode $
66 OptionalCfg {
67 consumer_key =
68 def &= typ "KEY"
69 &= groupname "Twitter API"
70 &= help consumer_key_help,
71
72 consumer_secret =
73 def &= typ "SECRET"
74 &= groupname "Twitter API"
75 &= help consumer_secret_help,
76
77 access_token =
78 def &= typ "TOKEN"
79 &= groupname "Twitter API"
80 &= help access_token_help,
81
82 access_secret =
83 def &= typ "SECRET"
84 &= groupname "Twitter API"
85 &= help access_secret_help,
86
87 heartbeat =
88 def &= groupname "Miscellaneous"
89 &= help heartbeat_help,
90
91 ignore_replies =
92 def &= groupname "Miscellaneous"
93 &= help ignore_replies_help,
94
95 ignore_retweets =
96 def &= groupname "Miscellaneous"
97 &= help ignore_retweets_help,
98
99 verbose =
100 def &= groupname "Miscellaneous"
101 &= help verbose_help,
102
103 sendmail_path =
104 def &= typ "PATH"
105 &= groupname "Mail Options"
106 &= help sendmail_path_help,
107
108 from_address =
109 def &= typ "ADDRESS"
110 &= groupname "Mail Options"
111 &= help from_address_help,
112
113 to_address =
114 def &= typ "ADDRESS"
115 &= groupname "Mail Options"
116 &= help to_address_help,
117
118 usernames =
119 def &= args
120 &= typ "USERNAMES" }
121
122 &= program program_name
123 &= summary my_summary
124 &= details [description]
125 &= helpArg [groupname "Common flags"]
126 &= versionArg [groupname "Common flags"]
127
128 show_help :: IO OptionalCfg
129 show_help = withArgs ["--help"] parse_args >>= cmdArgsApply
130
131
132
133 parse_args :: IO (CmdArgs OptionalCfg)
134 parse_args = do
135 x <- getArgs
136 let y = process arg_spec x
137 case y of
138 Right result -> return result
139 Left err -> do
140 hPutStrLn stderr err
141 exitWith (ExitFailure exit_args_parse_failed)
142
143
144 -- | Really get the command-line arguments. This calls 'parse_args'
145 -- first to replace the default "wrong number of arguments" error,
146 -- and then runs 'cmdArgsApply' on the result to do what the
147 -- 'cmdArgs' function usually does.
148 apply_args :: IO OptionalCfg
149 apply_args =
150 parse_args >>= cmdArgsApply