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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +251 -16
- data/LICENSE +1 -1
- data/README.md +35 -51
- data/bin/pry +3 -11
- data/lib/pry/basic_object.rb +6 -0
- data/lib/pry/cli.rb +50 -52
- data/lib/pry/code/code_file.rb +13 -6
- data/lib/pry/code/code_range.rb +3 -3
- data/lib/pry/code/loc.rb +14 -8
- data/lib/pry/code.rb +12 -5
- data/lib/pry/code_object.rb +27 -4
- data/lib/pry/color_printer.rb +20 -10
- data/lib/pry/command.rb +76 -45
- data/lib/pry/command_set.rb +17 -45
- data/lib/pry/commands/amend_line.rb +3 -4
- data/lib/pry/commands/bang.rb +1 -1
- data/lib/pry/commands/cat/exception_formatter.rb +10 -8
- data/lib/pry/commands/cat/file_formatter.rb +7 -3
- data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
- data/lib/pry/commands/cat.rb +7 -6
- data/lib/pry/commands/change_prompt.rb +29 -9
- data/lib/pry/commands/clear_screen.rb +14 -0
- data/lib/pry/commands/code_collector.rb +25 -23
- data/lib/pry/commands/easter_eggs.rb +12 -12
- data/lib/pry/commands/edit/file_and_line_locator.rb +1 -1
- data/lib/pry/commands/edit.rb +15 -10
- data/lib/pry/commands/exit.rb +2 -1
- data/lib/pry/commands/find_method.rb +12 -14
- data/lib/pry/commands/gem_cd.rb +1 -1
- data/lib/pry/commands/gem_install.rb +2 -2
- data/lib/pry/commands/gem_list.rb +2 -2
- data/lib/pry/commands/gem_open.rb +2 -2
- data/lib/pry/commands/gem_readme.rb +25 -0
- data/lib/pry/commands/gem_search.rb +40 -0
- data/lib/pry/commands/gem_stats.rb +83 -0
- data/lib/pry/commands/gist.rb +7 -6
- data/lib/pry/commands/help.rb +3 -3
- data/lib/pry/commands/hist.rb +11 -10
- data/lib/pry/commands/import_set.rb +2 -1
- data/lib/pry/commands/install_command.rb +7 -6
- data/lib/pry/commands/jump_to.rb +7 -7
- data/lib/pry/commands/list_inspectors.rb +2 -2
- data/lib/pry/commands/ls/constants.rb +14 -3
- data/lib/pry/commands/ls/formatter.rb +4 -2
- data/lib/pry/commands/ls/globals.rb +0 -2
- data/lib/pry/commands/ls/grep.rb +0 -2
- data/lib/pry/commands/ls/instance_vars.rb +0 -1
- data/lib/pry/commands/ls/jruby_hacks.rb +2 -2
- data/lib/pry/commands/ls/local_names.rb +0 -2
- data/lib/pry/commands/ls/local_vars.rb +0 -2
- data/lib/pry/commands/ls/ls_entity.rb +0 -1
- data/lib/pry/commands/ls/methods.rb +0 -3
- data/lib/pry/commands/ls/methods_helper.rb +1 -1
- data/lib/pry/commands/ls/self_methods.rb +2 -1
- data/lib/pry/commands/ls.rb +30 -31
- data/lib/pry/commands/play.rb +3 -4
- data/lib/pry/commands/pry_backtrace.rb +1 -1
- data/lib/pry/commands/raise_up.rb +2 -1
- data/lib/pry/commands/reload_code.rb +2 -2
- data/lib/pry/commands/ri.rb +9 -4
- data/lib/pry/commands/shell_command.rb +36 -9
- data/lib/pry/commands/shell_mode.rb +6 -6
- data/lib/pry/commands/show_doc.rb +5 -7
- data/lib/pry/commands/show_info.rb +41 -20
- data/lib/pry/commands/show_source.rb +5 -2
- data/lib/pry/commands/stat.rb +1 -1
- data/lib/pry/commands/watch_expression/expression.rb +1 -1
- data/lib/pry/commands/watch_expression.rb +9 -7
- data/lib/pry/commands/whereami.rb +16 -9
- data/lib/pry/commands/wtf.rb +15 -2
- data/lib/pry/config/behavior.rb +230 -114
- data/lib/pry/config/convenience.rb +24 -21
- data/lib/pry/config/default.rb +151 -153
- data/lib/pry/config/memoization.rb +48 -0
- data/lib/pry/config.rb +30 -19
- data/lib/pry/core_extensions.rb +15 -4
- data/lib/pry/editor.rb +5 -12
- data/lib/pry/exceptions.rb +1 -3
- data/lib/pry/forwardable.rb +23 -0
- data/lib/pry/helpers/base_helpers.rb +197 -110
- data/lib/pry/helpers/command_helpers.rb +5 -4
- data/lib/pry/helpers/documentation_helpers.rb +3 -2
- data/lib/pry/helpers/options_helpers.rb +6 -6
- data/lib/pry/helpers/platform.rb +58 -0
- data/lib/pry/helpers/table.rb +20 -15
- data/lib/pry/helpers/text.rb +82 -74
- data/lib/pry/helpers.rb +1 -0
- data/lib/pry/history.rb +44 -10
- data/lib/pry/hooks.rb +50 -109
- data/lib/pry/indent.rb +21 -19
- data/lib/pry/input_completer.rb +146 -123
- data/lib/pry/input_lock.rb +0 -2
- data/lib/pry/last_exception.rb +2 -2
- data/lib/pry/method/disowned.rb +3 -1
- data/lib/pry/method/patcher.rb +2 -5
- data/lib/pry/method/weird_method_locator.rb +21 -11
- data/lib/pry/method.rb +44 -38
- data/lib/pry/object_path.rb +5 -4
- data/lib/pry/output.rb +37 -37
- data/lib/pry/pager.rb +195 -181
- data/lib/pry/platform.rb +91 -0
- data/lib/pry/plugins.rb +27 -8
- data/lib/pry/prompt.rb +144 -25
- data/lib/pry/pry_class.rb +83 -33
- data/lib/pry/pry_instance.rb +94 -59
- data/lib/pry/repl.rb +70 -11
- data/lib/pry/repl_file_loader.rb +2 -3
- data/lib/pry/ring.rb +84 -0
- data/lib/pry/rubygem.rb +9 -7
- data/lib/pry/slop/LICENSE +20 -0
- data/lib/pry/slop/commands.rb +195 -0
- data/lib/pry/slop/option.rb +206 -0
- data/lib/pry/slop.rb +661 -0
- data/lib/pry/terminal.rb +18 -6
- data/lib/pry/testable/evalable.rb +15 -0
- data/lib/pry/testable/mockable.rb +14 -0
- data/lib/pry/testable/pry_tester.rb +73 -0
- data/lib/pry/testable/utility.rb +26 -0
- data/lib/pry/testable/variables.rb +46 -0
- data/lib/pry/testable.rb +70 -0
- data/lib/pry/version.rb +1 -1
- data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +9 -14
- data/lib/pry/wrapped_module.rb +22 -21
- data/lib/pry.rb +21 -50
- metadata +35 -46
- data/lib/pry/commands/list_prompts.rb +0 -35
- data/lib/pry/commands/simple_prompt.rb +0 -22
- data/lib/pry/history_array.rb +0 -121
- data/lib/pry/rbx_path.rb +0 -22
- data/lib/pry/test/helper.rb +0 -170
@@ -0,0 +1,195 @@
|
|
1
|
+
class Pry::Slop
|
2
|
+
class Commands
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :config, :commands, :arguments
|
6
|
+
attr_writer :banner
|
7
|
+
|
8
|
+
# Create a new instance of Slop::Commands and optionally build
|
9
|
+
# Slop instances via a block. Any configuration options used in
|
10
|
+
# this method will be the default configuration options sent to
|
11
|
+
# each Slop object created.
|
12
|
+
#
|
13
|
+
# config - An optional configuration Hash.
|
14
|
+
# block - Optional block used to define commands.
|
15
|
+
#
|
16
|
+
# Examples:
|
17
|
+
#
|
18
|
+
# commands = Slop::Commands.new do
|
19
|
+
# on :new do
|
20
|
+
# on '-o', '--outdir=', 'The output directory'
|
21
|
+
# on '-v', '--verbose', 'Enable verbose mode'
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# on :generate do
|
25
|
+
# on '--assets', 'Generate assets', :default => true
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# global do
|
29
|
+
# on '-D', '--debug', 'Enable debug mode', :default => false
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# commands[:new].class #=> Slop
|
34
|
+
# commands.parse
|
35
|
+
#
|
36
|
+
def initialize(config = {}, &block)
|
37
|
+
@config = config
|
38
|
+
@commands = {}
|
39
|
+
@banner = nil
|
40
|
+
@triggered_command = nil
|
41
|
+
|
42
|
+
warn "[DEPRECATED] Slop::Commands is deprecated and will be removed in "\
|
43
|
+
"Slop version 4. Check out http://injekt.github.com/slop/#commands for "\
|
44
|
+
"a new implementation of commands."
|
45
|
+
|
46
|
+
if block_given?
|
47
|
+
block.arity == 1 ? yield(self) : instance_eval(&block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Optionally set the banner for this command help output.
|
52
|
+
#
|
53
|
+
# banner - The String text to set the banner.
|
54
|
+
#
|
55
|
+
# Returns the String banner if one is set.
|
56
|
+
def banner(banner = nil)
|
57
|
+
@banner = banner if banner
|
58
|
+
@banner
|
59
|
+
end
|
60
|
+
|
61
|
+
# Add a Slop instance for a specific command.
|
62
|
+
#
|
63
|
+
# command - A String or Symbol key used to identify this command.
|
64
|
+
# config - A Hash of configuration options to pass to Slop.
|
65
|
+
# block - An optional block used to pass options to Slop.
|
66
|
+
#
|
67
|
+
# Returns the newly created Slop instance mapped to command.
|
68
|
+
def on(command, config = {}, &block)
|
69
|
+
commands[command.to_s] = Slop.new(@config.merge(config), &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Add a Slop instance used when no other commands exist.
|
73
|
+
#
|
74
|
+
# config - A Hash of configuration options to pass to Slop.
|
75
|
+
# block - An optional block used to pass options to Slop.
|
76
|
+
#
|
77
|
+
# Returns the newly created Slop instance mapped to default.
|
78
|
+
def default(config = {}, &block)
|
79
|
+
on('default', config, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Add a global Slop instance.
|
83
|
+
#
|
84
|
+
# config - A Hash of configuration options to pass to Slop.
|
85
|
+
# block - An optional block used to pass options to Slop.
|
86
|
+
#
|
87
|
+
# Returns the newly created Slop instance mapped to global.
|
88
|
+
def global(config = {}, &block)
|
89
|
+
on('global', config, &block)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Fetch the instance of Slop tied to a command.
|
93
|
+
#
|
94
|
+
# key - The String or Symbol key used to locate this command.
|
95
|
+
#
|
96
|
+
# Returns the Slop instance if this key is found, nil otherwise.
|
97
|
+
def [](key)
|
98
|
+
commands[key.to_s]
|
99
|
+
end
|
100
|
+
alias get []
|
101
|
+
|
102
|
+
# Check for a command presence.
|
103
|
+
#
|
104
|
+
# Examples:
|
105
|
+
#
|
106
|
+
# cmds.parse %w( foo )
|
107
|
+
# cmds.present?(:foo) #=> true
|
108
|
+
# cmds.present?(:bar) #=> false
|
109
|
+
#
|
110
|
+
# Returns true if the given key is present in the parsed arguments.
|
111
|
+
def present?(key)
|
112
|
+
key.to_s == @triggered_command
|
113
|
+
end
|
114
|
+
|
115
|
+
# Enumerable interface.
|
116
|
+
def each(&block)
|
117
|
+
@commands.each(&block)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Parse a list of items.
|
121
|
+
#
|
122
|
+
# items - The Array of items to parse.
|
123
|
+
#
|
124
|
+
# Returns the original Array of items.
|
125
|
+
def parse(items = ARGV)
|
126
|
+
parse! items.dup
|
127
|
+
items
|
128
|
+
end
|
129
|
+
|
130
|
+
# Parse a list of items, removing any options or option arguments found.
|
131
|
+
#
|
132
|
+
# items - The Array of items to parse.
|
133
|
+
#
|
134
|
+
# Returns the original Array of items with options removed.
|
135
|
+
def parse!(items = ARGV)
|
136
|
+
if (opts = commands[items[0].to_s])
|
137
|
+
@triggered_command = items.shift
|
138
|
+
execute_arguments! items
|
139
|
+
opts.parse! items
|
140
|
+
execute_global_opts! items
|
141
|
+
else
|
142
|
+
if (opts = commands['default'])
|
143
|
+
opts.parse! items
|
144
|
+
else
|
145
|
+
if config[:strict] && items[0]
|
146
|
+
raise InvalidCommandError, "Unknown command `#{items[0]}`"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
execute_global_opts! items
|
150
|
+
end
|
151
|
+
items
|
152
|
+
end
|
153
|
+
|
154
|
+
# Returns a nested Hash with Slop options and values. See Slop#to_hash.
|
155
|
+
def to_hash
|
156
|
+
Hash[commands.map { |k, v| [k.to_sym, v.to_hash] }]
|
157
|
+
end
|
158
|
+
|
159
|
+
# Returns the help String.
|
160
|
+
def to_s
|
161
|
+
defaults = commands.delete('default')
|
162
|
+
globals = commands.delete('global')
|
163
|
+
helps = commands.reject { |_, v| v.options.none? }
|
164
|
+
if globals && globals.options.any?
|
165
|
+
helps.merge!('Global options' => globals.to_s)
|
166
|
+
end
|
167
|
+
if defaults && defaults.options.any?
|
168
|
+
helps.merge!('Other options' => defaults.to_s)
|
169
|
+
end
|
170
|
+
banner = @banner ? "#{@banner}\n" : ""
|
171
|
+
banner + helps.map { |key, opts| " #{key}\n#{opts}" }.join("\n\n")
|
172
|
+
end
|
173
|
+
alias help to_s
|
174
|
+
|
175
|
+
# Returns the inspection String.
|
176
|
+
def inspect
|
177
|
+
"#<Slop::Commands #{config.inspect} #{commands.values.map(&:inspect)}>"
|
178
|
+
end
|
179
|
+
|
180
|
+
private
|
181
|
+
|
182
|
+
# Returns nothing.
|
183
|
+
def execute_arguments!(items)
|
184
|
+
@arguments = items.take_while { |arg| !arg.start_with?('-') }
|
185
|
+
items.shift @arguments.size
|
186
|
+
end
|
187
|
+
|
188
|
+
# Returns nothing.
|
189
|
+
def execute_global_opts!(items)
|
190
|
+
if (global_opts = commands['global'])
|
191
|
+
global_opts.parse! items
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
class Pry::Slop
|
2
|
+
class Option
|
3
|
+
# The default Hash of configuration options this class uses.
|
4
|
+
DEFAULT_OPTIONS = {
|
5
|
+
argument: false,
|
6
|
+
optional_argument: false,
|
7
|
+
tail: false,
|
8
|
+
default: nil,
|
9
|
+
callback: nil,
|
10
|
+
delimiter: ',',
|
11
|
+
limit: 0,
|
12
|
+
match: nil,
|
13
|
+
optional: true,
|
14
|
+
required: false,
|
15
|
+
as: String,
|
16
|
+
autocreated: false
|
17
|
+
}
|
18
|
+
|
19
|
+
attr_reader :short, :long, :description, :config, :types
|
20
|
+
attr_accessor :count, :argument_in_value
|
21
|
+
|
22
|
+
# Incapsulate internal option information, mainly used to store
|
23
|
+
# option specific configuration data, most of the meat of this
|
24
|
+
# class is found in the #value method.
|
25
|
+
#
|
26
|
+
# slop - The instance of Slop tied to this Option.
|
27
|
+
# short - The String or Symbol short flag.
|
28
|
+
# long - The String or Symbol long flag.
|
29
|
+
# description - The String description text.
|
30
|
+
# config - A Hash of configuration options.
|
31
|
+
# block - An optional block used as a callback.
|
32
|
+
def initialize(slop, short, long, description, config = {}, &block)
|
33
|
+
@slop = slop
|
34
|
+
@short = short
|
35
|
+
@long = long
|
36
|
+
@description = description
|
37
|
+
@config = DEFAULT_OPTIONS.merge(config)
|
38
|
+
@count = 0
|
39
|
+
@callback = block_given? ? block : config[:callback]
|
40
|
+
@value = nil
|
41
|
+
|
42
|
+
@types = {
|
43
|
+
string: proc { |v| v.to_s },
|
44
|
+
symbol: proc { |v| v.to_sym },
|
45
|
+
integer: proc { |v| value_to_integer(v) },
|
46
|
+
float: proc { |v| value_to_float(v) },
|
47
|
+
range: proc { |v| value_to_range(v) },
|
48
|
+
count: proc { |v| @count }
|
49
|
+
}
|
50
|
+
|
51
|
+
if long && long.size > @slop.config[:longest_flag]
|
52
|
+
@slop.config[:longest_flag] = long.size
|
53
|
+
end
|
54
|
+
|
55
|
+
@config.each_key do |key|
|
56
|
+
predicate = :"#{key}?"
|
57
|
+
unless self.class.method_defined? predicate
|
58
|
+
self.class.__send__(:define_method, predicate) { !!@config[key] }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns true if this option expects an argument.
|
64
|
+
def expects_argument?
|
65
|
+
config[:argument] && config[:argument] != :optional
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns true if this option accepts an optional argument.
|
69
|
+
def accepts_optional_argument?
|
70
|
+
config[:optional_argument] || config[:argument] == :optional
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns the String flag of this option. Preferring the long flag.
|
74
|
+
def key
|
75
|
+
long || short
|
76
|
+
end
|
77
|
+
|
78
|
+
# Call this options callback if one exists, and it responds to call().
|
79
|
+
#
|
80
|
+
# Returns nothing.
|
81
|
+
def call(*objects)
|
82
|
+
@callback.call(*objects) if @callback.respond_to?(:call)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Set the new argument value for this option.
|
86
|
+
#
|
87
|
+
# We use this setter method to handle concatenating lists. That is,
|
88
|
+
# when an array type is specified and used more than once, values from
|
89
|
+
# both options will be grouped together and flattened into a single array.
|
90
|
+
def value=(new_value)
|
91
|
+
if config[:as].to_s.downcase == 'array'
|
92
|
+
@value ||= []
|
93
|
+
|
94
|
+
if new_value.respond_to?(:split)
|
95
|
+
@value.concat new_value.split(config[:delimiter], config[:limit])
|
96
|
+
end
|
97
|
+
else
|
98
|
+
@value = new_value
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Fetch the argument value for this option.
|
103
|
+
#
|
104
|
+
# Returns the Object once any type conversions have taken place.
|
105
|
+
def value
|
106
|
+
value = @value.nil? ? config[:default] : @value
|
107
|
+
|
108
|
+
if [true, false, nil].include?(value) && config[:as].to_s != 'count'
|
109
|
+
return value
|
110
|
+
end
|
111
|
+
|
112
|
+
type = config[:as]
|
113
|
+
if type.respond_to?(:call)
|
114
|
+
type.call(value)
|
115
|
+
else
|
116
|
+
if (callable = types[type.to_s.downcase.to_sym])
|
117
|
+
callable.call(value)
|
118
|
+
else
|
119
|
+
value
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Returns the help String for this option.
|
125
|
+
def to_s
|
126
|
+
return config[:help] if config[:help].respond_to?(:to_str)
|
127
|
+
|
128
|
+
out = " #{short ? "-#{short}, " : ' ' * 4}"
|
129
|
+
|
130
|
+
if long
|
131
|
+
out << "--#{long}"
|
132
|
+
size = long.size
|
133
|
+
diff = @slop.config[:longest_flag] - size
|
134
|
+
out << (' ' * (diff + 6))
|
135
|
+
else
|
136
|
+
out << (' ' * (@slop.config[:longest_flag] + 8))
|
137
|
+
end
|
138
|
+
|
139
|
+
"#{out}#{description}"
|
140
|
+
end
|
141
|
+
alias help to_s
|
142
|
+
|
143
|
+
# Returns the String inspection text.
|
144
|
+
def inspect
|
145
|
+
"#<Slop::Option [-#{short} | --#{long}" +
|
146
|
+
"#{'=' if expects_argument?}#{'=?' if accepts_optional_argument?}]" +
|
147
|
+
" (#{description}) #{config.inspect}"
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
# Convert an object to an Integer if possible.
|
153
|
+
#
|
154
|
+
# value - The Object we want to convert to an integer.
|
155
|
+
#
|
156
|
+
# Returns the Integer value if possible to convert, else a zero.
|
157
|
+
def value_to_integer(value)
|
158
|
+
if @slop.strict?
|
159
|
+
begin
|
160
|
+
Integer(value.to_s, 10)
|
161
|
+
rescue ArgumentError
|
162
|
+
raise InvalidArgumentError, "#{value} could not be coerced into Integer"
|
163
|
+
end
|
164
|
+
else
|
165
|
+
value.to_s.to_i
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Convert an object to a Float if possible.
|
170
|
+
#
|
171
|
+
# value - The Object we want to convert to a float.
|
172
|
+
#
|
173
|
+
# Returns the Float value if possible to convert, else a zero.
|
174
|
+
def value_to_float(value)
|
175
|
+
if @slop.strict?
|
176
|
+
begin
|
177
|
+
Float(value.to_s)
|
178
|
+
rescue ArgumentError
|
179
|
+
raise InvalidArgumentError, "#{value} could not be coerced into Float"
|
180
|
+
end
|
181
|
+
else
|
182
|
+
value.to_s.to_f
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# Convert an object to a Range if possible.
|
187
|
+
#
|
188
|
+
# value - The Object we want to convert to a range.
|
189
|
+
#
|
190
|
+
# Returns the Range value if one could be found, else the original object.
|
191
|
+
def value_to_range(value)
|
192
|
+
case value.to_s
|
193
|
+
when /\A(\-?\d+)\z/
|
194
|
+
Range.new($1.to_i, $1.to_i)
|
195
|
+
when /\A(-?\d+?)(\.\.\.?|-|,)(-?\d+)\z/
|
196
|
+
Range.new($1.to_i, $3.to_i, $2 == '...')
|
197
|
+
else
|
198
|
+
if @slop.strict?
|
199
|
+
raise InvalidArgumentError, "#{value} could not be coerced into Range"
|
200
|
+
else
|
201
|
+
value
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|