Class: Clop

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

Constant Summary collapse

HELP_DEFINITION_STRING =
"\n\nShort name:     -h\nLong name:    --help\nDescription:    Help facility\nLong description:\n  When providing the command line option -h, followed by one or more\n  options, a one-line summary of each of the options will be printed.\n\n  These options can be specified in either short or long form, i.e\n  typing \"some_command -h -x\" or \"some_command -h --extra\" will produce\n  the same output, if \"-x\" and \"--extra\" invoke the same option.\n\n  When providing the command line option -h, with nothing else following,\n  a one-line summary of all options will be printed.\n\n  When providing the command line option --help, instead of -h, the same\n  actions occur, the only difference being that instead of a one-liner,\n  a longer description will be printed.\n\n  Anything that appears on the command line between the name of the\n  program and \"-h\" or \"--help\" will be ignored.  For example, typing\n  \"some_command gobbledygook -h -x\" will produce the same output as\n  typing \"some_command -h -x\"\n\n\nLong name:    ---help\nDescription:    Program description (the header part of --help)\nLong description:\n  This option prints only the header part of what would be printed with\n  the option --help, without printing all the information about specific\n  option.  In other words, it provides only the general information about\n  the program itself.\n\n\n"
@@proclist =
[]
@@additional_def_string =
""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(def_str, argv_array, global_variables_flag) ⇒ Clop

Returns a new instance of Clop.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/clop.rb', line 110

def initialize(def_str, argv_array, global_variables_flag)
  parse_option_definitions(def_str+@@additional_def_string)
  parse_command_line_options(argv_array)
  initialize_option_variables
  initialize_global_variables if global_variables_flag
  check_required_options
  eval(mk_reader,TOPLEVEL_BINDING)
  @@proclist.each{|x| x.call(self)}
  @@the_only_instance = self
  print_values unless defined? @do_not_print_values
end

Instance Attribute Details

#do_not_print_values=(value) ⇒ Object (writeonly)

Sets the attribute do_not_print_values

Parameters:

  • value

    the value to set the attribute do_not_print_values to.



106
107
108
# File 'lib/clop.rb', line 106

def do_not_print_values=(value)
  @do_not_print_values = value
end

Class Method Details

.add_defs(def_str) ⇒ Object



352
353
354
# File 'lib/clop.rb', line 352

def Clop.add_defs(def_str)
  @@additional_def_string+= def_str
end

.add_to_initialize_action_list(x) ⇒ Object



126
127
128
# File 'lib/clop.rb', line 126

def Clop.add_to_initialize_action_list(x)
  @@proclist.push x
end

.optionObject



122
123
124
# File 'lib/clop.rb', line 122

def Clop.option
  @@the_only_instance
end

Instance Method Details

#check_required_optionsObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/clop.rb', line 229

def check_required_options
  options_missing = 0
  @options.each do |x|
    if x.valuestring == "none"
      options_missing += 1
      STDERR.print "option "
      STDERR.print "\"#{x.shortname}\" or " if x.shortname
      STDERR.print "\"#{x.longname}\" required.  "
      STDERR.print "Description:\n#{x.longdescription}\n"
    end
  end
  if options_missing > 0
    STDERR.print "Please provide the required command line option"
    STDERR.print "s" if options_missing > 1
    STDERR.print ".\n"
    exit(1)
  end
end

#default_value_string(option) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'lib/clop.rb', line 300

def default_value_string(option)
  s = ""
  if option.type and option.type != "bool"
    reference_size = "#{option.description}".size + 2
    s = option.add_tabs(s, reference_size, 4)
    s += " [default: #{option.defaultvalue}]"
  end
  return s
end

#find_option(s) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/clop.rb', line 178

def find_option(s)
  i = nil
  @options.each_index do |x|
    i = x if s == @options[x].longname
    if @options[x].shortname
      i = x if s =~ Regexp.new(@options[x].shortname) and $` == ""
    end
  end
  return i
end

#help_string(option, long_flag) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/clop.rb', line 273

def help_string(option, long_flag)
  s = ""
  if option.type or option.longname =~ /-+-help/
    s += option_name_string(option)
  end
  if option.type or option.longname =~ /-+-help/ or not long_flag
    s += "#{option.description}"
    s += default_value_string(option)
    s += "\n"
  end
  if long_flag
    s += "\n#{option.longdescription}\n"
  end
  return s
end

#initialize_global_variablesObject



218
219
220
# File 'lib/clop.rb', line 218

def initialize_global_variables
  @options.each{|x| eval("$#{x.variablename}=x.eval_value") if x.variablename}
end

#initialize_option_variablesObject

def initialize_variables(magic) # for connoisseurs . . .

  @options.each{|x| eval("#{magic}#{x.variablename}=x.eval_value") if
                                                             x.variablename}
end


214
215
216
# File 'lib/clop.rb', line 214

def initialize_option_variables
  @options.each{|x| eval("@#{x.variablename}=x.eval_value") if x.variablename}
end

#mk_readerObject



222
223
224
225
226
227
# File 'lib/clop.rb', line 222

def mk_reader
  s = "class Clop \n attr_reader"
  @options.each{|x| s += " :#{x.variablename}," if x.variablename}
  s.chop!
  s + "\n end"
end

#option_name_string(option) ⇒ Object



289
290
291
292
293
294
295
296
297
298
# File 'lib/clop.rb', line 289

def option_name_string(option)
  s = ""
  if option.shortname
    s += "#{option.shortname}  "
  end
  s += "#{option.longname}"
  s = option.add_tabs(s, s.size, 3)
  s += ": "
  return s
end

#parse_command_line_options(argv_array) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/clop.rb', line 143

def parse_command_line_options(argv_array)
  s1 = argv_array.clone
  while s = s1.shift
    if s == "-h"
      parse_help(s1, false)
      exit
    elsif s == "--help"
      parse_help(s1, true)
      exit
    elsif s == "---help"
      print_help(true, 0)
      exit
    end
  end
  while s = argv_array.shift
    if i = find_option(s)
      parse_option(i, s, argv_array)
    else
      raise "\n  option \"#{s}\" not recognized; try \"-h\" or \"--help\"\n"
    end
  end
end

#parse_help(argv_array, long) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/clop.rb', line 248

def parse_help(argv_array, long)
  all = true
  while s = argv_array.shift
    all = false
    if i = find_option(s)
      print_help(long, i)
    else
      print_help_warning(s)
    end
  end
  print_help(long) if all
end

#parse_option(i, s, argv_array) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/clop.rb', line 189

def parse_option(i, s, argv_array)
  if @options[i].type == "bool"
    @options[i].valuestring = "true"
    return
  end
  if s =~ /^-[^-]/ and (value = $') =~ /\w/
    @options[i].valuestring = value
  else
    unless @options[i].valuestring = argv_array.shift
      raise "\n  option \"#{s}\" requires a value, but no value given;\n" +
            "  option description: #{@options[i].description}\n"
    end
  end
  # if @options[i].type =~ /^float\s*vector$/
  #       while (@options[i].valuestring !~ /\]/)
  #         @options[i].valuestring += " " + argv_array.shift
  #       end
  #     end
end

#parse_option_definitions(def_str) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/clop.rb', line 130

def parse_option_definitions(def_str)
  def_str += HELP_DEFINITION_STRING
  a = def_str.split("\n")
  @options=[]
  while a[0]
    if a[0] =~ /^\s*$/
      a.shift
    else
      @options.push(Clop_Option.new(a))
    end
  end
end


261
262
263
264
265
266
267
# File 'lib/clop.rb', line 261

def print_help(long, i = nil)
  if i
    STDERR.print help_string(@options[i], long)
  else
    @options.each{|x| STDERR.print help_string(x, long)}
  end
end


269
270
271
# File 'lib/clop.rb', line 269

def print_help_warning(s)
  STDERR.print "WARNING : ", s, " : ==> this option is not recognized <==\n"
end


174
175
176
# File 'lib/clop.rb', line 174

def print_values
  STDERR.print to_s
end

#to_sObject



166
167
168
169
170
171
172
# File 'lib/clop.rb', line 166

def to_s
  s = "==> " + @options[0].description + " <==\n"
  for i in 1...@options.size - 2   # exclude header & help (first & last two)
    s += @options[i].to_s
  end
  s
end