Class: OptionParser

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

Constant Summary collapse

LEFT =
2
MIDDLE =
33

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argsObject (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_spacesObject



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

Yields:

  • (parser)


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



25
26
27
# File 'lib/option_parser.rb', line 25

def banner(banner)
    @banner = 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_sObject



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 = @banner
        io << banner
        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