Class: Fast::Cli
- Inherits:
-
Object
- Object
- Fast::Cli
- Defined in:
- lib/fast/cli.rb
Overview
Command Line Interface for Fast
Instance Attribute Summary collapse
-
#from_code ⇒ Object
readonly
Returns the value of attribute from_code.
-
#help ⇒ Object
readonly
Returns the value of attribute help.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#pry ⇒ Object
readonly
Returns the value of attribute pry.
-
#show_sexp ⇒ Object
readonly
Returns the value of attribute show_sexp.
-
#similar ⇒ Object
readonly
Returns the value of attribute similar.
Class Method Summary collapse
-
.run!(argv) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize.
Instance Method Summary collapse
- #debug(*info) ⇒ Object
- #debug_mode? ⇒ Boolean
- #expression ⇒ Object
- #files ⇒ Object
-
#initialize(args) ⇒ Cli
constructor
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
- #report(result, file) ⇒ Object
- #run! ⇒ Object
- #search ⇒ Object
- #search_file(file) ⇒ Object
Constructor Details
#initialize(args) ⇒ Cli
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
16 17 18 19 20 21 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 54 55 56 57 58 59 60 |
# File 'lib/fast/cli.rb', line 16 def initialize(args) @opt = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength opts. = 'Usage: fast expression <files> [options]' opts.on('-d', '--debug', 'Debug fast engine') do @debug = true end opts.on('--ast', 'Print AST instead of code') do @show_sexp = true end opts.on('--pry', 'Jump into a pry session with results') do @pry = true end opts.on('-c', '--code', 'Create a pattern from code example') do if @pattern @from_code = true @pattern = Fast.ast(@pattern).to_sexp debug 'Expression from AST:', @pattern end end opts.on('-s', '--similar', 'Search for similar code.') do @similar = true @pattern = Fast.expression_from(Fast.ast(@pattern)) debug "Looking for code similar to #{@pattern}" end opts.on_tail('--version', 'Show version') do puts Fast::VERSION exit end opts.on_tail('-h', '--help', 'Show help. More at https://jonatas.github.io/fast') do @help = true end @pattern, @files = args.reject { |arg| arg.start_with? '-' } end @opt.parse! args @files = [*@files] @files.reject! { |arg| arg.start_with?('-') } end |
Instance Attribute Details
#from_code ⇒ Object (readonly)
Returns the value of attribute from_code.
12 13 14 |
# File 'lib/fast/cli.rb', line 12 def from_code @from_code end |
#help ⇒ Object (readonly)
Returns the value of attribute help.
12 13 14 |
# File 'lib/fast/cli.rb', line 12 def help @help end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
12 13 14 |
# File 'lib/fast/cli.rb', line 12 def pattern @pattern end |
#pry ⇒ Object (readonly)
Returns the value of attribute pry.
12 13 14 |
# File 'lib/fast/cli.rb', line 12 def pry @pry end |
#show_sexp ⇒ Object (readonly)
Returns the value of attribute show_sexp.
12 13 14 |
# File 'lib/fast/cli.rb', line 12 def show_sexp @show_sexp end |
#similar ⇒ Object (readonly)
Returns the value of attribute similar.
12 13 14 |
# File 'lib/fast/cli.rb', line 12 def similar @similar end |
Class Method Details
.run!(argv) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
65 66 67 68 |
# File 'lib/fast/cli.rb', line 65 def self.run!(argv) argv = argv.dup new(argv).run! end |
Instance Method Details
#debug(*info) ⇒ Object
119 120 121 |
# File 'lib/fast/cli.rb', line 119 def debug(*info) puts(info) if debug_mode? end |
#debug_mode? ⇒ Boolean
115 116 117 |
# File 'lib/fast/cli.rb', line 115 def debug_mode? @debug == true end |
#expression ⇒ Object
78 79 80 |
# File 'lib/fast/cli.rb', line 78 def expression Fast.expression(@pattern) end |
#files ⇒ Object
111 112 113 |
# File 'lib/fast/cli.rb', line 111 def files Fast.ruby_files_from(*@files) end |
#report(result, file) ⇒ Object
123 124 125 |
# File 'lib/fast/cli.rb', line 123 def report(result, file) Fast.report(result, file: file, show_sexp: @show_sexp) end |
#run! ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/fast/cli.rb', line 70 def run! if @help || @files.empty? && @pattern.nil? puts @opt.help else search end end |
#search ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fast/cli.rb', line 95 def search files.each do |file| results = search_file(file) next if results.nil? || results.empty? results.each do |result| if @pry require 'pry' binding.pry # rubocop:disable Lint/Debugger else report(result, file) end end end end |
#search_file(file) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fast/cli.rb', line 82 def search_file(file) if debug_mode? Fast.debug { Fast.search_file(expression, file) } else begin Fast.search_file(expression, file) rescue StandardError debug "Ops! An error occurred trying to search in #{expression.inspect} in #{file}", $ERROR_INFO, $ERROR_POSITION [] end end end |