Class: CLASP::OptionSpecification
- Inherits:
-
SpecificationBase
- Object
- SpecificationBase
- CLASP::OptionSpecification
- Defined in:
- lib/clasp/specifications.rb
Overview
A class that represents the specification for a command-line option
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
(Proc) The procedure.
-
#aliases ⇒ Object
readonly
The option’s aliases array.
-
#constraint ⇒ Object
readonly
The value constraint.
-
#default_value ⇒ Object
readonly
The default value of the option.
-
#extras ⇒ Object
readonly
The option’s extras.
-
#help ⇒ Object
readonly
The option’s help string.
-
#name ⇒ Object
readonly
The option’s name string.
-
#required_message ⇒ Object
readonly
The message to be used when reporting that a required option is missing.
-
#values_range ⇒ Object
readonly
The range of values supported by the option.
Instance Method Summary collapse
-
#==(rhs) ⇒ Object
Compares instance against another OptionSpecification or against a name (String).
-
#initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil, &blk) ⇒ OptionSpecification
constructor
Creates an OptionSpecification instance from the given name, aliases, help, values_range, and default_value.
-
#required? ⇒ Boolean
Indicates whether the option is required.
-
#to_s ⇒ Object
String form of the option.
Constructor Details
#initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil, &blk) ⇒ OptionSpecification
Creates an OptionSpecification instance from the given name, aliases, help, values_range, and default_value
Signature
-
Parameters
-
name
(String
) The name, or long-form, of the option -
aliases
(Array
) 0 or more strings specifying short-form or option-value aliases -
help
(String
) The help string, which may benil
-
values_range
(Array
) 0 or more strings specifying values supported by the option -
default_value
(String
) The default value of the option, which will be used in the case where an option is specified without a value. May benil
-
required
(boolean) Whether the option is required. May benil
-
required_message
(::String) Message to be used when reporting that a required option is missing. May benil
in which case a message of the form “<option-name> not specified; use –help for usage”. If begins with the nul character (“0”), then is used in the place of the <option-name> and placed into the rest of the standard form message -
constraint
(Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version -
extras
An application-defined additional parameter. Ifnil
, it is assigned an emptyHash
-
-
Block An optional block that is called when a matching option argument is found
NOTE: Users should prefer the CLASP::Option() method
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/clasp/specifications.rb', line 239 def initialize(name, aliases, help, values_range, default_value, required, , constraint, extras = nil, &blk) check_arity_(blk, 0..3, "option") @name = name @aliases = (aliases || []).select { |a| a and not a.empty? } @help = help @values_range = values_range || [] @default_value = default_value @required = required @required_message = nil @constraint = constraint || {} @extras = extras || {} @action = blk rm_name = nil if if "\0" == [0] rm_name = [1..-1] end else rm_name = "'#{name}'" end if rm_name = "#{rm_name} not specified; use --help for usage" end @required_message = end |
Instance Attribute Details
#action ⇒ Object
(Proc) The procedure
295 296 297 |
# File 'lib/clasp/specifications.rb', line 295 def action @action end |
#aliases ⇒ Object (readonly)
The option’s aliases array
278 279 280 |
# File 'lib/clasp/specifications.rb', line 278 def aliases @aliases end |
#constraint ⇒ Object (readonly)
The value constraint
290 291 292 |
# File 'lib/clasp/specifications.rb', line 290 def constraint @constraint end |
#default_value ⇒ Object (readonly)
The default value of the option
284 285 286 |
# File 'lib/clasp/specifications.rb', line 284 def default_value @default_value end |
#extras ⇒ Object (readonly)
The option’s extras
292 293 294 |
# File 'lib/clasp/specifications.rb', line 292 def extras @extras end |
#help ⇒ Object (readonly)
The option’s help string
280 281 282 |
# File 'lib/clasp/specifications.rb', line 280 def help @help end |
#name ⇒ Object (readonly)
The option’s name string
276 277 278 |
# File 'lib/clasp/specifications.rb', line 276 def name @name end |
#required_message ⇒ Object (readonly)
The message to be used when reporting that a required option is missing
288 289 290 |
# File 'lib/clasp/specifications.rb', line 288 def @required_message end |
#values_range ⇒ Object (readonly)
The range of values supported by the option
282 283 284 |
# File 'lib/clasp/specifications.rb', line 282 def values_range @values_range end |
Instance Method Details
#==(rhs) ⇒ Object
Compares instance against another OptionSpecification or against a name (String)
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/clasp/specifications.rb', line 336 def == rhs case rhs when self.class return self.eql? rhs when String return name == rhs else false end end |
#required? ⇒ Boolean
Indicates whether the option is required
286 |
# File 'lib/clasp/specifications.rb', line 286 def required?; @required; end |
#to_s ⇒ Object
String form of the option
306 307 308 309 |
# File 'lib/clasp/specifications.rb', line 306 def to_s "{#{name}; aliases=#{aliases.join(', ')}; values_range=[ #{values_range.join(', ')} ]; default_value='#{default_value}'; help='#{help}'; required?=#{required?}; required_message=#{}; constraint=#{constraint}; extras=#{extras}}" end |