Class: CLASP::OptionSpecification

Inherits:
Object
  • Object
show all
Defined in:
lib/clasp/specifications.rb

Overview

A class that represents the specification for a command-line option

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil) ⇒ 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 be nil

    • 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 be nil

    • required (boolean) Whether the option is required. May be nil

    • required_message (::String) Message to be used when reporting that a required option is missing. May be nil 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. If nil, it is assigned an empty Hash

NOTE: Users should prefer the CLASP::Option() method



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
# File 'lib/clasp/specifications.rb', line 178

def initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil)

	@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 || {}

	rm_name				=	nil

	if required_message

		if "\0" == required_message[0]

			rm_name		=	required_message[1..-1]
		end
	else

		rm_name			=	"'#{name}'"
	end

	if rm_name

		required_message	=	"#{rm_name} not specified; use --help for usage"
	end

	@required_message	=	required_message
end

Instance Attribute Details

#aliasesObject (readonly)

The option’s aliases array



214
215
216
# File 'lib/clasp/specifications.rb', line 214

def aliases
  @aliases
end

#constraintObject (readonly)

The value constraint



226
227
228
# File 'lib/clasp/specifications.rb', line 226

def constraint
  @constraint
end

#default_valueObject (readonly)

The default value of the option



220
221
222
# File 'lib/clasp/specifications.rb', line 220

def default_value
  @default_value
end

#extrasObject (readonly)

The option’s extras



228
229
230
# File 'lib/clasp/specifications.rb', line 228

def extras
  @extras
end

#helpObject (readonly)

The option’s help string



216
217
218
# File 'lib/clasp/specifications.rb', line 216

def help
  @help
end

#nameObject (readonly)

The option’s name string



212
213
214
# File 'lib/clasp/specifications.rb', line 212

def name
  @name
end

#required_messageObject (readonly)

The message to be used when reporting that a required option is missing



224
225
226
# File 'lib/clasp/specifications.rb', line 224

def required_message
  @required_message
end

#values_rangeObject (readonly)

The range of values supported by the option



218
219
220
# File 'lib/clasp/specifications.rb', line 218

def values_range
  @values_range
end

Instance Method Details

#==(rhs) ⇒ Object

Compares instance against another OptionSpecification or against a name (String)



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/clasp/specifications.rb', line 261

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

Returns:

  • (Boolean)


222
# File 'lib/clasp/specifications.rb', line 222

def required?; @required; end

#to_sObject

String form of the option



231
232
233
234
# File 'lib/clasp/specifications.rb', line 231

def to_s

	"{#{name}; aliases=#{aliases.join(', ')}; values_range=[ #{values_range.join(', ')} ]; default_value='#{default_value}'; help='#{help}'; required?=#{required?}; required_message=#{required_message}; constraint=#{constraint}; extras=#{extras}}"
end