Class: OptionParser
- Inherits:
-
Object
- Object
- OptionParser
- Defined in:
- lib/option_parser.rb
Constant Summary collapse
- LEFT =
2
- MIDDLE =
33
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Class Method Summary collapse
- .get_empty_spaces ⇒ Object
- .last_arg(args = ARGV) ⇒ Object
- .parse(args = ARGV) {|parser| ... } ⇒ Object
Instance Method Summary collapse
- #banner(banner) ⇒ Object
-
#initialize(args = ARGV) ⇒ OptionParser
constructor
A new instance of OptionParser.
- #on(short_flag, long_flag, description, &block) ⇒ Object
- #process(args = ARGV) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(args = ARGV) ⇒ OptionParser
Returns a new instance of OptionParser.
19 20 21 22 23 |
# File 'lib/option_parser.rb', line 19 def initialize(args = ARGV) @args = args @banner = nil @flags = Array.new end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
6 7 8 |
# File 'lib/option_parser.rb', line 6 def args @args end |
Class Method Details
.get_empty_spaces ⇒ Object
70 71 72 |
# File 'lib/option_parser.rb', line 70 def self.get_empty_spaces " " * (MIDDLE + LEFT) end |
.last_arg(args = ARGV) ⇒ Object
15 16 17 |
# File 'lib/option_parser.rb', line 15 def self.last_arg(args = ARGV) args.length >= 1 ? args[args.length - 1] : '' end |
.parse(args = ARGV) {|parser| ... } ⇒ Object
8 9 10 11 12 13 |
# File 'lib/option_parser.rb', line 8 def self.parse(args = ARGV) parser = OptionParser.new yield parser parser.process args parser end |
Instance Method Details
#banner(banner) ⇒ Object
25 26 27 |
# File 'lib/option_parser.rb', line 25 def () @banner = end |
#on(short_flag, long_flag, description, &block) ⇒ Object
29 30 31 32 |
# File 'lib/option_parser.rb', line 29 def on(short_flag, long_flag, description, &block) @flags << { short_flag: short_flag || '', long_flag: long_flag || '', description: description || '', block: block } end |
#process(args = ARGV) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/option_parser.rb', line 34 def process( args = ARGV ) unless args.length == 0 args.each_with_index do |arg, i| @flags.each do |flag| name = -> (type_flag) do flag[type_flag].gsub( /[a-z -]/, '' ) end flag_strip = -> (type_flag) do flag[type_flag].sub( name.(type_flag), '' ).strip() end has_flag = -> (type_flag) { arg == flag_strip.(type_flag) } if has_flag.(:short_flag) or has_flag.(:long_flag) has_name = -> (type_flag) do name.(type_flag) != "" end value = nil if has_name.(:short_flag) or has_name.(:long_flag) value = args[i + 1] end flag[:block].call( value ) end end end else flag = @flags[0] flag[:block].call end end |
#to_s ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/option_parser.rb', line 74 def to_s() io = Array.new if = @banner io << io << "\n" end @flags.each do |flag| l_flag = !flag[:long_flag].empty? ? ", #{flag[:long_flag]}" : "" flags = "#{flag[:short_flag]}#{l_flag}".ljust(MIDDLE) desc = flag[:description].gsub("\n", "\n#{OptionParser.get_empty_spaces}") io << "".ljust(LEFT) + flags + desc io << "\n" end io.join end |