Class: Benchmarker::OptionParser

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts_noparam, opts_hasparam, opts_mayparam = "") ⇒ OptionParser

Returns a new instance of OptionParser.



847
848
849
850
851
# File 'lib/benchmarker.rb', line 847

def initialize(opts_noparam, opts_hasparam, opts_mayparam="")
  @opts_noparam  = opts_noparam
  @opts_hasparam = opts_hasparam
  @opts_mayparam = opts_mayparam
end

Class Method Details

.help_message(command = nil) ⇒ Object



932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/benchmarker.rb', line 932

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

.parse_options(argv = ARGV, &b) ⇒ Object



897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'lib/benchmarker.rb', line 897

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

Instance Method Details

#parse(argv) ⇒ Object



853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/benchmarker.rb', line 853

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