Class: Ame::Option

Inherits:
Argument show all
Defined in:
lib/ame/option.rb

Instance Attribute Summary collapse

Attributes inherited from Argument

#default, #description, #name

Instance Method Summary collapse

Methods inherited from Argument

#arity, #optional?, #process, #required?

Constructor Details

#initialize(name, description, options = {}, &validate) ⇒ Option

Returns a new instance of Option.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ame/option.rb', line 4

def initialize(name, description, options = {}, &validate)
  is_boolean_type = [TrueClass, FalseClass].include? options[:type]
  options[:default] = false unless options.include? :default or options.include? :type
  if is_boolean_type and not options.include? :default
    options[:default] = options[:type] == FalseClass
  end
  is_boolean = [true, false].include? options[:default]
  raise ArgumentError,
    'optional arguments to options are only allowed for booleans' if
      options[:optional] and not(is_boolean_type or is_boolean)
  options[:optional] = is_boolean
  raise ArgumentError,
    'boolean options cannot have argument descriptions' if
      is_boolean and options[:argument]
  @argument_name = is_boolean ? "" : (options[:argument] || name).to_s
  @aliases = Array(options[:alias]) + Array(options[:aliases])
  @ignored = options[:ignore]
  super
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



28
29
30
# File 'lib/ame/option.rb', line 28

def aliases
  @aliases
end

#argument_nameObject (readonly)

Returns the value of attribute argument_name.



28
29
30
# File 'lib/ame/option.rb', line 28

def argument_name
  @argument_name
end

Instance Method Details

#ignored?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ame/option.rb', line 38

def ignored?
  @ignored
end

#longObject



34
35
36
# File 'lib/ame/option.rb', line 34

def long
  [name, *aliases].find{ |a| a.to_s.length > 1 }
end

#shortObject



30
31
32
# File 'lib/ame/option.rb', line 30

def short
  [name, *aliases].find{ |a| a.to_s.length == 1 }
end

#to_sObject



24
25
26
# File 'lib/ame/option.rb', line 24

def to_s
  (name.to_s.length > 1 ? '--%s' : '-%s') % name
end