Class: SeeingIsBelieving::Binary::ArgParser

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/binary/arg_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ArgParser

Returns a new instance of ArgParser.



15
16
17
18
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 15

def initialize(args)
  self.args      = args
  self.filenames = []
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



13
14
15
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 13

def args
  @args
end

Class Method Details

.help_screenObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 76

def ArgParser.help_screen
<<HELP_SCREEN
Usage: #{$0} [options] [filename]

  #{$0} is a program and library that will evaluate a Ruby file and capture/display the results.

  If no filename is provided, the binary will read the program from standard input.

  -l, --start-line    # line number to begin showing results on
  -L, --end-line      # line number to stop showing results on
  -d, --line-length   # max length of the entire line (only truncates results, not source lines)
  -D, --result-length # max length of the portion after the "# => "
  -h, --help          # this help screen
HELP_SCREEN
end

.parse(args) ⇒ Object



9
10
11
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 9

def self.parse(args)
  new(args).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 20

def call
  @result ||= begin
    until args.empty?
      case (arg = args.shift)
      when '-h', '--help'          then options[:help] = self.class.help_screen
      when '-l', '--start-line'    then extract_positive_int_for :start_line,    arg
      when '-L', '--end-line'      then extract_positive_int_for :end_line,      arg
      when '-d', '--line-length'   then extract_positive_int_for :line_length,   arg
      when '-D', '--result-length' then extract_positive_int_for :result_length, arg
      when /^-/                    then options[:errors] << "Unknown option: #{arg.inspect}" # unknown flags
      else # filenames
        filenames << arg
        options[:filename] = arg
      end
    end
    normalize_and_validate
    options
  end
end