Class: FiltriCmd
- Inherits:
-
Object
- Object
- FiltriCmd
- Defined in:
- lib/filtri/command.rb
Class Method Summary collapse
- .parse_opts ⇒ Object
-
.run(rules, input) ⇒ String
Apply rules to the provided input.
- .validate_opts(opts) ⇒ Object
Class Method Details
.parse_opts ⇒ Object
6 7 8 9 10 11 12 13 14 15 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 |
# File 'lib/filtri/command.rb', line 6 def self.parse_opts = {} OptionParser.new do |o| o. = "Usage: filtri [options] [input]" o.separator "" o.separator "Options:" o.on("-r", "--rule STRING", "Single rule") do |rule| if .include?(:rules) [:rules] << rule else [:rules] = [rule] end end o.on("-f", "--file RULE_FILE", "File containing rules") do |rule_files| if .include?(:rule_files) [:rule_files] << rule_files else [:rule_files] = [rule_files] end end o.on( '-v', '--version', 'Version information' ) do puts "version #{Filtri::VERSION}" exit end o.on( '-h', '--help', 'Help' ) do puts o exit end end.parse! [:input] = ARGV unless ARGV.length <= 0 end |
.run(rules, input) ⇒ String
Apply rules to the provided input
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/filtri/command.rb', line 78 def self.run(rules, input) rule_strs = rules[:rules] || [] rule_files = rules[:rule_files] || [] result = [] # Allow for shorthand format of rules upd_rule_strs = rule_strs.reduce([]) do |r,v| if v =~ /^(#{Filtri.valid_rules.join("|")})/ r << v else r << "rule #{v}" end end f = Filtri.new if upd_rule_strs.length > 0 f.add_rule_str(upd_rule_strs.join("\n")) end rule_files.each do |rf| f.load rf end ARGV.replace(input || []) result << f.apply(ARGF.read) result.join("") end |
.validate_opts(opts) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/filtri/command.rb', line 49 def self.validate_opts(opts) unless opts.include?(:rules) || opts.include?(:rule_files) $stderr.puts "Error: provide a rule or a rule file." return false end [:rule_files, :input].each do |f| if opts.include?(f) opts[f].each do |i| unless File.exist?(i) $stderr.puts "Error: file #{i} does not exist." return false end end end end true end |