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.



17
18
19
20
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 17

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

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

Class Method Details

.help_screenObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 133

def ArgParser.help_screen
<<HELP_SCREEN
Usage: seeing_is_believing [options] [filename]

  seeing_is_believing 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 "# => "
  -s, --alignment-strategy name # select the alignment strategy:
                              chunk (DEFAULT) =>  each chunk of code is at the same alignment
                              file            =>  the entire file is at the same alignment
                              line            =>  each line is at its own alignment
  -t, --timeout n               # timeout limit in seconds when evaluating source file (ex. -t 0.3 or -t 3)
  -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
  -g, --debug                   # print debugging information (useful if program is fucking up, or if you want to brag)
  -x, --xmpfilter-style         # annotate marked lines instead of every line
  -i, --inherit-exit-status     # exit with the exit status of the program being eval
  -v, --version                 # print the version (#{VERSION})
  -h, --help                    # this help screen
HELP_SCREEN
end

.parse(args) ⇒ Object



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

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

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/seeing_is_believing/binary/arg_parser.rb', line 22

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 '-v', '--version'             then options[:version]             = true
      when '-x', '--xmpfilter-style'     then options[:xmpfilter_style]     = true
      when '-i', '--inherit-exit-status' then options[:inherit_exit_status] = true
      when '-g', '--debug'               then options[:debugger]            = Debugger.new(enabled: true, colour: 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 '-t', '--timeout'             then extract_non_negative_float_for :timeout, 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 '-s', '--alignment-strategy'  then extract_alignment_strategy
      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