Class: Belafonte::Flag

Inherits:
Object
  • Object
show all
Defined in:
lib/belafonte/flag.rb

Overview

Flag is the base class for switches and options

Direct Known Subclasses

Option, Switch

Constant Summary collapse

NoFlags =

No flags were given

Class.new(StandardError)
NoName =

No name was given

Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Flag

Returns a new instance of Flag.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/belafonte/flag.rb', line 25

def initialize(options = {})
  options[:name].tap do |name|
    unless name
      raise Errors::NoName.new("Flag name cannot be blank")
    end
    @name = name.to_sym
  end

  @short = flag_array(options[:short])
  @long = flag_array(options[:long])

  if short.empty? && long.empty?
    raise NoFlags.new("You must define at least one flag")
  end

  @description = options[:description] || ''
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



11
12
13
# File 'lib/belafonte/flag.rb', line 11

def description
  @description
end

#longObject (readonly)

Returns the value of attribute long.



11
12
13
# File 'lib/belafonte/flag.rb', line 11

def long
  @long
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/belafonte/flag.rb', line 11

def name
  @name
end

#shortObject (readonly)

Returns the value of attribute short.



11
12
13
# File 'lib/belafonte/flag.rb', line 11

def short
  @short
end

Class Method Details

.longify(option) ⇒ Object



17
18
19
# File 'lib/belafonte/flag.rb', line 17

def self.longify(option)
  "--#{option}"
end

.normalize_flag(flag) ⇒ Object



21
22
23
# File 'lib/belafonte/flag.rb', line 21

def self.normalize_flag(flag)
  flag.to_s.strip.gsub(/\s+/, '-')
end

.shortify(option) ⇒ Object



13
14
15
# File 'lib/belafonte/flag.rb', line 13

def self.shortify(option)
  "-#{option}"
end

Instance Method Details

#to_opt_parseObject



43
44
45
46
47
48
49
# File 'lib/belafonte/flag.rb', line 43

def to_opt_parse
  [
    short.map {|short_option| shortify(short_option)},
    long.map {|long_option| longify(long_option)},
    description
  ].flatten
end