Class: SeeingIsBelieving::Binary::ParseArgs

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, outstream) ⇒ ParseArgs

Returns a new instance of ParseArgs.



18
19
20
21
22
# File 'lib/seeing_is_believing/binary/parse_args.rb', line 18

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

Class Method Details

.call(args, outstream) ⇒ Object



14
15
16
# File 'lib/seeing_is_believing/binary/parse_args.rb', line 14

def self.call(args, outstream)
  new(args, outstream).call
end

.help_screenObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/seeing_is_believing/binary/parse_args.rb', line 142

def ParseArgs.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 "#{VALUE_MARKER}"
  -n, --number-of-captures n    # how many results to capture for a given line
                              if you had 1 million results on a line, it could take a long time to record
                              and serialize them, you might limit it to 1000 results as an optimization
  -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
  -j, --json                    # print results in json format (i.e. so another program can consume them)
  -i, --inherit-exit-status     # exit with the exit status of the program being eval
  --shebang ruby-executable # if you want SiB to use some ruby other than the one in the path
  -v, --version                 # print the version (#{VERSION})
  -h, --help                    # this help screen

Examples: A few examples, for a more comprehensive set of examples, check out features/flags.feature

  Run the file f.rb
$ echo __FILE__ > f.rb; seeing_is_believing f.rb
__FILE__  #{VALUE_MARKER}"f.rb"

  Aligning comments
$ ruby -e 'puts "123\\n4\\n\\n567890"' > f.rb


$ seeing_is_believing f.rb -s line
123  #{VALUE_MARKER}123
4  #{VALUE_MARKER}4

567890  #{VALUE_MARKER}567890


$ seeing_is_believing f.rb -s chunk
123  #{VALUE_MARKER}123
4    #{VALUE_MARKER}4

567890  #{VALUE_MARKER}567890


$ seeing_is_believing f.rb -s file
123     #{VALUE_MARKER}123
4       #{VALUE_MARKER}4

567890  #{VALUE_MARKER}567890

  Run against standard input
$ echo '3.times { |i| puts i }' | seeing_is_believing
2.times { |i| puts i }  #{VALUE_MARKER}2

#{STDOUT_MARKER}0
#{STDOUT_MARKER}1

  Run against a library you're working on by fixing the load path
$ seeing_is_believing -I lib f.rb

  Load up some library (can be used in tandem with -I)
$ seeing_is_believing -r pp -e 'pp [[*1..15],[*15..30]]; nil'
pp [[*1..15],[*15..30]]; nil  #{VALUE_MARKER}nil

#{STDOUT_MARKER}[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
#{STDOUT_MARKER} [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]]

  Only update the lines you've marked
$ ruby -e 'puts "1\\n2 # =>\\n3"' | seeing_is_believing -x
1
2 #{VALUE_MARKER}2
3

  Set a timeout (especially useful if running via an editor)
$ seeing_is_believing -e 'loop { sleep 1 }' -t 3
Timeout Error after 3.0 seconds!

  Set the encoding to utf-8
$ seeing_is_believing -Ku -e '"⛄ "'
"⛄ "  #{VALUE_MARKER}"⛄ "

  The exit status will be 1 if the error is displayable inline
$ seeing_is_believing -e 'raise "omg"'; echo $?
raise "omg"  #{EXCEPTION_MARKER}RuntimeError: omg
1

  The exit status will be 2 if the error is not displayable
$ seeing_is_believing -e 'a='; echo $status
-:1: syntax error, unexpected $end
2

  Run with previous output
$ echo "1+1  #{VALUE_MARKER}old-value" | seeing_is_believing
1+1  #{VALUE_MARKER}2

$ echo "1+1  #{VALUE_MARKER}old-value" | seeing_is_believing --clean
1+1

  If your Ruby binary is named something else (e.g. ruby2.0)
$ ruby2.0 -S seeing_is_believing --shebang ruby2.0 -e '123'
123  #{VALUE_MARKER}123

HELP_SCREEN
end

Instance Method Details

#callObject



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
54
55
56
57
58
# File 'lib/seeing_is_believing/binary/parse_args.rb', line 24

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 '-j', '--json'                then options[:result_as_json]      = true
      when '-g', '--debug'               then options[:debugger]            = Debugger.new(stream: outstream, 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 :max_line_length,    arg
      when '-D', '--result-length'       then extract_positive_int_for :max_result_length,  arg
      when '-n', '--number-of-captures'  then extract_positive_int_for :number_of_captures, 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       '--shebang'             then next_arg("#{arg} expects a ruby executable as the following argument but did not see one") { |executable| options[:shebang]   = executable }
      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