class ArgParser

Public Class Methods

atoi(string) click to toggle source

Translate a string like '23' to an integer like 23

# File argparser.rb, line 61
def self::atoi(string)
  ciphers = (48..57)
  a_num = string.bytes.collect {|b| b if ciphers === b}.compact
  if( ! a_num.empty? && a_num.length == string.length)
    number = 0
    # To facilitate work with the index, I avoid reverse_each and rather
    # reverse the array right away.
    a_num.reverse!
    a_num.each_with_index do |b, i|
      # Do the multiplication with 10s as taught in your first programming lessons.
      number += (b-48) * (10**i)
    end
    return number
  else
    return nil
  end 
end
parse(args, param_procs = {}) click to toggle source

Returns a structure describing the options. param_procs are closures to handle specific options, as needed by specific executables. There is a strategy-pattern lurking somewhere, but this appears to be the wrong place to apply it. The really cool detail here is the hash, not the procs.

# File argparser.rb, line 84
def self.parse(args, param_procs = {})
  # The options specified on the command line will be collected in <b>options</b>.
  # I set default values here.
  options = OpenStruct.new
  options.sheet = 1
  options.backup = false
  options.delref = nil

  op = OptionParser.new do |opts|
    opts.banner = trl("Usage") << ":\t" << "%s <options>" %($0) <<"\n"

    opts.separator ""
    opts.separator trl("Specific options") << ":"

    # An attempt to treat numerical sheet-ids and sheet-names alike on the
    # command-line. This can be wrong but I like it for the time.
    opts.on('-' << trl('s'), '--' << trl('sheet') << '=' << trl('SHEET'), trl('The sheet to modify. Name or numerical ID.')) do |sheet|
      # set to integer- or string-id
      options.sheet = atoi(sheet)
      options.sheet ||= sheet
    end

    opts.on('-' << trl('b'), '--' << trl('backup'), TrueClass, trl('Create a backup-copy of the original spreadsheet file.') ) do 
      options.backup = true
    end

    opts.on('-' << trl('d'), '--' << trl('delete EXPR'), trl('Delete all lines which conform to the expression.') ) do |expr|
      # handle the delete argument as needed by the calling executable.
      if(param_procs[:delete] && param_procs[:delete].lambda?)
         options.delete = param_procs[:delete].call(expr)
      else
        ex = OptionParser::ParseError.new()
        ex.reason = trl("Option %s is known but no way to handle it.") %'-d/--delete'
        raise ex
      end
    end

    opts.separator ""
    opts.separator (trl("Common options")) << ':'

    # show help
    opts.on_tail('-' << trl("h"), '--' << trl("help"), (trl("Show this message") ) ) do
      puts opts
      exit true
    end

    # show version information.
    opts.on_tail(("-v"), ("--version"), (trl("Show version and program information") )) do
      puts "\t#{$0} #{VERSION}"
      puts "\t#{COPYRIGHT}"
      exit true
    end
  end
  begin
    op.parse!(args)
  rescue OptionParser::ParseError => ex
    @log.error(trl('Option(s)') << ' ' << ex.args.join(', ' ) << ': ' << trl(ex.reason) )
    @log.info(trl("Call with option -h or --help to see the option summary.") )
    exit false
  end
  options
end