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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fast/cli.rb', line 46 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.
42 43 44 |
# File 'lib/fast/cli.rb', line 42 def from_code @from_code end |
#help ⇒ Object (readonly)
Returns the value of attribute help.
42 43 44 |
# File 'lib/fast/cli.rb', line 42 def help @help end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
42 43 44 |
# File 'lib/fast/cli.rb', line 42 def pattern @pattern end |
#pry ⇒ Object (readonly)
Returns the value of attribute pry.
42 43 44 |
# File 'lib/fast/cli.rb', line 42 def pry @pry end |
#show_sexp ⇒ Object (readonly)
Returns the value of attribute show_sexp.
42 43 44 |
# File 'lib/fast/cli.rb', line 42 def show_sexp @show_sexp end |
#similar ⇒ Object (readonly)
Returns the value of attribute similar.
42 43 44 |
# File 'lib/fast/cli.rb', line 42 def similar @similar end |
Class Method Details
.run!(argv) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
95 96 97 98 |
# File 'lib/fast/cli.rb', line 95 def self.run!(argv) argv = argv.dup new(argv).run! end |
Instance Method Details
#debug(*info) ⇒ Object
149 150 151 |
# File 'lib/fast/cli.rb', line 149 def debug(*info) puts(info) if debug_mode? end |
#debug_mode? ⇒ Boolean
145 146 147 |
# File 'lib/fast/cli.rb', line 145 def debug_mode? @debug == true end |
#expression ⇒ Object
108 109 110 |
# File 'lib/fast/cli.rb', line 108 def expression Fast.expression(@pattern) end |
#files ⇒ Object
141 142 143 |
# File 'lib/fast/cli.rb', line 141 def files Fast.ruby_files_from(*@files) end |
#report(result, file) ⇒ Object
153 154 155 |
# File 'lib/fast/cli.rb', line 153 def report(result, file) Fast.report(result, file: file, show_sexp: @show_sexp) end |
#run! ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/fast/cli.rb', line 100 def run! if @help || @files.empty? && @pattern.nil? puts @opt.help else search end end |
#search ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/fast/cli.rb', line 125 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
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/fast/cli.rb', line 112 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 |