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.



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

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

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

Class Method Details

.help_screenObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 89

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 n      # line number to begin showing results on
  -L, --end-line n        # line number to stop showing results on
  -d, --line-length n     # max length of the entire line (only truncates results, not source lines)
  -D, --result-length n   # max length of the portion after the "# => "
  -I, --load-path dir     # a dir that should be added to the $LOAD_PATH
  -r, --require file      # additional files to be required before running the program
  -e, --program program   # Pass the program to execute as an argument
  -K, --encoding encoding # sets file encoding, equivalent to Ruby's -Kx (see `man ruby` for valid values)
  -a, --as filename       # run the program as if it was the specified filename
  -c, --clean             # remove annotations from previous runs of seeing_is_believing
  -h, --help              # this help screen
HELP_SCREEN
end

.parse(args) ⇒ Object



4
5
6
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 4

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

Instance Method Details

#callObject



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

def call
  @result ||= begin
    until args.empty?
      case (arg = args.shift)
      when '-h', '--help'          then options[:help] = self.class.help_screen
      when '-c', '--clean'         then options[:clean] = true
      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 '-r', '--require'       then next_arg("#{arg} expected a filename as the following argument but did not see one")  { |filename| options[:require]   << filename }
      when '-I', '--load-path'     then next_arg("#{arg} expected a directory as the following argument but did not see one") { |dir|      options[:load_path] << dir }
      when '-e', '--program'       then next_arg("#{arg} expected a program as the following argument but did not see one")   { |program|  options[:program]   = program }
      when '-a', '--as'            then next_arg("#{arg} expected a filename as the following argument but did not see one")  { |filename| options[:as]        = filename }
      when /\A-K(.+)/              then options[:encoding] = $1
      when '-K', '--encoding'      then next_arg("#{arg} expects an encoding, see `man ruby` for possibile values") { |encoding| options[:encoding] = encoding }
      when /^-/                    then options[:errors] << "Unknown option: #{arg.inspect}" # unknown flags
      else
        filenames << arg
        options[:filename] = arg
      end
    end
    normalize_and_validate
    options
  end
end