class Benchmarker::OptionParser

Public Class Methods

help_message(command=nil) click to toggle source
# File lib/benchmarker.rb, line 931
    def self.help_message(command=nil)
      #; [!jnm2w] returns help message.
      command ||= File.basename($0)
      return <<"END"
Usage: #{command} [<options>]
  -h, --help     : help message
  -v             : print Benchmarker version
  -w <N>         : width of task name (default: 30)
  -n <N>         : loop N times in each benchmark (default: 1)
  -i <N>         : iterates all benchmark tasks N times (default: 1)
  -x <N>         : ignore worst N results and best N results (default: 0)
  -I[<N>]        : print inverse number (= N/sec) (default: same as '-n')
  -o <file>      : output file in JSON format
  -q             : quiet a little (suppress output of each iteration)
  -c             : enable colorized output
  -C             : disable colorized output
  -s <N>         : sleep N seconds after each benchmark task
  -S             : print sample code
  -F task=<...>  : filter benchmark task by name (operator: '=' or '!=')
  -F tag=<...>   : filter benchmark task by tag (operator: '=' or '!=')
  --<key>[=<val>]: define global variable `$opt_<key> = "<val>"`
END
    end
new(opts_noparam, opts_hasparam, opts_mayparam="") click to toggle source
# File lib/benchmarker.rb, line 846
def initialize(opts_noparam, opts_hasparam, opts_mayparam="")
  @opts_noparam  = opts_noparam
  @opts_hasparam = opts_hasparam
  @opts_mayparam = opts_mayparam
end
parse_options(argv=ARGV) { |"-#{c}#{c == 'I' ? '' : ' '}#{options}: integer expected."| ... } click to toggle source
# File lib/benchmarker.rb, line 896
def self.parse_options(argv=ARGV, &b)
  parser = self.new("hvqcCS", "wnixosF", "I")
  options, keyvals = parser.parse(argv, &b)
  #; [!v19y5] converts option argument into integer if necessary.
  "wnixI".each_char do |c|
    next if !options.key?(c)
    next if options[c] == true
    #; [!frfz2] yields error message when argument of '-n/i/x/I' is not an integer.
    options[c] =~ /\A\d+\z/  or
      yield "-#{c}#{c == 'I' ? '' : ' '}#{options[c]}: integer expected."
    options[c] = options[c].to_i
  end
  #; [!nz15w] convers '-s' option value into number (integer or float).
  "s".each_char do |c|
    next unless options.key?(c)
    case options[c]
    when /\A\d+\z/      ; options[c] = options[c].to_i
    when /\A\d+\.\d+\z/ ; options[c] = options[c].to_f
    else
      #; [!3x1m7] yields error message when argument of '-s' is not a number.
      yield "-#{c} #{options[c]}: number expected."
    end
  end
  #
  if options['F']
    #; [!emavm] yields error message when argumetn of '-F' option is invalid.
    if options['F'] !~ /^(\w+)(=|!=)[^=]/
      yield "-F #{options['F']}: invalid filter (expected operator is '=' or '!=')."
    elsif ! ($1 == 'task' || $1 == 'tag')
      yield "-F #{options['F']}: expected 'task=...' or 'tag=...'."
    end
  end
  return options, keyvals
end

Public Instance Methods

parse(argv) { |"#{argstr}: invalid option."| ... } click to toggle source
# File lib/benchmarker.rb, line 852
def parse(argv)
  #; [!2gq7g] returns options and keyvals.
  options = {}; keyvals = {}
  while !argv.empty? && argv[0] =~ /^-/
    argstr = argv.shift
    case argstr
    #; [!ulfpu] stops parsing when '--' found.
    when '--'
      break
    #; [!8f085] regards '--long=option' as key-value.
    when /^--/
      argstr =~ /^--(\w[-\w]*)(?:=(.*))?$/  or
        yield "#{argstr}: invalid option."
      key = $1; val = $2
      keyvals[key] = val || true
    #; [!dkq1u] parses short options.
    when /^-/
      i = 1
      while i < argstr.length
        c = argstr[i]
        if @opts_noparam.include?(c)
          options[c] = true
          i += 1
        elsif @opts_hasparam.include?(c)
          #; [!8xqla] error when required argument is not provided.
          options[c] = i+1 < argstr.length ? argstr[(i+1)..-1] : argv.shift()  or
            yield "-#{c}: argument required."
          break
        elsif @opts_mayparam.include?(c)
          options[c] = i+1 < argstr.length ? argstr[(i+1)..-1] : true
          break
        #; [!tmx6o] error when option is unknown.
        else
          yield "-#{c}: unknown option."
          i += 1
        end
      end
    else
      raise "** internall error"
    end
  end
  return options, keyvals
end