Class: TheFox::Timr::SimpleOptParser

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

Overview

Simple Option Parser

Because OptParse sucks. Parsing arguments should be easy. :(

Covered Usecases

  • -a

  • –all

  • -a –all

  • -a -b val

  • -a -b val cmd

  • -a -b val cmd1 cmd2

  • -a -b val1 val2

  • -a -b val1 val2 cmd1 cmd2

  • -ab

  • -ab val

### Usage “‘ optparser = SimpleOptParser.new optparser.register_option() optparser.register_option() optparser.register_option(, 1) opts = optparser.parse(’-a -b -c val’) “‘

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSimpleOptParser

Returns a new instance of SimpleOptParser.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/timr/simple_opt_parser.rb', line 38

def initialize
  #  -a   => 0
  # --all => 0
  #  -b   => 1
  @valid_options = Hash.new
  
  # a   => 0
  # all => 0
  # b   => 1
  # @valid_options_without_prefix = Hash.new
  
  # Parsed Options (+Commands)
  @options = Array.new
  
  # Not Recognized Options
  @unknown_options = Array.new
end

Instance Attribute Details

#optionsObject (readonly)

Parsed Options (+Commands)



33
34
35
# File 'lib/timr/simple_opt_parser.rb', line 33

def options
  @options
end

#unknown_optionsObject (readonly)

Not Recognized Options



36
37
38
# File 'lib/timr/simple_opt_parser.rb', line 36

def unknown_options
  @unknown_options
end

Instance Method Details

#parse(args) ⇒ Object

‘args` (Array|String)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/timr/simple_opt_parser.rb', line 82

def parse(args)
  # Reset previous options.
  @options = []
  
  if args.is_a?(Array)
    pre_argv = args
  else
    pre_argv = args.split(' ')
  end
  
  argv = Array.new
  
  # Pre-process Special Argument (Compact)
  # For example '-abcd' is '-a -b -c -d'.
  pre_argv.each do |arg|
    if arg[0] == '-'
      if arg[1] == '-'
        # Normal --arg
        argv << arg
      else
        if arg.length > 2
          # Special -ab
          arg = arg[1..-1]
          
          # Add each single argument separate.
          # Array ['-abc'] will become ['-a', '-b', '-c'].
          arg.length.times do |n|
            argv << "-#{arg[n]}"
          end
        else
          # Normal -a
          argv << arg
        end
      end
    else
      # Commands, Values, etc
      argv << arg
    end
  end
  
  loop_c = 0 # Limit the loop.
  while argv.length > 0 && loop_c < 1024
    loop_c += 1
    
    arg = argv.shift
    
    if arg[0] == '-'
      if @valid_options[arg]
        # Recognized Argument
        
        number_values = @valid_options[arg]
        
        if number_values == 0
          # No values. Like Command.
          @options << [arg]
        elsif number_values == -1
          # Eat all the arguments. Nom nom nom.
          arg_values = []
          sub_loop_c = 0 # Limit the loop.
          while argv.length > 0 && sub_loop_c < 1024
            sub_loop_c += 1
            
            arg = argv.shift
            
            # When reaching the end.
            unless arg
              break
            end
            
            arg_values << arg
          end
          @options << arg_values
        else
          # n values.
          
          arg_values = []
          sub_loop_c = 0 # Limit the loop.
          while argv.length > 0 && sub_loop_c < number_values && sub_loop_c < 1024
            sub_loop_c += 1
            
            val = argv.shift
            
            # When reaching the end.
            unless val
              raise ArgumentError, "SimpleOptParser: Argument '#{arg}' expects #{number_values} value(s). Found #{sub_loop_c}."
            end
            
            if @valid_options[val]
              raise ArgumentError, "SimpleOptParser: Argument '#{arg}' expects #{number_values} value(s). Found another Argument '#{val}'."
            end
            
            arg_values << val
          end
          
          if arg_values.length != number_values
            raise ArgumentError, "SimpleOptParser: Argument '#{arg}' expects #{number_values} value(s), #{arg_values.length} given."
          end
          
          arg_values.unshift(arg)
          @options << arg_values
        end
      else
        # Unknown Arguments
        # '-ab' arguments will be processed in pre_argv.
        @unknown_options << arg
      end
    else
      # Command
      @options << [arg]
    end
  end
  
  @options
end

#register_option(aliases, number_values = 0) ⇒ Object

‘aliases` (Array)

‘number_values`

  • 0 = no value, just a switch

  • -1 = unlimited

  • n = n values



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/timr/simple_opt_parser.rb', line 63

def register_option(aliases, number_values = 0)
  aliases.each do |ali|
    @valid_options[ali] = number_values
    
    # if ali[0] == '-'
    #   if ali[1] == '-'
    #     # --arg
    #     # Cut the two first characters.
    #     @valid_options_without_prefix[ali[2..-1]] = number_values
    #   else
    #     # -a
    #     # Cut only the first character.
    #     @valid_options_without_prefix[ali[1..-1]] = number_values
    #   end
    # end
  end
end

#verify(opts) ⇒ Object

Do not re-parse. Verify an already parsed array.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/timr/simple_opt_parser.rb', line 198

def verify(opts)
  opts.each do |opt_ar|
    arg = opt_ar.first
    
    number_values = @valid_options[arg]
    if number_values.nil?
      # Unknown Arguments
    else
      # Argument is valid.
      
      # Check length.
      if number_values == 0
        if opt_ar.length > 0
          raise ArgumentError, "SimpleOptParser: Argument '#{arg}' expects no values, #{opt_ar.length} given."
        end
      elsif number_values == -1
        # Always OK. Keep eating, Pacman.
      else
        if number_values != opt_ar.length
          raise ArgumentError, "SimpleOptParser: Argument '#{arg}' expects #{number_values} value(s), #{opt_ar.length} given."
        end
      end
    end
  end
  
  true
end