From ebccd570133e2578841b4ff4469b239d494c08c8 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Tue, 25 Dec 2012 23:35:17 +0200 Subject: [PATCH] Convert all commands to classes John "banister" Mair describes the following key features of commands as classes: 1. It enables people to extend them by either subclassing or monkeypatching. 2. It enables them to provide their own API, so that for example, the Pry::Command::Edit class could have class methods for people to configure it. Please, note that I didn't touch easter eggs commands. I also prettified some strings (your source code reading experience should vastly improve!). Signed-off-by: Kyrylo Silin --- lib/pry/commands/amend_line.rb | 9 ++++++--- lib/pry/commands/bang.rb | 13 ++++++++----- lib/pry/commands/bang_pry.rb | 9 ++++++--- lib/pry/commands/cat.rb | 5 ++++- lib/pry/commands/cd.rb | 9 ++++++--- lib/pry/commands/disable_pry.rb | 5 ++++- lib/pry/commands/edit.rb | 7 +++++-- lib/pry/commands/edit_method.rb | 7 +++++-- lib/pry/commands/exit.rb | 8 +++++--- lib/pry/commands/exit_all.rb | 10 ++++++---- lib/pry/commands/exit_program.rb | 10 ++++++---- lib/pry/commands/find_method.rb | 12 +++++++----- lib/pry/commands/gem_cd.rb | 5 ++++- lib/pry/commands/gem_install.rb | 7 +++++-- lib/pry/commands/gem_list.rb | 9 ++++++--- lib/pry/commands/gem_open.rb | 7 +++++-- lib/pry/commands/gist.rb | 8 +++++--- lib/pry/commands/help.rb | 7 +++++-- lib/pry/commands/hist.rb | 10 ++++++---- lib/pry/commands/import_set.rb | 11 ++++++++--- lib/pry/commands/install_command.rb | 7 +++++-- lib/pry/commands/jump_to.rb | 9 ++++++--- lib/pry/commands/ls.rb | 9 ++++++--- lib/pry/commands/nesting.rb | 11 +++++++---- lib/pry/commands/play.rb | 8 +++++--- lib/pry/commands/pry_backtrace.rb | 7 +++++-- lib/pry/commands/pry_version.rb | 7 +++++-- lib/pry/commands/raise_up.rb | 7 +++++-- lib/pry/commands/reload_method.rb | 7 +++++-- lib/pry/commands/reset.rb | 11 +++++++---- lib/pry/commands/ri.rb | 7 +++++-- lib/pry/commands/save_file.rb | 7 +++++-- lib/pry/commands/shell_command.rb | 7 +++++-- lib/pry/commands/shell_mode.rb | 8 +++++--- lib/pry/commands/show_command.rb | 7 +++++-- lib/pry/commands/show_doc.rb | 10 ++++++---- lib/pry/commands/show_input.rb | 7 +++++-- lib/pry/commands/show_source.rb | 10 ++++++---- lib/pry/commands/simple_prompt.rb | 7 +++++-- lib/pry/commands/stat.rb | 7 +++++-- lib/pry/commands/switch_to.rb | 9 ++++++--- lib/pry/commands/toggle_color.rb | 7 +++++-- lib/pry/commands/whereami.rb | 8 ++++++-- lib/pry/commands/wtf.rb | 7 +++++-- 44 files changed, 242 insertions(+), 117 deletions(-) diff --git a/lib/pry/commands/amend_line.rb b/lib/pry/commands/amend_line.rb index 9ad17abf..f4bc7845 100644 --- a/lib/pry/commands/amend_line.rb +++ b/lib/pry/commands/amend_line.rb @@ -1,8 +1,9 @@ class Pry - Pry::Commands.create_command(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/) do + class Command::AmendLine < Pry::ClassCommand + match /amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/ group 'Editing' - description "Amend a line of input in multi-line mode." - command_options :interpolate => false, :listing => "amend-line" + description 'Amend a line of input in multi-line mode.' + command_options :interpolate => false, :listing => 'amend-line' banner <<-'BANNER' Amend a line of input in multi-line mode. `amend-line N`, where the N in `amend-line N` represents line to replace. @@ -40,5 +41,7 @@ class Pry run "show-input" end end + + Pry::Commands.add_command(Pry::Command::AmendLine) end diff --git a/lib/pry/commands/bang.rb b/lib/pry/commands/bang.rb index c6a5a338..a73980fe 100644 --- a/lib/pry/commands/bang.rb +++ b/lib/pry/commands/bang.rb @@ -1,13 +1,16 @@ class Pry - Pry::Commands.create_command "!" do + class Command::Bang < Pry::ClassCommand + match '!' group 'Editing' - description "Clear the input buffer. Useful if the parsing process goes " \ - "wrong and you get stuck in the read loop." + description 'Clear the input buffer. Useful if the parsing process goes ' \ + 'wrong and you get stuck in the read loop.' command_options :use_prefix => false def process - output.puts "Input buffer cleared!" - eval_string.replace("") + output.puts 'Input buffer cleared!' + eval_string.replace('') end end + + Pry::Commands.add_command(Pry::Command::Bang) end diff --git a/lib/pry/commands/bang_pry.rb b/lib/pry/commands/bang_pry.rb index a37a05f8..d5fdaca7 100644 --- a/lib/pry/commands/bang_pry.rb +++ b/lib/pry/commands/bang_pry.rb @@ -1,11 +1,14 @@ class Pry - Pry::Commands.create_command "!pry" do + class Command::BangPry < Pry::ClassCommand + match '!pry' group 'Navigating Pry' - description "Start a Pry session on current self; this even works mid " \ - "multi-line expression." + description 'Start a Pry session on current self; this even works mid ' \ + 'multi-line expression.' def process target.pry end end + + Pry::Commands.add_command(Pry::Command::BangPry) end diff --git a/lib/pry/commands/cat.rb b/lib/pry/commands/cat.rb index 84027d51..0f2e733c 100644 --- a/lib/pry/commands/cat.rb +++ b/lib/pry/commands/cat.rb @@ -1,5 +1,6 @@ class Pry - Pry::Commands.create_command "cat" do + class Command::Cat < Pry::ClassCommand + match 'cat' group 'Input and Output' description "Show code from a file, Pry's input buffer, or the last " \ "exception." @@ -175,4 +176,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::Cat) end diff --git a/lib/pry/commands/cd.rb b/lib/pry/commands/cd.rb index 6a76e660..10879430 100644 --- a/lib/pry/commands/cd.rb +++ b/lib/pry/commands/cd.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "cd" do - group "Context" - description "Move into a new context (object or scope)." + class Command::Cd < Pry::ClassCommand + match 'cd' + group 'Context' + description 'Move into a new context (object or scope).' banner <<-BANNER Usage: cd [OPTIONS] [--help] @@ -25,4 +26,6 @@ class Pry _pry_.binding_stack = stack if stack end end + + Pry::Commands.add_command(Pry::Command::Cd) end diff --git a/lib/pry/commands/disable_pry.rb b/lib/pry/commands/disable_pry.rb index dd2d0433..7d187e6a 100644 --- a/lib/pry/commands/disable_pry.rb +++ b/lib/pry/commands/disable_pry.rb @@ -1,5 +1,6 @@ class Pry - Pry::Commands.create_command "disable-pry" do + class Command::DisablePry < Pry::ClassCommand + match 'disable-pry' group 'Navigating Pry' description 'Stops all future calls to pry and exits the current session.' @@ -21,4 +22,6 @@ class Pry _pry_.run_command "exit" end end + + Pry::Commands.add_command(Pry::Command::DisablePry) end diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index 00e41d13..8c658a4c 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -6,9 +6,10 @@ class Pry # the entire source code because an exception may happen anywhere in the # code and there is no way to predict that. So we simply superimpose # everything (admittedly, doing extra job). - Pry::Commands.create_command "edit" do + class Command::Edit < Pry::ClassCommand + match 'edit' group 'Editing' - description "Invoke the default editor on a file." + description 'Invoke the default editor on a file.' banner <<-BANNER Usage: edit [--no-reload|--reload] [--line LINE] [--temp|--ex|FILE[:LINE]|--in N] @@ -175,4 +176,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::Edit) end diff --git a/lib/pry/commands/edit_method.rb b/lib/pry/commands/edit_method.rb index cf5aa820..cfad425b 100644 --- a/lib/pry/commands/edit_method.rb +++ b/lib/pry/commands/edit_method.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "edit-method" do + class Command::EditMethod < Pry::ClassCommand + match 'edit-method' group 'Editing' - description "Edit the source code for a method." + description 'Edit the source code for a method.' banner <<-BANNER Usage: edit-method [OPTIONS] [METH] @@ -177,4 +178,6 @@ class Pry source end end + + Pry::Commands.add_command(Pry::Command::EditMethod) end diff --git a/lib/pry/commands/exit.rb b/lib/pry/commands/exit.rb index 5de6a102..982b2fe3 100644 --- a/lib/pry/commands/exit.rb +++ b/lib/pry/commands/exit.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "exit" do + class Command::Exit < Pry::ClassCommand + match 'exit' group 'Navigating Pry' - description "Pop the previous binding (does NOT exit program). Aliases: quit" + description 'Pop the previous binding (does NOT exit program). Aliases: quit' banner <<-BANNER Usage: exit [OPTIONS] [--help] @@ -38,5 +39,6 @@ class Pry end end - Pry::Commands.alias_command "quit", "exit" + Pry::Commands.add_command(Pry::Command::Exit) + Pry::Commands.alias_command 'quit', 'exit' end diff --git a/lib/pry/commands/exit_all.rb b/lib/pry/commands/exit_all.rb index c31cedaf..52f4327b 100644 --- a/lib/pry/commands/exit_all.rb +++ b/lib/pry/commands/exit_all.rb @@ -1,8 +1,9 @@ class Pry - Pry::Commands.create_command "exit-all" do + class Command::ExitAll < Pry::ClassCommand + match 'exit-all' group 'Navigating Pry' - description "End the current Pry session (popping all bindings) and " \ - "returning to caller. Accepts optional return value. Aliases: !!@" + description 'End the current Pry session (popping all bindings) and ' \ + 'returning to caller. Accepts optional return value. Aliases: !!@' def process # calculate user-given value @@ -16,5 +17,6 @@ class Pry end end - Pry::Commands.alias_command "!!@", "exit-all" + Pry::Commands.add_command(Pry::Command::ExitAll) + Pry::Commands.alias_command '!!@', 'exit-all' end diff --git a/lib/pry/commands/exit_program.rb b/lib/pry/commands/exit_program.rb index 7d52c0ab..50731b05 100644 --- a/lib/pry/commands/exit_program.rb +++ b/lib/pry/commands/exit_program.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "exit-program" do + class Command::ExitProgram < Pry::ClassCommand + match 'exit-program' group 'Navigating Pry' - description "End the current program. Aliases: quit-program, !!!" + description 'End the current program. Aliases: quit-program, !!!' def process Pry.save_history if Pry.config.history.should_save @@ -9,6 +10,7 @@ class Pry end end - Pry::Commands.alias_command "quit-program", "exit-program" - Pry::Commands.alias_command "!!!", "exit-program" + Pry::Commands.add_command(Pry::Command::ExitProgram) + Pry::Commands.alias_command 'quit-program', 'exit-program' + Pry::Commands.alias_command '!!!', 'exit-program' end diff --git a/lib/pry/commands/find_method.rb b/lib/pry/commands/find_method.rb index 3fb85a3c..8a586e91 100644 --- a/lib/pry/commands/find_method.rb +++ b/lib/pry/commands/find_method.rb @@ -1,13 +1,13 @@ class Pry - Pry::Commands.create_command "find-method" do + class Command::FindMethod < Pry::ClassCommand extend Pry::Helpers::BaseHelpers - group "Context" - - options :requires_gem => "ruby18_source_location" if mri_18? + match 'find-method' + group 'Context' + options :requires_gem => 'ruby18_source_location' if mri_18? options :shellwords => false - description "Recursively search for a method within a Class/Module or the current namespace. find-method [-n | -c] METHOD [NAMESPACE]" + description 'Recursively search for a method within a Class/Module or the current namespace. find-method [-n | -c] METHOD [NAMESPACE]' banner <<-BANNER Usage: find-method [-n | -c] METHOD [NAMESPACE] @@ -162,4 +162,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::FindMethod) end diff --git a/lib/pry/commands/gem_cd.rb b/lib/pry/commands/gem_cd.rb index a51598b4..714269bd 100644 --- a/lib/pry/commands/gem_cd.rb +++ b/lib/pry/commands/gem_cd.rb @@ -1,5 +1,6 @@ class Pry - Pry::Commands.create_command "gem-cd" do |gem| + class Command::GemCd < Pry::ClassCommand + match 'gem-cd' group 'Gems' description "Change working directory to specified gem's directory." command_options :argument_required => true @@ -19,4 +20,6 @@ class Pry gem_complete(str) end end + + Pry::Commands.add_command(Pry::Command::GemCd) end diff --git a/lib/pry/commands/gem_install.rb b/lib/pry/commands/gem_install.rb index 1bb4d162..aa524db4 100644 --- a/lib/pry/commands/gem_install.rb +++ b/lib/pry/commands/gem_install.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "gem-install" do |gem| + class Command::GemInstall < Pry::ClassCommand + match 'gem-install' group 'Gems' - description "Install a gem and refresh the gem cache." + description 'Install a gem and refresh the gem cache.' command_options :argument_required => true banner <<-BANNER @@ -30,4 +31,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::GemInstall) end diff --git a/lib/pry/commands/gem_list.rb b/lib/pry/commands/gem_list.rb index faa0aa99..3c82879f 100644 --- a/lib/pry/commands/gem_list.rb +++ b/lib/pry/commands/gem_list.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "gem-list" do |pattern| + class Command::GemList < Pry::ClassCommand + match 'gem-list' group 'Gems' - description "List and search installed gems." + description 'List and search installed gems.' banner <<-BANNER Usage: gem-list [REGEX] @@ -10,7 +11,7 @@ class Pry match the regex. BANNER - def process(pattern=nil) + def process(pattern = nil) pattern = Regexp.compile(pattern || '') gems = gem_list(pattern).group_by(&:name) @@ -27,4 +28,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::GemList) end diff --git a/lib/pry/commands/gem_open.rb b/lib/pry/commands/gem_open.rb index a4e2f7aa..98689ded 100644 --- a/lib/pry/commands/gem_open.rb +++ b/lib/pry/commands/gem_open.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "gem-open" do |gem| + class Command::GemOpen < Pry::ClassCommand + match 'gem-open' group 'Gems' - description "Opens the working directory of the gem in your editor" + description 'Opens the working directory of the gem in your editor' command_options :argument_required => true banner <<-BANNER @@ -21,4 +22,6 @@ class Pry gem_complete(str) end end + + Pry::Commands.add_command(Pry::Command::GemOpen) end diff --git a/lib/pry/commands/gist.rb b/lib/pry/commands/gist.rb index d04c4211..39128e47 100644 --- a/lib/pry/commands/gist.rb +++ b/lib/pry/commands/gist.rb @@ -28,9 +28,10 @@ class Pry end end - Pry::Commands.create_command "gist" do + class Command::Gist < Pry::ClassCommand include Pry::Helpers::DocumentationHelpers + match 'gist' group 'Misc' description Pry::Gist::DESCRIPTION command_options :requires_gem => 'jist', :shellwords => false @@ -192,6 +193,7 @@ class Pry end - Pry::Commands.alias_command "clipit", "gist --clip" - Pry::Commands.alias_command "jist", "gist" + Pry::Commands.add_command(Pry::Command::Gist) + Pry::Commands.alias_command 'clipit', 'gist --clip' + Pry::Commands.alias_command 'jist', 'gist' end diff --git a/lib/pry/commands/help.rb b/lib/pry/commands/help.rb index 9608ee8c..2db1c138 100644 --- a/lib/pry/commands/help.rb +++ b/lib/pry/commands/help.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "help" do |cmd| + class Command::Help < Pry::ClassCommand + match 'help' group 'Help' - description "Show a list of commands. Type `help ` for information about ." + description 'Show a list of commands. Type `help ` for information about .' banner <<-BANNER Usage: help [ COMMAND ] @@ -121,4 +122,6 @@ class Pry [%w(Help Context Editing Introspection Input_and_output Navigating_pry Gems Basic Commands).index(group_name.gsub(' ', '_')) || 99, group_name] end end + + Pry::Commands.add_command(Pry::Command::Help) end diff --git a/lib/pry/commands/hist.rb b/lib/pry/commands/hist.rb index 8131e359..82093d32 100644 --- a/lib/pry/commands/hist.rb +++ b/lib/pry/commands/hist.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "hist" do - group "Editing" - description "Show and replay Readline history. Aliases: history" + class Command::Hist < Pry::ClassCommand + match 'hist' + group 'Editing' + description 'Show and replay Readline history. Aliases: history' banner <<-USAGE Usage: hist @@ -153,5 +154,6 @@ class Pry end end - Pry::Commands.alias_command "history", "hist" + Pry::Commands.add_command(Pry::Command::Hist) + Pry::Commands.alias_command 'history', 'hist' end diff --git a/lib/pry/commands/import_set.rb b/lib/pry/commands/import_set.rb index 348bc95b..472eb3c3 100644 --- a/lib/pry/commands/import_set.rb +++ b/lib/pry/commands/import_set.rb @@ -1,7 +1,10 @@ class Pry - Pry::Commands.create_command "import-set" do - group "Commands" - description "Import a command set." + class Command::ImportSet < Pry::ClassCommand + match 'import-set' + group 'Commands' + # TODO: Provide a better description with examples and a general conception + # of this command. + description 'Import a Pry command set.' def process(command_set_name) raise CommandError, "Provide a command set name" if command_set.nil? @@ -10,4 +13,6 @@ class Pry _pry_.commands.import set end end + + Pry::Commands.add_command(Pry::Command::ImportSet) end diff --git a/lib/pry/commands/install_command.rb b/lib/pry/commands/install_command.rb index 0e5a4983..41c08b5e 100644 --- a/lib/pry/commands/install_command.rb +++ b/lib/pry/commands/install_command.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "install-command" do + class Command::InstallCommand < Pry::ClassCommand + match 'install-command' group 'Commands' - description "Install a disabled command." + description 'Install a disabled command.' banner <<-BANNER Usage: install-command COMMAND @@ -45,4 +46,6 @@ class Pry output.puts "Installation of `#{name}` successful! Type `help #{name}` for information" end end + + Pry::Commands.add_command(Pry::Command::InstallCommand) end diff --git a/lib/pry/commands/jump_to.rb b/lib/pry/commands/jump_to.rb index 9769d6f8..2a489ec9 100644 --- a/lib/pry/commands/jump_to.rb +++ b/lib/pry/commands/jump_to.rb @@ -1,8 +1,9 @@ class Pry - Pry::Commands.create_command "jump-to" do + class Command::JumpTo < Pry::ClassCommand + match 'jump-to' group 'Navigating Pry' - description "Jump to a binding further up the stack, popping all " \ - "bindings below." + description 'Jump to a binding further up the stack, popping all ' \ + 'bindings below.' def process(break_level) break_level = break_level.to_i @@ -20,4 +21,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::JumpTo) end diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index f0aa0167..e97bd33e 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "ls" do - group "Context" - description "Show the list of vars and methods in the current scope." + class Command::Ls < Pry::ClassCommand + match 'ls' + group 'Context' + description 'Show the list of vars and methods in the current scope.' command_options :shellwords => false, :interpolate => false def options(opt) @@ -320,4 +321,6 @@ class Pry text.send(Pry.config.ls.send(:"#{type}_color"), str) end end + + Pry::Commands.add_command(Pry::Command::Ls) end diff --git a/lib/pry/commands/nesting.rb b/lib/pry/commands/nesting.rb index 717e8b17..bf59df4c 100644 --- a/lib/pry/commands/nesting.rb +++ b/lib/pry/commands/nesting.rb @@ -1,11 +1,12 @@ class Pry - Pry::Commands.create_command "nesting" do + class Command::Nesting < Pry::ClassCommand + match 'nesting' group 'Navigating Pry' - description "Show nesting information." + description 'Show nesting information.' def process - output.puts "Nesting status:" - output.puts "--" + output.puts 'Nesting status:' + output.puts '--' _pry_.binding_stack.each_with_index do |obj, level| if level == 0 output.puts "#{level}. #{Pry.view_clip(obj.eval('self'))} (Pry top level)" @@ -15,4 +16,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::Nesting) end diff --git a/lib/pry/commands/play.rb b/lib/pry/commands/play.rb index 577a6414..636154d4 100644 --- a/lib/pry/commands/play.rb +++ b/lib/pry/commands/play.rb @@ -1,9 +1,10 @@ class Pry - Pry::Commands.create_command "play" do + class Command::Play < Pry::ClassCommand include Pry::Helpers::DocumentationHelpers + match 'play' group 'Editing' - description "Play back a string variable or a method or a file as input." + description 'Play back a string variable or a method or a file as input.' banner <<-BANNER Usage: play [OPTIONS] [--help] @@ -115,6 +116,7 @@ class Pry self.content << File.read(_pry_.last_file) self.content = restrict_to_lines(self.content, lines) end - end + + Pry::Commands.add_command(Pry::Command::Play) end diff --git a/lib/pry/commands/pry_backtrace.rb b/lib/pry/commands/pry_backtrace.rb index aa31e1b6..8b9af9ed 100644 --- a/lib/pry/commands/pry_backtrace.rb +++ b/lib/pry/commands/pry_backtrace.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "pry-backtrace" do + class Command::PryBacktrace < Pry::ClassCommand + match 'pry-backtrace' group 'Context' - description "Show the backtrace for the Pry session." + description 'Show the backtrace for the Pry session.' banner <<-BANNER Usage: pry-backtrace [OPTIONS] [--help] @@ -21,4 +22,6 @@ class Pry stagger_output _pry_.backtrace.join("\n") end end + + Pry::Commands.add_command(Pry::Command::PryBacktrace) end diff --git a/lib/pry/commands/pry_version.rb b/lib/pry/commands/pry_version.rb index 90301245..3021b7e4 100644 --- a/lib/pry/commands/pry_version.rb +++ b/lib/pry/commands/pry_version.rb @@ -1,10 +1,13 @@ class Pry - Pry::Commands.create_command "pry-version" do + class Command::Version < Pry::ClassCommand + match 'pry-version' group 'Misc' - description "Show Pry version." + description 'Show Pry version.' def process output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}." end end + + Pry::Commands.add_command(Pry::Command::Version) end diff --git a/lib/pry/commands/raise_up.rb b/lib/pry/commands/raise_up.rb index 0179343f..ed05dc86 100644 --- a/lib/pry/commands/raise_up.rb +++ b/lib/pry/commands/raise_up.rb @@ -1,8 +1,9 @@ class Pry # N.B. using a regular expresion here so that "raise-up 'foo'" does the right thing. - Pry::Commands.create_command(/raise-up(!?\b.*)/) do + class Command::RaiseUp < Pry::ClassCommand + match /raise-up(!?\b.*)/ group 'Context' - description "Raise an exception out of the current pry instance." + description 'Raise an exception out of the current pry instance.' command_options :listing => 'raise-up' banner <<-BANNER @@ -23,4 +24,6 @@ class Pry target.eval("_pry_.raise_up#{captures[0]}") end end + + Pry::Commands.add_command(Pry::Command::RaiseUp) end diff --git a/lib/pry/commands/reload_method.rb b/lib/pry/commands/reload_method.rb index a94202f2..8c84afcb 100644 --- a/lib/pry/commands/reload_method.rb +++ b/lib/pry/commands/reload_method.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "reload-method" do + class Command::ReloadMethod < Pry::ClassCommand + match 'reload-method' group 'Misc' - description "Reload the source file that contains the specified method" + description 'Reload the source file that contains the specified method' def process(meth_name) meth = get_method_or_raise(meth_name, target, {}, :omit_help) @@ -17,4 +18,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::ReloadMethod) end diff --git a/lib/pry/commands/reset.rb b/lib/pry/commands/reset.rb index 3d12504c..5a864dd1 100644 --- a/lib/pry/commands/reset.rb +++ b/lib/pry/commands/reset.rb @@ -1,11 +1,14 @@ class Pry - Pry::Commands.create_command "reset" do + class Command::Reset < Pry::ClassCommand + match 'reset' group 'Context' - description "Reset the REPL to a clean state." + description 'Reset the REPL to a clean state.' def process - output.puts "Pry reset." - exec "pry" + output.puts 'Pry reset.' + exec 'pry' end end + + Pry::Commands.add_command(Pry::Command::Reset) end diff --git a/lib/pry/commands/ri.rb b/lib/pry/commands/ri.rb index b9c69961..1a2d64b6 100644 --- a/lib/pry/commands/ri.rb +++ b/lib/pry/commands/ri.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "ri" do + class Command::Ri < Pry::ClassCommand + match 'ri' group 'Introspection' - description "View ri documentation. e.g `ri Array#each`" + description 'View ri documentation. e.g `ri Array#each`' banner <<-BANNER Usage: ri [spec] @@ -48,4 +49,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::Ri) end diff --git a/lib/pry/commands/save_file.rb b/lib/pry/commands/save_file.rb index 4d0f8dea..f50f3e96 100644 --- a/lib/pry/commands/save_file.rb +++ b/lib/pry/commands/save_file.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "save-file" do + class Command::SaveFile < Pry::ClassCommand + match 'save-file' group 'Input and Output' - description "Export to a file using content from the REPL." + description 'Export to a file using content from the REPL.' banner <<-USAGE Usage: save-file [OPTIONS] [FILE] @@ -96,4 +97,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::SaveFile) end diff --git a/lib/pry/commands/shell_command.rb b/lib/pry/commands/shell_command.rb index 7c07859a..6de87d0c 100644 --- a/lib/pry/commands/shell_command.rb +++ b/lib/pry/commands/shell_command.rb @@ -1,8 +1,9 @@ class Pry - Pry::Commands.create_command(/\.(.*)/) do + class Command::ShellCommand < Pry::ClassCommand + match /\.(.*)/ group 'Input and Output' description "All text following a '.' is forwarded to the shell." - command_options :listing => ".", :use_prefix => false, + command_options :listing => '.', :use_prefix => false, :takes_block => true def process(cmd) @@ -28,4 +29,6 @@ class Pry super + Bond::Rc.files(search.split(" ").last || '') end end + + Pry::Commands.add_command(Pry::Command::ShellCommand) end diff --git a/lib/pry/commands/shell_mode.rb b/lib/pry/commands/shell_mode.rb index 53de16fb..3e760431 100644 --- a/lib/pry/commands/shell_mode.rb +++ b/lib/pry/commands/shell_mode.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "shell-mode" do + class Command::ShellMode < Pry::ClassCommand + match 'shell-mode' group 'Input and Output' - description "Toggle shell mode. Bring in pwd prompt and file completion." + description 'Toggle shell mode. Bring in pwd prompt and file completion.' def process case _pry_.prompt @@ -17,5 +18,6 @@ class Pry end end - Pry::Commands.alias_command "file-mode", "shell-mode" + Pry::Commands.add_command(Pry::Command::ShellMode) + Pry::Commands.alias_command 'file-mode', 'shell-mode' end diff --git a/lib/pry/commands/show_command.rb b/lib/pry/commands/show_command.rb index 81d129c0..a43406c9 100644 --- a/lib/pry/commands/show_command.rb +++ b/lib/pry/commands/show_command.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "show-command" do + class Command::ShowCommand < Pry::ClassCommand + match 'show-command' group 'Introspection' - description "Show the source for CMD." + description 'Show the source for CMD.' def process(*args) target = target() @@ -19,4 +20,6 @@ class Pry render_output opts.banner end end + + Pry::Commands.add_command(Pry::Command::ShowCommand) end diff --git a/lib/pry/commands/show_doc.rb b/lib/pry/commands/show_doc.rb index 23ff011f..df37d5d8 100644 --- a/lib/pry/commands/show_doc.rb +++ b/lib/pry/commands/show_doc.rb @@ -1,12 +1,13 @@ class Pry - Pry::Commands.create_command "show-doc" do + class Command::ShowDoc < Pry::ClassCommand include Pry::Helpers::DocumentationHelpers extend Pry::Helpers::BaseHelpers + match 'show-doc' group 'Introspection' - description "Show the documentation for a method or class. Aliases: \?" + description 'Show the documentation for a method or class. Aliases: \?' command_options :shellwords => false - command_options :requires_gem => "ruby18_source_location" if mri_18? + command_options :requires_gem => 'ruby18_source_location' if mri_18? banner <<-BANNER Usage: show-doc [OPTIONS] [METH] @@ -174,5 +175,6 @@ class Pry end end - Pry::Commands.alias_command "?", "show-doc" + Pry::Commands.add_command(Pry::Command::ShowDoc) + Pry::Commands.alias_command '?', 'show-doc' end diff --git a/lib/pry/commands/show_input.rb b/lib/pry/commands/show_input.rb index 6ae5175d..4f8dbf1d 100644 --- a/lib/pry/commands/show_input.rb +++ b/lib/pry/commands/show_input.rb @@ -1,10 +1,13 @@ class Pry - Pry::Commands.create_command "show-input" do + class Command::ShowInput < Pry::ClassCommand + match 'show-input' group 'Editing' - description "Show the contents of the input buffer for the current multi-line expression." + description 'Show the contents of the input buffer for the current multi-line expression.' def process output.puts Code.new(eval_string).with_line_numbers end end + + Pry::Commands.add_command(Pry::Command::ShowInput) end diff --git a/lib/pry/commands/show_source.rb b/lib/pry/commands/show_source.rb index f9f970f2..619c4798 100644 --- a/lib/pry/commands/show_source.rb +++ b/lib/pry/commands/show_source.rb @@ -1,9 +1,10 @@ class Pry - Pry::Commands.create_command "show-source" do + class Command::ShowSource < Pry::ClassCommand extend Pry::Helpers::BaseHelpers + match 'show-source' group 'Introspection' - description "Show the source for a method or class. Aliases: $, show-method" + description 'Show the source for a method or class. Aliases: $, show-method' banner <<-BANNER Usage: show-source [OPTIONS] [METH|CLASS] @@ -130,6 +131,7 @@ class Pry end end - Pry::Commands.alias_command "show-method", "show-source" - Pry::Commands.alias_command "$", "show-source" + Pry::Commands.add_command(Pry::Command::ShowSource) + Pry::Commands.alias_command 'show-method', 'show-source' + Pry::Commands.alias_command '$', 'show-source' end diff --git a/lib/pry/commands/simple_prompt.rb b/lib/pry/commands/simple_prompt.rb index 7efbd16c..42911afe 100644 --- a/lib/pry/commands/simple_prompt.rb +++ b/lib/pry/commands/simple_prompt.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "simple-prompt" do + class Command::SimplePrompt < Pry::ClassCommand + match 'simple-prompt' group 'Misc' - description "Toggle the simple prompt." + description 'Toggle the simple prompt.' def process case _pry_.prompt @@ -12,4 +13,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::SimplePrompt) end diff --git a/lib/pry/commands/stat.rb b/lib/pry/commands/stat.rb index 86c83cbd..4c1005d8 100644 --- a/lib/pry/commands/stat.rb +++ b/lib/pry/commands/stat.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command "stat" do + class Command::Stat < Pry::ClassCommand + match 'stat' group 'Introspection' - description "View method information and set _file_ and _dir_ locals." + description 'View method information and set _file_ and _dir_ locals.' command_options :shellwords => false banner <<-BANNER @@ -32,4 +33,6 @@ class Pry EOS end end + + Pry::Commands.add_command(Pry::Command::Stat) end diff --git a/lib/pry/commands/switch_to.rb b/lib/pry/commands/switch_to.rb index 10d9adf6..a1fc481e 100644 --- a/lib/pry/commands/switch_to.rb +++ b/lib/pry/commands/switch_to.rb @@ -1,8 +1,9 @@ class Pry - Pry::Commands.create_command "switch-to" do + class Command::SwitchTo < Pry::ClassCommand + match 'switch-to' group 'Navigating Pry' - description "Start a new sub-session on a binding in the current stack " \ - "(numbered by nesting)." + description 'Start a new subsession on a binding in the current stack ' \ + '(numbered by nesting).' def process(selection) selection = selection.to_i @@ -14,4 +15,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::SwitchTo) end diff --git a/lib/pry/commands/toggle_color.rb b/lib/pry/commands/toggle_color.rb index 9c34a783..85bbe8e5 100644 --- a/lib/pry/commands/toggle_color.rb +++ b/lib/pry/commands/toggle_color.rb @@ -1,11 +1,14 @@ class Pry - Pry::Commands.create_command "toggle-color" do + class Command::ToggleColor < Pry::ClassCommand + match 'toggle-color' group 'Misc' - description "Toggle syntax highlighting." + description 'Toggle syntax highlighting.' def process Pry.color = !Pry.color output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}" end end + + Pry::Commands.add_command(Pry::Command::ToggleColor) end diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index 8f922043..17d75d01 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -1,7 +1,9 @@ class Pry - Pry::Commands.create_command "whereami" do - description "Show code surrounding the current context." + class Command::Whereami < Pry::ClassCommand + match 'whereami' + description 'Show code surrounding the current context.' group 'Context' + banner <<-BANNER Usage: whereami [-q] [N] @@ -84,4 +86,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::Whereami) end diff --git a/lib/pry/commands/wtf.rb b/lib/pry/commands/wtf.rb index a01efda6..214c70ad 100644 --- a/lib/pry/commands/wtf.rb +++ b/lib/pry/commands/wtf.rb @@ -1,7 +1,8 @@ class Pry - Pry::Commands.create_command(/wtf([?!]*)/) do + class Command::Wtf < Pry::ClassCommand + match /wtf([?!]*)/ group 'Context' - description "Show the backtrace of the most recent exception" + description 'Show the backtrace of the most recent exception' options :listing => 'wtf?' banner <<-BANNER @@ -35,4 +36,6 @@ class Pry end end end + + Pry::Commands.add_command(Pry::Command::Wtf) end