argser 1.3 → 1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +4 -2
- data/examples/greetings.rb +5 -4
- data/lib/argser/parser.rb +22 -21
- metadata +1 -1
data/README.md
CHANGED
@@ -40,7 +40,8 @@ Argser::Parser.new({
|
|
40
40
|
- <code>:required</code>, a boolean value indicating if this parameter is required or not
|
41
41
|
- <code>:sanitize</code>, a function to sanitize the value given (return the identifier of a message to show it or the sanitized value the parser should save)
|
42
42
|
- <code>:callback</code>, a function to call to do something with the parameter
|
43
|
-
- <code>:
|
43
|
+
- <code>:help</code>, a string with the message to print as the help for this parameter
|
44
|
+
- <code>:messages</code> A hash with all available messages to show where the key is used as an identifier (:type and :required are created by default)
|
44
45
|
|
45
46
|
## REMAINING ARRAY OPTIONS
|
46
47
|
|
@@ -50,7 +51,8 @@ Argser::Parser.new({
|
|
50
51
|
- <code>:maximum</code>, maximum number of strings allowed
|
51
52
|
- <code>:sanitize</code>, a function to sanitize the value given (return the identifier of a message to show it or the sanitized value the parser should save)
|
52
53
|
- <code>:callback</code>, a function to call to do something with the parameter
|
53
|
-
- <code>:
|
54
|
+
- <code>:help</code>, a string with the message to print as the help for the remaining strings
|
55
|
+
- <code>:messages</code>, hash with all available messages to show where the key is used as an identifier (:type and :required are created by default)
|
54
56
|
|
55
57
|
## AVAILABLE TYPES
|
56
58
|
|
data/examples/greetings.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'argser'
|
1
|
+
require '../lib/argser/parser.rb'
|
2
2
|
|
3
3
|
parser = Argser::Parser.new({
|
4
4
|
options: {
|
@@ -16,8 +16,8 @@ parser = Argser::Parser.new({
|
|
16
16
|
default: 18,
|
17
17
|
type: :integer,
|
18
18
|
sanitize: lambda {|value, default| (value >= 18) ? value : :young},
|
19
|
+
help: "Specify your age",
|
19
20
|
messages: {
|
20
|
-
help: "Specify your age",
|
21
21
|
type: "I can't believe you're '$v' years old",
|
22
22
|
young: "You can't use this application unless you're older than 18!"
|
23
23
|
}
|
@@ -28,8 +28,8 @@ parser = Argser::Parser.new({
|
|
28
28
|
minimum: 1,
|
29
29
|
maximum: 1,
|
30
30
|
callback: lambda {|values, default| puts "Alright, I got your name!"},
|
31
|
+
help: "Specify your name and surname",
|
31
32
|
messages: {
|
32
|
-
help: "Specify your name and surname",
|
33
33
|
type: "What!? Your name is '$v'!?"
|
34
34
|
}
|
35
35
|
}
|
@@ -42,7 +42,8 @@ if (parser.param? :age)
|
|
42
42
|
comment = case parser.param(:age)
|
43
43
|
when 18..20 then "you're a teenager!"
|
44
44
|
when 21..30 then "you're a young man!"
|
45
|
-
when 30..99 then "you're
|
45
|
+
when 30..99 then "you're an adult!"
|
46
|
+
when 100..1.0/0.0 then "you're awesome!"
|
46
47
|
end
|
47
48
|
else
|
48
49
|
comment = 'how old are you?'
|
data/lib/argser/parser.rb
CHANGED
@@ -37,7 +37,7 @@ module Argser
|
|
37
37
|
if (arg =~ /^--?\w+$/)
|
38
38
|
arg = arg.downcase
|
39
39
|
|
40
|
-
|
40
|
+
raise_error(@options[:unknown], arg) unless @tokens.has_key? arg
|
41
41
|
param = @params[@tokens[arg]]
|
42
42
|
param[:given] = arg
|
43
43
|
if (@params[@tokens[arg]][:type] != :boolean)
|
@@ -60,13 +60,13 @@ module Argser
|
|
60
60
|
|
61
61
|
#Iterate over all parameters and process their values
|
62
62
|
@params.each do |token, param|
|
63
|
-
|
63
|
+
raise_error(param[:messages][:required], "--#{param[:token].to_s}") unless param?(token) or !param[:required]
|
64
64
|
validate_param(param)
|
65
65
|
sanitize(param)
|
66
66
|
end
|
67
67
|
|
68
68
|
#Process the value of the remaining array
|
69
|
-
|
69
|
+
raise_error(@remaining[:messages][:required]) unless (@remaining[:minimum] <= remaining?) and (remaining? <= @remaining[:maximum])
|
70
70
|
validate_remaining()
|
71
71
|
sanitize(@remaining)
|
72
72
|
|
@@ -116,16 +116,16 @@ module Argser
|
|
116
116
|
@params.each do |token, param|
|
117
117
|
token = "--#{token.to_s}"
|
118
118
|
shortcut = (param[:shortcut].is_a? Symbol) ? "-#{param[:shortcut]}" : ''
|
119
|
-
help = param[:
|
119
|
+
help = param[:help]
|
120
120
|
puts " #{shortcut.ljust(5, ' ')}#{token.ljust(20, ' ')}#{help}"
|
121
121
|
end
|
122
|
-
puts "\n#{25
|
122
|
+
puts "\n #{''.ljust(25, ' ')}#{@remaining[:help]}" unless @remaining[:help].empty?
|
123
123
|
puts "\n#{@options[:details]}" unless @options[:details].empty?
|
124
124
|
exit
|
125
125
|
end
|
126
126
|
|
127
127
|
#Raise an error message
|
128
|
-
def
|
128
|
+
def raise_error(message, token='', value='')
|
129
129
|
raise ParserError.new(message || @options[:fatal], token, value)
|
130
130
|
end
|
131
131
|
|
@@ -160,6 +160,7 @@ module Argser
|
|
160
160
|
# :required A boolean value indicating if this parameter is required or not
|
161
161
|
# :sanitize A function to sanitize the value given (return the identifier of a message to show it or the sanitized value the parser should save)
|
162
162
|
# :callback A function to call to do something with the parameter
|
163
|
+
# :help A string with the message to print as the help for this parameter
|
163
164
|
# :messages A hash with all available messages to show where the key is used as an identifier (:help, :type and :required are created by default)
|
164
165
|
|
165
166
|
#Save all parameters the parser should recognize
|
@@ -170,9 +171,8 @@ module Argser
|
|
170
171
|
shortcut: :h,
|
171
172
|
type: :boolean,
|
172
173
|
default: false,
|
173
|
-
|
174
|
-
|
175
|
-
}
|
174
|
+
help: 'Display this help and exit',
|
175
|
+
messages: {}
|
176
176
|
}
|
177
177
|
end
|
178
178
|
|
@@ -185,6 +185,7 @@ module Argser
|
|
185
185
|
required: false,
|
186
186
|
sanitize: lambda {|value, default| value},
|
187
187
|
callback: lambda {|value, default|},
|
188
|
+
help: "",
|
188
189
|
messages: {}
|
189
190
|
}.merge param
|
190
191
|
|
@@ -192,9 +193,8 @@ module Argser
|
|
192
193
|
@params[token][:value] = @params[token][:default]
|
193
194
|
@params[token][:given] = false
|
194
195
|
|
195
|
-
#Save messages, make sure :
|
196
|
+
#Save messages, make sure :type and :required is available
|
196
197
|
@params[token][:messages] = {
|
197
|
-
help: "",
|
198
198
|
type: "ERROR: Invalid value '$v' for parameter '$p'",
|
199
199
|
required: "ERROR: Parameter '$p' is required"
|
200
200
|
}.merge @params[token][:messages]
|
@@ -216,26 +216,27 @@ module Argser
|
|
216
216
|
# :maximum Maximum number of strings allowed (default: Infinity)
|
217
217
|
# :sanitize A function to sanitize the value given (return the identifier of a message to show it or the sanitized value the parser should save)
|
218
218
|
# :callback A function to call to do something with the parameter
|
219
|
-
# :
|
219
|
+
# :help A string with the message to print as the help for the remaining strings
|
220
|
+
# :messages A hash with all available messages to show where the key is used as an identifier (:type and :required are created by default)
|
220
221
|
|
221
222
|
#Save options for remaining strings
|
222
223
|
def init_remaining(remaining={})
|
223
224
|
@remaining = {
|
224
225
|
type: :string,
|
225
226
|
default: [],
|
226
|
-
minimum:
|
227
|
-
maximum:
|
227
|
+
minimum: 0,
|
228
|
+
maximum: Float::INFINITY,
|
228
229
|
sanitize: lambda {|value, default| value},
|
229
230
|
callback: lambda {|value, default|},
|
231
|
+
help: "",
|
230
232
|
messages: {}
|
231
233
|
}.merge remaining
|
232
234
|
|
233
235
|
@remaining[:value] = @remaining[:default]
|
234
236
|
@remaining[:given] = 0
|
235
237
|
|
236
|
-
#Save messages, make sure
|
238
|
+
#Save messages, make sure, :type and :required is available
|
237
239
|
@remaining[:messages] = {
|
238
|
-
help: "",
|
239
240
|
type: "ERROR: Invalid value '$v'",
|
240
241
|
required: "ERROR: Incorrect number of remaining arguments"
|
241
242
|
}.merge @remaining[:messages]
|
@@ -246,11 +247,11 @@ module Argser
|
|
246
247
|
#If the type is a symbol, it must be a valid type
|
247
248
|
if (param[:type].is_a? Symbol)
|
248
249
|
type = TYPES[param[:type]]
|
249
|
-
|
250
|
+
raise_error(param[:messages][:type], param[:token], param[:value]) if (param[:given]) and (param[:value].is_a? String) and !(match = type[0].match param[:value])
|
250
251
|
#Convert value to the correct type
|
251
252
|
param[:value] = type[1].call(param[:value], match, !param[:given])
|
252
253
|
else #Otherwise it must be a regexp and the value must match
|
253
|
-
|
254
|
+
raise_error(param[:messages][:type], param[:token], param[:value]) unless param[:value] =~ param[:type]
|
254
255
|
end
|
255
256
|
end
|
256
257
|
|
@@ -260,13 +261,13 @@ module Argser
|
|
260
261
|
if (@remaining[:type].is_a? Symbol)
|
261
262
|
type = TYPES[@remaining[:type]]
|
262
263
|
@remaining[:value].each_with_index do |value, i|
|
263
|
-
|
264
|
+
raise_error(@remaining[:messages][:type], @remaining[:token], value) unless (@remaining[:given] == 0) or (match = type[0].match value)
|
264
265
|
#Convert value to the correct type
|
265
266
|
@remaining[:value][i] = type[1].call(value, match, @remaining[:given] == 0)
|
266
267
|
end
|
267
268
|
else #Otherwise it must be a regexp and the values must match
|
268
269
|
@remaining[:value].each do |value|
|
269
|
-
|
270
|
+
raise_error(@remaining[:messages][:type], @remaining[:token], value) unless value =~ @remaining[:type]
|
270
271
|
end
|
271
272
|
end
|
272
273
|
end
|
@@ -274,7 +275,7 @@ module Argser
|
|
274
275
|
#Sanitize the value of the parameter, if a symbol is returned the associated message is shown
|
275
276
|
def sanitize(param)
|
276
277
|
value = param[:sanitize].call(param[:value], !param[:given])
|
277
|
-
|
278
|
+
raise_error(param[:messages][value], param[:token], param[:value]) if value.is_a? Symbol
|
278
279
|
#Update the value
|
279
280
|
param[:value] = value
|
280
281
|
end
|