argser 1.5 → 1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,8 +28,8 @@ Argser::Parser.new({
28
28
  - <code>:usage</code>, a string describing how to use the application (default: "")
29
29
  - <code>:details</code>, a string containing details about the application (default: "")
30
30
  - <code>:info</code>, a string printer bellow errors indicating how to find more information about them (default: "For more information try '--help'")
31
- - <code>:unknown</code>, a string with the message to show when a parameter is not known (default: "Unknown parameter '$p'")
32
- - <code>:fatal</code>, a string with the message to show when an internal error is found while parsing parameters (default: "An internal error was found")
31
+ - <code>:unknown</code>, a string with the error to show when a parameter is not known (default: "Unknown parameter '$p'")
32
+ - <code>:fatal</code>, a string with the error to show when an internal error is found while parsing parameters (default: "An internal error was found")
33
33
  - <code>:help</code>, a boolean value indicating if the parser should automatically display the help if parameter :help is given (default: true)
34
34
 
35
35
  ## PARAMETERS OPTIONS
@@ -38,10 +38,10 @@ Argser::Parser.new({
38
38
  - <code>:type</code>, symbol indicating the type of this parameter (default: :string)
39
39
  - <code>:default</code>, the default value if not other value is provided (default: '')
40
40
  - <code>:required</code>, a boolean value indicating if this parameter is required or not
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)
41
+ - <code>:sanitize</code>, a function to sanitize the value given (return the identifier of an error 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
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
+ - <code>:errors</code>, a hash with all available errors to show where the key is used as an identifier (:type and :required are created by default)
45
45
 
46
46
  ## REMAINING ARRAY OPTIONS
47
47
 
@@ -49,10 +49,10 @@ Argser::Parser.new({
49
49
  - <code>:default</code>, the default value if not other value is provided (default: '')
50
50
  - <code>:minimum</code>, minimum number of strings allowed
51
51
  - <code>:maximum</code>, maximum number of strings allowed
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
+ - <code>:sanitize</code>, a function to sanitize the value given (return the identifier of an error to show it or the sanitized value the parser should save)
53
53
  - <code>:callback</code>, a function to call to do something with the parameter
54
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)
55
+ - <code>:errors</code>, a hash with all available errors to show where the key is used as an identifier (:type and :required are created by default)
56
56
 
57
57
  ## AVAILABLE TYPES
58
58
 
@@ -91,8 +91,8 @@ parser = Argser::Parser.new({
91
91
  default: 18,
92
92
  type: :integer,
93
93
  sanitize: lambda {|value, default| (value >= 18) ? value : :young},
94
- messages: {
95
- help: "Specify your age",
94
+ help: "Specify your age",
95
+ errors: {
96
96
  type: "I can't believe you're '$v' years old",
97
97
  young: "You can't use this application unless you're older than 18!"
98
98
  }
@@ -103,8 +103,8 @@ parser = Argser::Parser.new({
103
103
  minimum: 1,
104
104
  maximum: 1,
105
105
  callback: lambda {|values, default| puts "Alright, I got your name!"},
106
- messages: {
107
- help: "Specify your name and surname",
106
+ help: "Specify your name and surname",
107
+ errors: {
108
108
  type: "What!? Your name is '$v'!?"
109
109
  }
110
110
  }
@@ -117,7 +117,8 @@ if (parser.param? :age)
117
117
  comment = case parser.param(:age)
118
118
  when 18..20 then "you're a teenager!"
119
119
  when 21..30 then "you're a young man!"
120
- when 30..99 then "you're a little adult!"
120
+ when 30..99 then "you're an adult!"
121
+ when 100..1.0/0.0 then "you're awesome!"
121
122
  end
122
123
  else
123
124
  comment = 'how old are you?'
@@ -139,6 +140,8 @@ greeting [OPTIONS] NAME
139
140
  -a --age Specify your age
140
141
  -h --help Display this help and exit
141
142
 
143
+ Specify your name and surname
144
+
142
145
  Provided your age and your name, this application will greet you in a unique way.
143
146
  ```
144
147
 
@@ -17,7 +17,7 @@ parser = Argser::Parser.new({
17
17
  type: :integer,
18
18
  sanitize: lambda {|value, default| (value >= 18) ? value : :young},
19
19
  help: "Specify your age",
20
- messages: {
20
+ errors: {
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
  }
@@ -29,7 +29,7 @@ parser = Argser::Parser.new({
29
29
  maximum: 1,
30
30
  callback: lambda {|values, default| puts "Alright, I got your name!"},
31
31
  help: "Specify your name and surname",
32
- messages: {
32
+ errors: {
33
33
  type: "What!? Your name is '$v'!?"
34
34
  }
35
35
  }
@@ -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
- raise_error(param[:messages][:required], "--#{param[:token].to_s}") unless param?(token) or !param[:required]
63
+ raise_error(param[:errors][: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
- raise_error(@remaining[:messages][:required]) unless (@remaining[:minimum] <= remaining?) and (remaining? <= @remaining[:maximum])
69
+ raise_error(@remaining[:errors][:required]) unless (@remaining[:minimum] <= remaining?) and (remaining? <= @remaining[:maximum])
70
70
  validate_remaining()
71
71
  sanitize(@remaining)
72
72
 
@@ -124,7 +124,7 @@ module Argser
124
124
  exit
125
125
  end
126
126
 
127
- #Raise an error message
127
+ #Raise an error
128
128
  def raise_error(message, token='', value='')
129
129
  raise ParserError.new(message || @options[:fatal], token, value)
130
130
  end
@@ -136,8 +136,8 @@ module Argser
136
136
  # :usage A string describing how to use the application (default: "")
137
137
  # :details A string containing details about the application (default: "")
138
138
  # :info A string printer bellow errors indicating how to find more information about them (default: "For more information try '--help'")
139
- # :unknown A string with the message to show when a parameter is not known (default: "Unknown parameter '$p'")
140
- # :fatal A string with the message to show when an internal error is found while parsing parameters (default: "An internal error was found")
139
+ # :unknown A string with the error to show when a parameter is not known (default: "Unknown parameter '$p'")
140
+ # :fatal A string with the error to show when an internal error is found while parsing parameters (default: "An internal error was found")
141
141
  # :help A boolean value indicating if the parser should automatically display the help if parameter :help is given (default: true)
142
142
 
143
143
  #Save all options for the parser
@@ -158,10 +158,10 @@ module Argser
158
158
  # :type A symbol indicating the type of this parameter (default: :string)
159
159
  # :default The default value if not other value is provided (default: '')
160
160
  # :required A boolean value indicating if this parameter is required or not
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)
161
+ # :sanitize A function to sanitize the value given (return the identifier of an error 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
163
  # :help A string with the message to print as the help for this parameter
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
+ # :errors A hash with all available errors to show where the key is used as an identifier (:help, :type and :required are created by default)
165
165
 
166
166
  #Save all parameters the parser should recognize
167
167
  def init_params(params={})
@@ -172,7 +172,7 @@ module Argser
172
172
  type: :boolean,
173
173
  default: false,
174
174
  help: 'Display this help and exit',
175
- messages: {}
175
+ errors: {}
176
176
  }
177
177
  end
178
178
 
@@ -186,18 +186,18 @@ module Argser
186
186
  sanitize: lambda {|value, default| value},
187
187
  callback: lambda {|value, default|},
188
188
  help: "",
189
- messages: {}
189
+ errors: {}
190
190
  }.merge param
191
191
 
192
192
  @params[token][:token] = token
193
193
  @params[token][:value] = @params[token][:default]
194
194
  @params[token][:given] = false
195
195
 
196
- #Save messages, make sure :type and :required is available
197
- @params[token][:messages] = {
196
+ #Save errors, make sure :type and :required is available
197
+ @params[token][:errors] = {
198
198
  type: "ERROR: Invalid value '$v' for parameter '$p'",
199
199
  required: "ERROR: Parameter '$p' is required"
200
- }.merge @params[token][:messages]
200
+ }.merge @params[token][:errors]
201
201
 
202
202
  #Register all tokens for the parameter
203
203
  @tokens[token] = token
@@ -214,10 +214,10 @@ module Argser
214
214
  # :default The default value if not other value is provided (default: '')
215
215
  # :minimum Minimum number of strings allowed (default: 0)
216
216
  # :maximum Maximum number of strings allowed (default: Infinity)
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)
217
+ # :sanitize A function to sanitize the value given (return the identifier of an error 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
+ # :errors A hash with all available errors to show where the key is used as an identifier (:type and :required are created by default)
221
221
 
222
222
  #Save options for remaining strings
223
223
  def init_remaining(remaining={})
@@ -229,17 +229,17 @@ module Argser
229
229
  sanitize: lambda {|value, default| value},
230
230
  callback: lambda {|value, default|},
231
231
  help: "",
232
- messages: {}
232
+ errors: {}
233
233
  }.merge remaining
234
234
 
235
235
  @remaining[:value] = @remaining[:default]
236
236
  @remaining[:given] = 0
237
237
 
238
- #Save messages, make sure, :type and :required is available
239
- @remaining[:messages] = {
238
+ #Save errors, make sure, :type and :required is available
239
+ @remaining[:errors] = {
240
240
  type: "ERROR: Invalid value '$v'",
241
241
  required: "ERROR: Incorrect number of remaining arguments"
242
- }.merge @remaining[:messages]
242
+ }.merge @remaining[:errors]
243
243
  end
244
244
 
245
245
  #Validate the value of a parameter based on its type
@@ -247,11 +247,11 @@ module Argser
247
247
  #If the type is a symbol, it must be a valid type
248
248
  if (param[:type].is_a? Symbol)
249
249
  type = TYPES[param[:type]]
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
+ raise_error(param[:errors][:type], param[:token], param[:value]) if (param[:given]) and (param[:value].is_a? String) and !(match = type[0].match param[:value])
251
251
  #Convert value to the correct type
252
252
  param[:value] = type[1].call(param[:value], match, !param[:given])
253
253
  else #Otherwise it must be a regexp and the value must match
254
- raise_error(param[:messages][:type], param[:token], param[:value]) unless param[:value] =~ param[:type]
254
+ raise_error(param[:errors][:type], param[:token], param[:value]) unless param[:value] =~ param[:type]
255
255
  end
256
256
  end
257
257
 
@@ -261,21 +261,21 @@ module Argser
261
261
  if (@remaining[:type].is_a? Symbol)
262
262
  type = TYPES[@remaining[:type]]
263
263
  @remaining[:value].each_with_index do |value, i|
264
- raise_error(@remaining[:messages][:type], @remaining[:token], value) unless (@remaining[:given] == 0) or (match = type[0].match value)
264
+ raise_error(@remaining[:errors][:type], @remaining[:token], value) unless (@remaining[:given] == 0) or (match = type[0].match value)
265
265
  #Convert value to the correct type
266
266
  @remaining[:value][i] = type[1].call(value, match, @remaining[:given] == 0)
267
267
  end
268
268
  else #Otherwise it must be a regexp and the values must match
269
269
  @remaining[:value].each do |value|
270
- raise_error(@remaining[:messages][:type], @remaining[:token], value) unless value =~ @remaining[:type]
270
+ raise_error(@remaining[:errors][:type], @remaining[:token], value) unless value =~ @remaining[:type]
271
271
  end
272
272
  end
273
273
  end
274
274
 
275
- #Sanitize the value of the parameter, if a symbol is returned the associated message is shown
275
+ #Sanitize the value of the parameter, if a symbol is returned the associated error is shown
276
276
  def sanitize(param)
277
277
  value = param[:sanitize].call(param[:value], !param[:given])
278
- raise_error(param[:messages][value], param[:token], param[:value]) if value.is_a? Symbol
278
+ raise_error(param[:errors][value], param[:token], param[:value]) if value.is_a? Symbol
279
279
  #Update the value
280
280
  param[:value] = value
281
281
  end
@@ -288,7 +288,7 @@ module Argser
288
288
  @remaining[:callback].call(@remaining[:value], !@remaining[:given])
289
289
  end
290
290
 
291
- #Parser errors used to display messages ($p is replaced by the parameter used by the user, $v is replaced by the value given)
291
+ #Parser errors used to display errors ($p is replaced by the parameter used by the user, $v is replaced by the value given)
292
292
  class ParserError < Exception
293
293
  attr_reader :message
294
294
  def initialize(message, token='', value='')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: argser
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.5'
4
+ version: '1.6'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: