-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUI.hs
223 lines (187 loc) · 5.87 KB
/
UI.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{-|
This module describes the attributes that can be specified on flags and modes.
Many attributes have examples specified on the following data type:
> data Sample = Sample {hello :: String}
-}
module System.Console.CmdArgs.Implicit.UI where
import System.Console.CmdArgs.Implicit.Ann
import Data.Typeable
-- | Flag: \"I want users to be able to omit the value associated with this flag.\"
--
-- Make the value of a flag optional. If @--flag@ is given, it will
-- be treated as @--flag=/this_argument/@.
--
-- > {hello = def &= opt "foo"}
-- > -h --hello[=VALUE] (default=foo)
--
-- Note that all flags in CmdArgs are optional, and if omitted will use their default value.
-- Those annotated with @opt@ also allow the flag to be present without an associated value.
-- As an example:
--
-- > {hello = "DEFAULT" &= opt "OPTIONAL"}
--
-- > $ main
-- > {hello = "DEFAULT"}
-- > $ main --hello
-- > {hello = "OPTIONAL"}
-- > $ main --hello=VALUE
-- > {hello = "VALUE"}
opt :: (Show a, Typeable a) => a -> Ann
opt x = FlagOptional $ case cast x of
Just y -> y
_ -> show x
-- | Flag: \"For this flag, users need to give something of type ...\"
--
-- The the type of a flag's value, usually upper case. Only used
-- for the help message. Commonly the type will be @FILE@ ('typFile')
-- or @DIR@ ('typDir').
--
-- > {hello = def &= typ "MESSAGE"}
-- > -h --hello=MESSAGE
typ :: String -> Ann
typ = FlagType
-- | Flag: \"Users must give a file for this flag's value.\"
--
-- Alias for @'typ' "FILE"@.
typFile :: Ann
typFile = typ "FILE"
-- | Flag: \"Users must give a directory for this flag's value.\"
--
-- Alias for @'typ' "DIR"@.
typDir :: Ann
typDir = typ "DIR"
-- | Flag/Mode: \"The help message is ...\"
--
-- Descriptive text used in the help output.
--
-- > {hello = def &= help "Help message"}
-- > -h --hello=VALUE Help message
help :: String -> Ann
help = Help
{-
-- | Flag: Specify group membership for this flag
--
-- > {str = def &= group "Repository Management"
-- > ---- Repository Management ----
-- > -s --str=VALUE
group :: String -> Ann
group = FldGroup
-}
-- | Flag: \"Use this flag name for this field.\"
--
-- Add flags which trigger this option.
--
-- > {hello = def &= name "foo"}
-- > -h --hello --foo=VALUE
name :: String -> Ann
name = Name
-- | Flag: \"Put non-flag arguments here.\"
--
-- All argument flags not captured by 'argPos' are returned by 'args'.
--
-- > {hello = def &= args}
args :: Ann
args = FlagArgs
-- | Flag: \"Put the nth non-flag argument here.\"
--
-- This field should be used to store a particular argument position
-- (0-based).
--
-- > {hello = def &= argPos 0}
argPos :: Int -> Ann
argPos = FlagArgPos
-- | Flag\/Mode: \"Give these flags/modes a group name in the help output.\"
--
-- This mode will be used for all following modes/flags, until the
-- next @groupname@.
--
-- > {hello = def &= groupname "Welcomes"}
-- > Welcomes
-- > -h --hello=VALUE
groupname :: String -> Ann
groupname = GroupName
-- | Mode: \"A longer description of this mode is ...\"
--
-- Suffix to be added to the help message.
--
-- > Sample{..} &= details ["More details on the website www.example.org"]
details :: [String] -> Ann
details = ModeHelpSuffix
-- | Modes: \"My program name\/version\/copyright is ...\"
--
-- One line summary of the entire program, the first line of
-- @--help@ and the only line of @--version@. If the string contains a
-- version number component will also provide @--numeric-version@.
--
-- > Sample{..} &= summary "CmdArgs v0.0, (C) Neil Mitchell 1981"
summary :: String -> Ann
summary = ProgSummary
-- | Mode: \"If the user doesn't give a mode, use this one.\"
--
-- This mode is the default. If no mode is specified and a mode has this
-- attribute then that mode is selected, otherwise an error is raised.
--
-- > modes [Mode1{..}, Mode2{..} &= auto, Mode3{..}]
auto :: Ann
auto = ModeDefault
-- | Modes: \"My program executable is named ...\"
--
-- This is the name of the program executable. Only used in the help message.
-- Defaults to the type of the mode.
--
-- > Sample{..} &= program "sample"
program :: String -> Ann
program = ProgProgram
-- | Flag: \"Don't guess any names for this field.\"
--
-- A field should not have any flag names guessed for it.
-- All flag names must be specified by 'flag'.
--
-- > {hello = def &= explicit &= name "foo"}
-- > --foo=VALUE
explicit :: Ann
explicit = Explicit
-- | Flag/Mode: \"Ignore this field, don't let the user set it.\"
--
-- A mode or field is not dealt with by CmdArgs.
--
-- > {hello = def, extra = def &= ignore}
-- > --hello=VALUE
ignore :: Ann
ignore = Ignore
-- | Modes: \"My program needs verbosity flags.\"
--
-- Add @--verbose@ and @--quiet@ flags.
verbosity :: Ann
verbosity = ProgVerbosity
-- | Modes: \"Customise the help argument.\"
--
-- Add extra options to a help argument, such as 'help', 'name', 'ignore' or 'explicit'.
--
-- > Sample{..} &= helpArg [explicit, name "h"]
helpArg :: [Ann] -> Ann
helpArg = ProgHelpArg
-- | Modes: \"Customise the version argument.\"
--
-- Add extra options to a version argument, such as 'help', 'name', 'ignore', 'summary' or 'explicit'.
--
-- > Sample{..} &= versionArg [ignore]
versionArg :: [Ann] -> Ann
versionArg = ProgVersionArg
-- | Modes: \"Customise the verbosity arguments.\"
--
-- Add extra options to a verbosity arguments (@--verbose@ and @--quiet@),
-- such as 'help', 'name', 'ignore' or 'explicit'. The verbose options come
-- first, followed by the quiet options.
--
-- > Sample{..} &= verbosityArgs [ignore] [name "silent", explicit]
verbosityArgs :: [Ann] -> [Ann] -> Ann
verbosityArgs = ProgVerbosityArgs
-- | Program: \"Turn off \@ expansion.\"
--
-- Usually arguments starting with \@ are treated as a file containing
-- a set of arguments. This annotation turns off that behaviour.
--
-- > Sample{..} &= noAtExpand
noAtExpand :: Ann
noAtExpand = ProgNoAtExpand