pry 0.10.3 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +251 -16
  3. data/LICENSE +1 -1
  4. data/README.md +35 -51
  5. data/bin/pry +3 -11
  6. data/lib/pry/basic_object.rb +6 -0
  7. data/lib/pry/cli.rb +50 -52
  8. data/lib/pry/code/code_file.rb +13 -6
  9. data/lib/pry/code/code_range.rb +3 -3
  10. data/lib/pry/code/loc.rb +14 -8
  11. data/lib/pry/code.rb +12 -5
  12. data/lib/pry/code_object.rb +27 -4
  13. data/lib/pry/color_printer.rb +20 -10
  14. data/lib/pry/command.rb +76 -45
  15. data/lib/pry/command_set.rb +17 -45
  16. data/lib/pry/commands/amend_line.rb +3 -4
  17. data/lib/pry/commands/bang.rb +1 -1
  18. data/lib/pry/commands/cat/exception_formatter.rb +10 -8
  19. data/lib/pry/commands/cat/file_formatter.rb +7 -3
  20. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  21. data/lib/pry/commands/cat.rb +7 -6
  22. data/lib/pry/commands/change_prompt.rb +29 -9
  23. data/lib/pry/commands/clear_screen.rb +14 -0
  24. data/lib/pry/commands/code_collector.rb +25 -23
  25. data/lib/pry/commands/easter_eggs.rb +12 -12
  26. data/lib/pry/commands/edit/file_and_line_locator.rb +1 -1
  27. data/lib/pry/commands/edit.rb +15 -10
  28. data/lib/pry/commands/exit.rb +2 -1
  29. data/lib/pry/commands/find_method.rb +12 -14
  30. data/lib/pry/commands/gem_cd.rb +1 -1
  31. data/lib/pry/commands/gem_install.rb +2 -2
  32. data/lib/pry/commands/gem_list.rb +2 -2
  33. data/lib/pry/commands/gem_open.rb +2 -2
  34. data/lib/pry/commands/gem_readme.rb +25 -0
  35. data/lib/pry/commands/gem_search.rb +40 -0
  36. data/lib/pry/commands/gem_stats.rb +83 -0
  37. data/lib/pry/commands/gist.rb +7 -6
  38. data/lib/pry/commands/help.rb +3 -3
  39. data/lib/pry/commands/hist.rb +11 -10
  40. data/lib/pry/commands/import_set.rb +2 -1
  41. data/lib/pry/commands/install_command.rb +7 -6
  42. data/lib/pry/commands/jump_to.rb +7 -7
  43. data/lib/pry/commands/list_inspectors.rb +2 -2
  44. data/lib/pry/commands/ls/constants.rb +14 -3
  45. data/lib/pry/commands/ls/formatter.rb +4 -2
  46. data/lib/pry/commands/ls/globals.rb +0 -2
  47. data/lib/pry/commands/ls/grep.rb +0 -2
  48. data/lib/pry/commands/ls/instance_vars.rb +0 -1
  49. data/lib/pry/commands/ls/jruby_hacks.rb +2 -2
  50. data/lib/pry/commands/ls/local_names.rb +0 -2
  51. data/lib/pry/commands/ls/local_vars.rb +0 -2
  52. data/lib/pry/commands/ls/ls_entity.rb +0 -1
  53. data/lib/pry/commands/ls/methods.rb +0 -3
  54. data/lib/pry/commands/ls/methods_helper.rb +1 -1
  55. data/lib/pry/commands/ls/self_methods.rb +2 -1
  56. data/lib/pry/commands/ls.rb +30 -31
  57. data/lib/pry/commands/play.rb +3 -4
  58. data/lib/pry/commands/pry_backtrace.rb +1 -1
  59. data/lib/pry/commands/raise_up.rb +2 -1
  60. data/lib/pry/commands/reload_code.rb +2 -2
  61. data/lib/pry/commands/ri.rb +9 -4
  62. data/lib/pry/commands/shell_command.rb +36 -9
  63. data/lib/pry/commands/shell_mode.rb +6 -6
  64. data/lib/pry/commands/show_doc.rb +5 -7
  65. data/lib/pry/commands/show_info.rb +41 -20
  66. data/lib/pry/commands/show_source.rb +5 -2
  67. data/lib/pry/commands/stat.rb +1 -1
  68. data/lib/pry/commands/watch_expression/expression.rb +1 -1
  69. data/lib/pry/commands/watch_expression.rb +9 -7
  70. data/lib/pry/commands/whereami.rb +16 -9
  71. data/lib/pry/commands/wtf.rb +15 -2
  72. data/lib/pry/config/behavior.rb +230 -114
  73. data/lib/pry/config/convenience.rb +24 -21
  74. data/lib/pry/config/default.rb +151 -153
  75. data/lib/pry/config/memoization.rb +48 -0
  76. data/lib/pry/config.rb +30 -19
  77. data/lib/pry/core_extensions.rb +15 -4
  78. data/lib/pry/editor.rb +5 -12
  79. data/lib/pry/exceptions.rb +1 -3
  80. data/lib/pry/forwardable.rb +23 -0
  81. data/lib/pry/helpers/base_helpers.rb +197 -110
  82. data/lib/pry/helpers/command_helpers.rb +5 -4
  83. data/lib/pry/helpers/documentation_helpers.rb +3 -2
  84. data/lib/pry/helpers/options_helpers.rb +6 -6
  85. data/lib/pry/helpers/platform.rb +58 -0
  86. data/lib/pry/helpers/table.rb +20 -15
  87. data/lib/pry/helpers/text.rb +82 -74
  88. data/lib/pry/helpers.rb +1 -0
  89. data/lib/pry/history.rb +44 -10
  90. data/lib/pry/hooks.rb +50 -109
  91. data/lib/pry/indent.rb +21 -19
  92. data/lib/pry/input_completer.rb +146 -123
  93. data/lib/pry/input_lock.rb +0 -2
  94. data/lib/pry/last_exception.rb +2 -2
  95. data/lib/pry/method/disowned.rb +3 -1
  96. data/lib/pry/method/patcher.rb +2 -5
  97. data/lib/pry/method/weird_method_locator.rb +21 -11
  98. data/lib/pry/method.rb +44 -38
  99. data/lib/pry/object_path.rb +5 -4
  100. data/lib/pry/output.rb +37 -37
  101. data/lib/pry/pager.rb +195 -181
  102. data/lib/pry/platform.rb +91 -0
  103. data/lib/pry/plugins.rb +27 -8
  104. data/lib/pry/prompt.rb +144 -25
  105. data/lib/pry/pry_class.rb +83 -33
  106. data/lib/pry/pry_instance.rb +94 -59
  107. data/lib/pry/repl.rb +70 -11
  108. data/lib/pry/repl_file_loader.rb +2 -3
  109. data/lib/pry/ring.rb +84 -0
  110. data/lib/pry/rubygem.rb +9 -7
  111. data/lib/pry/slop/LICENSE +20 -0
  112. data/lib/pry/slop/commands.rb +195 -0
  113. data/lib/pry/slop/option.rb +206 -0
  114. data/lib/pry/slop.rb +661 -0
  115. data/lib/pry/terminal.rb +18 -6
  116. data/lib/pry/testable/evalable.rb +15 -0
  117. data/lib/pry/testable/mockable.rb +14 -0
  118. data/lib/pry/testable/pry_tester.rb +73 -0
  119. data/lib/pry/testable/utility.rb +26 -0
  120. data/lib/pry/testable/variables.rb +46 -0
  121. data/lib/pry/testable.rb +70 -0
  122. data/lib/pry/version.rb +1 -1
  123. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +9 -14
  124. data/lib/pry/wrapped_module.rb +22 -21
  125. data/lib/pry.rb +21 -50
  126. metadata +35 -46
  127. data/lib/pry/commands/list_prompts.rb +0 -35
  128. data/lib/pry/commands/simple_prompt.rb +0 -22
  129. data/lib/pry/history_array.rb +0 -121
  130. data/lib/pry/rbx_path.rb +0 -22
  131. data/lib/pry/test/helper.rb +0 -170
data/lib/pry/slop.rb ADDED
@@ -0,0 +1,661 @@
1
+ class Pry::Slop
2
+ require_relative 'slop/option'
3
+ require_relative 'slop/commands'
4
+ include Enumerable
5
+ VERSION = '3.4.0'
6
+
7
+ # The main Error class, all Exception classes inherit from this class.
8
+ class Error < StandardError; end
9
+
10
+ # Raised when an option argument is expected but none are given.
11
+ class MissingArgumentError < Error; end
12
+
13
+ # Raised when an option is expected/required but not present.
14
+ class MissingOptionError < Error; end
15
+
16
+ # Raised when an argument does not match its intended match constraint.
17
+ class InvalidArgumentError < Error; end
18
+
19
+ # Raised when an invalid option is found and the strict flag is enabled.
20
+ class InvalidOptionError < Error; end
21
+
22
+ # Raised when an invalid command is found and the strict flag is enabled.
23
+ class InvalidCommandError < Error; end
24
+
25
+ # Returns a default Hash of configuration options this Slop instance uses.
26
+ DEFAULT_OPTIONS = {
27
+ strict: false,
28
+ help: false,
29
+ banner: nil,
30
+ ignore_case: false,
31
+ autocreate: false,
32
+ arguments: false,
33
+ optional_arguments: false,
34
+ multiple_switches: true,
35
+ longest_flag: 0
36
+ }
37
+
38
+ class << self
39
+ # items - The Array of items to extract options from (default: ARGV).
40
+ # config - The Hash of configuration options to send to Slop.new().
41
+ # block - An optional block used to add options.
42
+ #
43
+ # Examples:
44
+ #
45
+ # Slop.parse(ARGV, :help => true) do
46
+ # on '-n', '--name', 'Your username', :argument => true
47
+ # end
48
+ #
49
+ # Returns a new instance of Slop.
50
+ def parse(items = ARGV, config = {}, &block)
51
+ parse! items.dup, config, &block
52
+ end
53
+
54
+ # items - The Array of items to extract options from (default: ARGV).
55
+ # config - The Hash of configuration options to send to Slop.new().
56
+ # block - An optional block used to add options.
57
+ #
58
+ # Returns a new instance of Slop.
59
+ def parse!(items = ARGV, config = {}, &block)
60
+ config, items = items, ARGV if items.is_a?(Hash) && config.empty?
61
+ slop = Pry::Slop.new config, &block
62
+ slop.parse! items
63
+ slop
64
+ end
65
+
66
+ # Build a Slop object from a option specification.
67
+ #
68
+ # This allows you to design your options via a simple String rather
69
+ # than programatically. Do note though that with this method, you're
70
+ # unable to pass any advanced options to the on() method when creating
71
+ # options.
72
+ #
73
+ # string - The optspec String
74
+ # config - A Hash of configuration options to pass to Slop.new
75
+ #
76
+ # Examples:
77
+ #
78
+ # opts = Slop.optspec(<<-SPEC)
79
+ # ruby foo.rb [options]
80
+ # ---
81
+ # n,name= Your name
82
+ # a,age= Your age
83
+ # A,auth Sign in with auth
84
+ # p,passcode= Your secret pass code
85
+ # SPEC
86
+ #
87
+ # opts.fetch_option(:name).description #=> "Your name"
88
+ #
89
+ # Returns a new instance of Slop.
90
+ def optspec(string, config = {})
91
+ config[:banner], optspec = string.split(/^--+$/, 2) if string[/^--+$/]
92
+ lines = optspec.split("\n").reject(&:empty?)
93
+ opts = Slop.new(config)
94
+
95
+ lines.each do |line|
96
+ opt, description = line.split(' ', 2)
97
+ short, long = opt.split(',').map { |s| s.sub(/\A--?/, '') }
98
+ opt = opts.on(short, long, description)
99
+
100
+ if long && long.end_with?('=')
101
+ long.sub!(/\=$/, '')
102
+ opt.config[:argument] = true
103
+ end
104
+ end
105
+
106
+ opts
107
+ end
108
+ end
109
+
110
+ # The Hash of configuration options for this Slop instance.
111
+ attr_reader :config
112
+
113
+ # The Array of Slop::Option objects tied to this Slop instance.
114
+ attr_reader :options
115
+
116
+ # Create a new instance of Slop and optionally build options via a block.
117
+ #
118
+ # config - A Hash of configuration options.
119
+ # block - An optional block used to specify options.
120
+ def initialize(config = {}, &block)
121
+ @config = DEFAULT_OPTIONS.merge(config)
122
+ @options = []
123
+ @commands = {}
124
+ @trash = []
125
+ @triggered_options = []
126
+ @unknown_options = []
127
+ @callbacks = {}
128
+ @separators = {}
129
+ @runner = nil
130
+
131
+ if block_given?
132
+ block.arity == 1 ? yield(self) : instance_eval(&block)
133
+ end
134
+
135
+ if config[:help]
136
+ on('-h', '--help', 'Display this help message.', tail: true) do
137
+ $stderr.puts help
138
+ end
139
+ end
140
+ end
141
+
142
+ # Is strict mode enabled?
143
+ #
144
+ # Returns true if strict mode is enabled, false otherwise.
145
+ def strict?
146
+ config[:strict]
147
+ end
148
+
149
+ # Set the banner.
150
+ #
151
+ # banner - The String to set the banner.
152
+ def banner=(banner)
153
+ config[:banner] = banner
154
+ end
155
+
156
+ # Get or set the banner.
157
+ #
158
+ # banner - The String to set the banner.
159
+ #
160
+ # Returns the banner String.
161
+ def banner(banner = nil)
162
+ config[:banner] = banner if banner
163
+ config[:banner]
164
+ end
165
+
166
+ # Set the description (used for commands).
167
+ #
168
+ # desc - The String to set the description.
169
+ def description=(desc)
170
+ config[:description] = desc
171
+ end
172
+
173
+ # Get or set the description (used for commands).
174
+ #
175
+ # desc - The String to set the description.
176
+ #
177
+ # Returns the description String.
178
+ def description(desc = nil)
179
+ config[:description] = desc if desc
180
+ config[:description]
181
+ end
182
+
183
+ # Add a new command.
184
+ #
185
+ # command - The Symbol or String used to identify this command.
186
+ # options - A Hash of configuration options (see Slop::new)
187
+ #
188
+ # Returns a new instance of Slop mapped to this command.
189
+ def command(command, options = {}, &block)
190
+ @commands[command.to_s] = Pry::Slop.new(options, &block)
191
+ end
192
+
193
+ # Parse a list of items, executing and gathering options along the way.
194
+ #
195
+ # items - The Array of items to extract options from (default: ARGV).
196
+ # block - An optional block which when used will yield non options.
197
+ #
198
+ # Returns an Array of original items.
199
+ def parse(items = ARGV, &block)
200
+ parse! items.dup, &block
201
+ items
202
+ end
203
+
204
+ # Parse a list of items, executing and gathering options along the way.
205
+ # unlike parse() this method will remove any options and option arguments
206
+ # from the original Array.
207
+ #
208
+ # items - The Array of items to extract options from (default: ARGV).
209
+ # block - An optional block which when used will yield non options.
210
+ #
211
+ # Returns an Array of original items with options removed.
212
+ def parse!(items = ARGV, &block)
213
+ if items.empty? && @callbacks[:empty]
214
+ @callbacks[:empty].each { |cb| cb.call(self) }
215
+ return items
216
+ end
217
+
218
+ if (cmd = @commands[items[0]])
219
+ return cmd.parse! items[1..-1]
220
+ end
221
+
222
+ items.each_with_index do |item, index|
223
+ @trash << index && break if item == '--'
224
+ autocreate(items, index) if config[:autocreate]
225
+ process_item(items, index, &block) unless @trash.include?(index)
226
+ end
227
+ items.reject!.with_index { |item, index| @trash.include?(index) }
228
+
229
+ missing_options = options.select { |opt| opt.required? && opt.count < 1 }
230
+ if missing_options.any?
231
+ raise MissingOptionError,
232
+ "Missing required option(s): #{missing_options.map(&:key).join(', ')}"
233
+ end
234
+
235
+ if @unknown_options.any?
236
+ raise InvalidOptionError, "Unknown options #{@unknown_options.join(', ')}"
237
+ end
238
+
239
+ if @triggered_options.empty? && @callbacks[:no_options]
240
+ @callbacks[:no_options].each { |cb| cb.call(self) }
241
+ end
242
+
243
+ @runner.call(self, items) if @runner.respond_to?(:call)
244
+
245
+ items
246
+ end
247
+
248
+ # Add an Option.
249
+ #
250
+ # objects - An Array with an optional Hash as the last element.
251
+ #
252
+ # Examples:
253
+ #
254
+ # on '-u', '--username=', 'Your username'
255
+ # on :v, :verbose, 'Enable verbose mode'
256
+ #
257
+ # Returns the created instance of Slop::Option.
258
+ def on(*objects, &block)
259
+ option = build_option(objects, &block)
260
+ options << option
261
+ option
262
+ end
263
+ alias option on
264
+ alias opt on
265
+
266
+ # Fetch an options argument value.
267
+ #
268
+ # key - The Symbol or String option short or long flag.
269
+ #
270
+ # Returns the Object value for this option, or nil.
271
+ def [](key)
272
+ option = fetch_option(key)
273
+ option.value if option
274
+ end
275
+ alias get []
276
+
277
+ # Returns a new Hash with option flags as keys and option values as values.
278
+ #
279
+ # include_commands - If true, merge options from all sub-commands.
280
+ def to_hash(include_commands = false)
281
+ hash = Hash[options.map { |opt| [opt.key.to_sym, opt.value] }]
282
+ if include_commands
283
+ @commands.each { |cmd, opts| hash.merge!(cmd.to_sym => opts.to_hash) }
284
+ end
285
+ hash
286
+ end
287
+ alias to_h to_hash
288
+
289
+ # Enumerable interface. Yields each Slop::Option.
290
+ def each(&block)
291
+ options.each(&block)
292
+ end
293
+
294
+ # Specify code to be executed when these options are parsed.
295
+ #
296
+ # callable - An object responding to a call method.
297
+ #
298
+ # yields - The instance of Slop parsing these options
299
+ # An Array of unparsed arguments
300
+ #
301
+ # Example:
302
+ #
303
+ # Slop.parse do
304
+ # on :v, :verbose
305
+ #
306
+ # run do |opts, args|
307
+ # puts "Arguments: #{args.inspect}" if opts.verbose?
308
+ # end
309
+ # end
310
+ def run(callable = nil, &block)
311
+ @runner = callable || block
312
+ unless @runner.respond_to?(:call)
313
+ raise ArgumentError, "You must specify a callable object or a block to #run"
314
+ end
315
+ end
316
+
317
+ # Check for an options presence.
318
+ #
319
+ # Examples:
320
+ #
321
+ # opts.parse %w( --foo )
322
+ # opts.present?(:foo) #=> true
323
+ # opts.present?(:bar) #=> false
324
+ #
325
+ # Returns true if all of the keys are present in the parsed arguments.
326
+ def present?(*keys)
327
+ keys.all? { |key| (opt = fetch_option(key)) && opt.count > 0 }
328
+ end
329
+
330
+ # Override this method so we can check if an option? method exists.
331
+ #
332
+ # Returns true if this option key exists in our list of options.
333
+ def respond_to_missing?(method_name, include_all = false)
334
+ options.any? { |o| o.key == method_name.to_s.chop } || super
335
+ end
336
+
337
+ # Fetch a list of options which were missing from the parsed list.
338
+ #
339
+ # Examples:
340
+ #
341
+ # opts = Slop.new do
342
+ # on :n, :name=
343
+ # on :p, :password=
344
+ # end
345
+ #
346
+ # opts.parse %w[ --name Lee ]
347
+ # opts.missing #=> ['password']
348
+ #
349
+ # Returns an Array of Strings representing missing options.
350
+ def missing
351
+ (options - @triggered_options).map(&:key)
352
+ end
353
+
354
+ # Fetch a Slop::Option object.
355
+ #
356
+ # key - The Symbol or String option key.
357
+ #
358
+ # Examples:
359
+ #
360
+ # opts.on(:foo, 'Something fooey', :argument => :optional)
361
+ # opt = opts.fetch_option(:foo)
362
+ # opt.class #=> Slop::Option
363
+ # opt.accepts_optional_argument? #=> true
364
+ #
365
+ # Returns an Option or nil if none were found.
366
+ def fetch_option(key)
367
+ options.find { |option| [option.long, option.short].include?(clean(key)) }
368
+ end
369
+
370
+ # Fetch a Slop object associated with this command.
371
+ #
372
+ # command - The String or Symbol name of the command.
373
+ #
374
+ # Examples:
375
+ #
376
+ # opts.command :foo do
377
+ # on :v, :verbose, 'Enable verbose mode'
378
+ # end
379
+ #
380
+ # # ruby run.rb foo -v
381
+ # opts.fetch_command(:foo).verbose? #=> true
382
+ def fetch_command(command)
383
+ @commands[command.to_s]
384
+ end
385
+
386
+ # Add a callback.
387
+ #
388
+ # label - The Symbol identifier to attach this callback.
389
+ #
390
+ # Returns nothing.
391
+ def add_callback(label, &block)
392
+ (@callbacks[label] ||= []) << block
393
+ end
394
+
395
+ # Add string separators between options.
396
+ #
397
+ # text - The String text to print.
398
+ def separator(text)
399
+ if @separators[options.size]
400
+ @separators[options.size] << "\n#{text}"
401
+ else
402
+ @separators[options.size] = text
403
+ end
404
+ end
405
+
406
+ # Print a handy Slop help string.
407
+ #
408
+ # Returns the banner followed by available option help strings.
409
+ def to_s
410
+ heads = options.reject(&:tail?)
411
+ tails = (options - heads)
412
+ opts = (heads + tails).select(&:help).map(&:to_s)
413
+ optstr = opts.each_with_index.map { |o, i|
414
+ (str = @separators[i + 1]) ? [o, str].join("\n") : o
415
+ }.join("\n")
416
+
417
+ if @commands.any?
418
+ optstr << "\n" if !optstr.empty?
419
+ optstr << "\nAvailable commands:\n\n"
420
+ optstr << commands_to_help
421
+ optstr << "\n\nSee `<command> --help` for more information on a specific command."
422
+ end
423
+
424
+ banner = config[:banner]
425
+ banner = "Usage: #{File.basename($0, '.*')}#{' [command]' if @commands.any?} [options]" if banner.nil?
426
+ if banner
427
+ "#{banner}\n#{@separators[0] ? "#{@separators[0]}\n" : ''}#{optstr}"
428
+ else
429
+ optstr
430
+ end
431
+ end
432
+ alias help to_s
433
+
434
+ private
435
+
436
+ # Convenience method for present?(:option).
437
+ #
438
+ # Examples:
439
+ #
440
+ # opts.parse %( --verbose )
441
+ # opts.verbose? #=> true
442
+ # opts.other? #=> false
443
+ #
444
+ # Returns true if this option is present. If this method does not end
445
+ # with a ? character it will instead call super().
446
+ def method_missing(method, *args, &block)
447
+ meth = method.to_s
448
+ if meth.end_with?('?')
449
+ meth.chop!
450
+ present?(meth) || present?(meth.gsub('_', '-'))
451
+ else
452
+ super
453
+ end
454
+ end
455
+
456
+ # Process a list item, figure out if it's an option, execute any
457
+ # callbacks, assign any option arguments, and do some sanity checks.
458
+ #
459
+ # items - The Array of items to process.
460
+ # index - The current Integer index of the item we want to process.
461
+ # block - An optional block which when passed will yield non options.
462
+ #
463
+ # Returns nothing.
464
+ def process_item(items, index, &block)
465
+ return unless (item = items[index])
466
+
467
+ option, argument = extract_option(item) if item.start_with?('-')
468
+
469
+ if option
470
+ option.count += 1 unless item.start_with?('--no-')
471
+ option.count += 1 if option.key[0, 3] == "no-"
472
+ @trash << index
473
+ @triggered_options << option
474
+
475
+ if option.expects_argument?
476
+ argument ||= items.at(index + 1)
477
+
478
+ if !argument || argument =~ /\A--?[a-zA-Z][a-zA-Z0-9_-]*\z/
479
+ raise MissingArgumentError, "#{option.key} expects an argument"
480
+ end
481
+
482
+ execute_option(option, argument, index, item)
483
+ elsif option.accepts_optional_argument?
484
+ argument ||= items.at(index + 1)
485
+
486
+ if argument && argument =~ /\A([^\-?]|-\d)+/
487
+ execute_option(option, argument, index, item)
488
+ else
489
+ option.call(nil)
490
+ end
491
+ elsif config[:multiple_switches] && argument
492
+ execute_multiple_switches(option, argument, index)
493
+ else
494
+ option.value = option.count > 0
495
+ option.call(nil)
496
+ end
497
+ else
498
+ @unknown_options << item if strict? && item =~ /\A--?/
499
+ block.call(item) if block && !@trash.include?(index)
500
+ end
501
+ end
502
+
503
+ # Execute an option, firing off callbacks and assigning arguments.
504
+ #
505
+ # option - The Slop::Option object found by #process_item.
506
+ # argument - The argument Object to assign to this option.
507
+ # index - The current Integer index of the object we're processing.
508
+ # item - The optional String item we're processing.
509
+ #
510
+ # Returns nothing.
511
+ def execute_option(option, argument, index, item = nil)
512
+ if !option
513
+ if config[:multiple_switches] && strict?
514
+ raise InvalidOptionError, "Unknown option -#{item}"
515
+ end
516
+
517
+ return
518
+ end
519
+
520
+ if argument
521
+ unless item && item.end_with?("=#{argument}")
522
+ @trash << index + 1 unless option.argument_in_value
523
+ end
524
+ option.value = argument
525
+ else
526
+ option.value = option.count > 0
527
+ end
528
+
529
+ if option.match? && !argument.match(option.config[:match])
530
+ raise InvalidArgumentError, "#{argument} is an invalid argument"
531
+ end
532
+
533
+ option.call(option.value)
534
+ end
535
+
536
+ # Execute a `-abc` type option where a, b and c are all options. This
537
+ # method is only executed if the multiple_switches argument is true.
538
+ #
539
+ # option - The first Option object.
540
+ # argument - The argument to this option. (Split into multiple Options).
541
+ # index - The index of the current item being processed.
542
+ #
543
+ # Returns nothing.
544
+ def execute_multiple_switches(option, argument, index)
545
+ execute_option(option, nil, index)
546
+ argument.split('').each do |key|
547
+ next unless (opt = fetch_option(key))
548
+
549
+ opt.count += 1
550
+ execute_option(opt, nil, index, key)
551
+ end
552
+ end
553
+
554
+ # Extract an option from a flag.
555
+ #
556
+ # flag - The flag key used to extract an option.
557
+ #
558
+ # Returns an Array of [option, argument].
559
+ def extract_option(flag)
560
+ option = fetch_option(flag)
561
+ option ||= fetch_option(flag.downcase) if config[:ignore_case]
562
+ option ||= fetch_option(flag.gsub(/([^-])-/, '\1_'))
563
+
564
+ unless option
565
+ case flag
566
+ when /\A--?([^=]+)=(.+)\z/, /\A-([a-zA-Z])(.+)\z/, /\A--no-(.+)\z/
567
+ option, argument = fetch_option($1), ($2 || false)
568
+ option.argument_in_value = true if option
569
+ end
570
+ end
571
+
572
+ [option, argument]
573
+ end
574
+
575
+ # Autocreate an option on the fly. See the :autocreate Slop config option.
576
+ #
577
+ # items - The Array of items we're parsing.
578
+ # index - The current Integer index for the item we're processing.
579
+ #
580
+ # Returns nothing.
581
+ def autocreate(items, index)
582
+ flag = items[index]
583
+ if !fetch_option(flag) && !@trash.include?(index)
584
+ option = build_option(Array(flag))
585
+ argument = items[index + 1]
586
+ option.config[:argument] = (argument && argument !~ /\A--?/)
587
+ option.config[:autocreated] = true
588
+ options << option
589
+ end
590
+ end
591
+
592
+ # Build an option from a list of objects.
593
+ #
594
+ # objects - An Array of objects used to build this option.
595
+ #
596
+ # Returns a new instance of Slop::Option.
597
+ def build_option(objects, &block)
598
+ config = {}
599
+ config[:argument] = true if @config[:arguments]
600
+ config[:optional_argument] = true if @config[:optional_arguments]
601
+
602
+ if objects.last.is_a?(Hash)
603
+ config.merge!(objects.last)
604
+ objects.pop
605
+ end
606
+ short = extract_short_flag(objects, config)
607
+ long = extract_long_flag(objects, config)
608
+ desc = objects[0].respond_to?(:to_str) ? objects.shift : nil
609
+
610
+ Option.new(self, short, long, desc, config, &block)
611
+ end
612
+
613
+ # Extract the short flag from an item.
614
+ #
615
+ # objects - The Array of objects passed from #build_option.
616
+ # config - The Hash of configuration options built in #build_option.
617
+ def extract_short_flag(objects, config)
618
+ flag = clean(objects.first)
619
+
620
+ if flag.size == 2 && flag.end_with?('=')
621
+ config[:argument] ||= true
622
+ flag.chop!
623
+ end
624
+
625
+ if flag.size == 1
626
+ objects.shift
627
+ flag
628
+ end
629
+ end
630
+
631
+ # Extract the long flag from an item.
632
+ #
633
+ # objects - The Array of objects passed from #build_option.
634
+ # config - The Hash of configuration options built in #build_option.
635
+ def extract_long_flag(objects, config)
636
+ flag = objects.first.to_s
637
+ if flag =~ /\A(?:--?)?[a-zA-Z][a-zA-Z0-9_-]+\=?\??\z/
638
+ config[:argument] ||= true if flag.end_with?('=')
639
+ config[:optional_argument] = true if flag.end_with?('=?')
640
+ objects.shift
641
+ clean(flag).sub(/\=\??\z/, '')
642
+ end
643
+ end
644
+
645
+ # Remove any leading -- characters from a string.
646
+ #
647
+ # object - The Object we want to cast to a String and clean.
648
+ #
649
+ # Returns the newly cleaned String with leading -- characters removed.
650
+ def clean(object)
651
+ object.to_s.sub(/\A--?/, '')
652
+ end
653
+
654
+ def commands_to_help
655
+ padding = 0
656
+ @commands.each { |c, _| padding = c.size if c.size > padding }
657
+ @commands.map do |cmd, opts|
658
+ " #{cmd}#{' ' * (padding - cmd.size)} #{opts.description}"
659
+ end.join("\n")
660
+ end
661
+ end
data/lib/pry/terminal.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  class Pry::Terminal
2
3
  class << self
3
4
  # Return a pair of [rows, columns] which gives the size of the window.
@@ -40,12 +41,22 @@ class Pry::Terminal
40
41
  end
41
42
 
42
43
  def screen_size_according_to_io_console
43
- return if Pry::Helpers::BaseHelpers.jruby?
44
- require 'io/console'
45
- $stdout.winsize if $stdout.tty? and $stdout.respond_to?(:winsize)
46
- rescue LoadError
47
- # They probably don't have the io/console stdlib or the io-console gem.
48
- # We'll keep trying.
44
+ return if Pry::Helpers::Platform.jruby?
45
+
46
+ begin
47
+ require 'io/console'
48
+
49
+ begin
50
+ if $stdout.respond_to?(:tty?) && $stdout.tty? && $stdout.respond_to?(:winsize)
51
+ $stdout.winsize
52
+ end
53
+ rescue Errno::EOPNOTSUPP
54
+ # $stdout is probably a socket, which doesn't support #winsize.
55
+ end
56
+ rescue LoadError
57
+ # They probably don't have the io/console stdlib or the io-console gem.
58
+ # We'll keep trying.
59
+ end
49
60
  end
50
61
 
51
62
  def screen_size_according_to_env
@@ -66,6 +77,7 @@ class Pry::Terminal
66
77
 
67
78
  def screen_size_according_to_ansicon_env
68
79
  return unless ENV['ANSICON'] =~ /\((.*)x(.*)\)/
80
+
69
81
  size = [$2, $1]
70
82
  size if nonzero_column?(size)
71
83
  end
@@ -0,0 +1,15 @@
1
+ module Pry::Testable::Evalable
2
+ def pry_tester(*args, &block)
3
+ if args.length == 0 || args[0].is_a?(Hash)
4
+ args.unshift(Pry.toplevel_binding)
5
+ end
6
+ Pry::Testable::PryTester.new(*args).tap do |t|
7
+ t.singleton_class.class_eval(&block) if block
8
+ end
9
+ end
10
+
11
+ def pry_eval(*eval_strs)
12
+ b = String === eval_strs.first ? Pry.toplevel_binding : Pry.binding_for(eval_strs.shift)
13
+ pry_tester(b).eval(*eval_strs)
14
+ end
15
+ end