Class: Masticate::MyOptionParser
- Inherits:
-
Object
- Object
- Masticate::MyOptionParser
- Defined in:
- lib/masticate/myoptparse.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #execute(command, options, filenames = nil) ⇒ Object
-
#initialize ⇒ MyOptionParser
constructor
A new instance of MyOptionParser.
- #logmessage(command, options, results) ⇒ Object
- #parse(argv = ARGV) ⇒ Object
- #prepare(command, options) ⇒ Object
Constructor Details
#initialize ⇒ MyOptionParser
Returns a new instance of MyOptionParser.
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 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 |
# File 'lib/masticate/myoptparse.rb', line 6 def initialize @options = {} @parser = OptionParser.new do |opts| opts. = "Usage: masticate [command] [options]" opts.on("--output FILENAME", String, "Redirect output from stdout to file") do |f| @options[:output] = f end opts.on("--format FORMAT", String, "Specify format") do |v| @options[:format] = v end opts.on("--delim DELIMITER", String, "Specify field delimiter (character or TAB; default is ',')") do |v| @options[:col_sep] = v @options[:col_sep] = "\t" if @options[:col_sep] == "TAB" end opts.on("--quote QUOTE-CHAR", String, "Specify character used for quoting fields (optional; default is no quoting)") do |char| @options[:quote_char] = char end opts.on("--stats", "(for *sniff*) collect & display input stats") do @options[:stats] = true end opts.on("--fields LIST", Array, "Specify fields to select") do |list| @options[:fields] = list end opts.on("--field FIELD", String, "Specify field to convert") do |f| @options[:field] = f end # if I specify String here, then a blank string '' is considered invalid and triggers an exception. opts.on("--value VALUE", "(*exclude* only) Value to compare field to for exclusion") do |s| @options[:value] = s end opts.on("--snip DIRECTIVE", String, "Specify header fields to snip: first N, or by name") do |f| @options[:snip] = f.to_i end opts.on("--from REGEXP", String, "Regular expression for gsub conversion") do |s| @options[:from] = s end # if I specify String here, then a blank string '' is considered invalid and triggers an exception. opts.on("--to STRING", "Result string for gsub conversion") do |s| @options[:to] = s end opts.on("--inlined", "(for *mend* only) Source file has headers inlined on each line") do |b| @options[:inlined] = true end opts.on("--dejunk", "(for *mend* only) Expunge junk lines from source") do |b| @options[:dejunk] = true end opts.on("--by FIELD", String, "(for *maxrows* only) Field to group by") do |f| @options[:by] = f end opts.on("--max FIELD", String, "(for *maxrows* only) Field to find max value for") do |f| @options[:max] = f end opts.on("--recipe FILENAME", String, "(*cook* only) Recipe file") do |f| @options[:recipe] = f end opts.on("--rule {downcase,upcase}", String, "(*transform* only) Transformation rule") do |f| @options[:rule] = f end opts.on("--buried FIELD", String, "(*mend* only) Remove embedded delimiters from named field") do |f| @options[:buried] = f end end end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
4 5 6 |
# File 'lib/masticate/myoptparse.rb', line 4 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/masticate/myoptparse.rb', line 4 def @options end |
Instance Method Details
#execute(command, options, filenames = nil) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/masticate/myoptparse.rb', line 112 def execute(command, , filenames = nil) filename = filenames.first case command when 'sniff' results = Masticate.sniff(filename, ) col_sep = results[:col_sep] col_sep = "TAB" if col_sep == "\t" quote_char = results[:quote_char] || "NONE" $stderr.puts <<-EOT Processing complete. Input delimiter: #{col_sep} Quote char: #{quote_char} Field counts: #{results[:field_counts].inspect} Headers: #{results[:headers].join(',')} EOT when 'mend' results = Masticate.mend(filename, ) (command, , results) when 'csvify' results = Masticate.csvify(filename, ) (command, , results) when 'pluck' results = Masticate.pluck(filename, ) (command, , results) when 'datify' results = Masticate.datify(filename, ) (command, , results) when 'gsub' results = Masticate.gsub(filename, ) (command, , results) when 'maxrows' results = Masticate.maxrows(filename, ) (command, , results) when 'concat' results = Masticate.concat(ARGV, ) # logmessage(command, options, results) when 'relabel' results = Masticate.relabel(filename, ) # logmessage(command, options, results) when 'cook' results = Masticate.cook(filename, ) (command, , results) when 'exclude' results = Masticate.exclude(filename, ) (command, , results) when 'include' results = Masticate.include(filename, ) (command, , results) when 'transform' results = Masticate.transform(filename, ) (command, , results) else raise "unknown command #{command}" end end |
#logmessage(command, options, results) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/masticate/myoptparse.rb', line 182 def (command, , results) $stderr.puts <<-EOT * masticate #{command} (#{.keys.join(', ')}) Lines in input: #{results[:input_count]} Lines in output: #{results[:output_count]} Headers: #{results[:headers].inspect} EOT if results[:field_counts] $stderr.puts " Field counts: #{results[:field_counts].inspect}" end end |
#parse(argv = ARGV) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/masticate/myoptparse.rb', line 88 def parse(argv = ARGV) @command = argv.shift filenames = @parser.parse(argv) # argv remnants are filenames [@command, @options, filenames] end |
#prepare(command, options) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/masticate/myoptparse.rb', line 95 def prepare(command, ) klasses = { 'gsub' => Masticate::Gsubber, 'transform' => Masticate::Transform, 'datify' => Masticate::Datify, 'maxrows' => Masticate::MaxRows, 'relabel' => Masticate::Relabel, 'pluck' => Masticate::Plucker, 'exclude' => Masticate::Exclude, 'include' => Masticate::Include, 'mend' => Masticate::Mender } klass = klasses[command] klass.new() end |