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