class Unparser::CLI
Unparser CLI implementation
def self.run(*arguments)
- Api: - private
Returns:
-
(Integer)
-
Parameters:
-
arguments
(Array
) --
def self.run(*arguments) new(*arguments).exit_status end
def add_options(builder)
- Api: - private
Returns:
-
(undefined)
-
Parameters:
-
builder
(OptionParser
) --
def add_options(builder) builder.banner = 'usage: unparse [options] FILE [FILE]' builder.separator('') builder.on('-e', '--evaluate SOURCE') do |source| @targets << Target::String.new(source) end builder.on('--start-with FILE') do |path| @start_with = targets(path).first end builder.on('-v', '--verbose') do @verbose = true end builder.on('-l', '--literal') do @validation = :literal_validation end builder.on('--ignore-original-syntax-error') do @ignore_original_syntax_error = true end builder.on('--ignore FILE') do |file| @ignore.merge(targets(file)) end builder.on('--fail-fast') do @fail_fast = true end end
def effective_targets
def effective_targets if @start_with reject = true @targets.reject do |targets| if reject && targets.eql?(@start_with) reject = false end reject end else @targets end.reject(&@ignore.method(:include?)) end
def exit_status
- Api: - private
Returns:
-
(Integer)
-
def exit_status effective_targets.each do |target| process_target(target) break if @fail_fast && !@success end @success ? EXIT_SUCCESS : EXIT_FAILURE end
def ignore_original_syntax_error?(validation)
def ignore_original_syntax_error?(validation) @ignore_original_syntax_error && validation.original_node.from_left do nil end.instance_of?(Parser::SyntaxError) end
def initialize(arguments)
- Api: - private
Returns:
-
(undefined)
-
Parameters:
-
arguments
(Array
) --
def initialize(arguments) @ignore = Set.new @targets = [] @fail_fast = false @start_with = nil @success = true @validation = :validation @verbose = false @ignore_original_syntax_error = false opts = OptionParser.new do |builder| add_options(builder) end opts.parse!(arguments).each do |name| @targets.concat(targets(name)) end end
def process_target(target)
def process_target(target) validation = target.public_send(@validation) if validation.success? puts validation.report if @verbose puts "Success: #{validation.identification}" elsif ignore_original_syntax_error?(validation) exception = validation.original_node.from_left puts "#{exception.class}: #{validation.identification} #{exception}" else puts validation.report puts "Error: #{validation.identification}" @success = false end end
def targets(file_name)
def targets(file_name) if File.directory?(file_name) Dir.glob(File.join(file_name, '**/*.rb')) elsif File.file?(file_name) [file_name] else Dir.glob(file_name) end.map { |file| Target::Path.new(Pathname.new(file)) } end