Class: Belafonte::Flag
- Inherits:
-
Object
- Object
- Belafonte::Flag
- Defined in:
- lib/belafonte/flag.rb
Overview
Flag is the base class for switches and options
Constant Summary collapse
- NoFlags =
No flags were given
Class.new(StandardError)
- NoName =
No name was given
Class.new(StandardError)
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Flag
constructor
A new instance of Flag.
- #to_opt_parse ⇒ Object
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( = {}) [:name].tap do |name| unless name raise Errors::NoName.new("Flag name cannot be blank") end @name = name.to_sym end @short = flag_array([:short]) @long = flag_array([:long]) if short.empty? && long.empty? raise NoFlags.new("You must define at least one flag") end @description = [:description] || '' end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
11 12 13 |
# File 'lib/belafonte/flag.rb', line 11 def description @description end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
11 12 13 |
# File 'lib/belafonte/flag.rb', line 11 def long @long end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/belafonte/flag.rb', line 11 def name @name end |
#short ⇒ Object (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_parse ⇒ Object
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 |