bin/masticate in masticate-0.0.4 vs bin/masticate in masticate-0.1.0
- old
+ new
@@ -1,42 +1,98 @@
#!/usr/bin/env ruby
require_relative "../lib/masticate"
+require "optparse"
-command, filename = ARGV
+command = ARGV.shift
-case ARGV.shift
+options = {}
+OptionParser.new do |opts|
+ opts.banner = "Usage: example.rb [options]"
+
+ opts.on("--format FORMAT", "Specify format") do |v|
+ options[:format] = v
+ end
+
+ opts.on("--delim DELIMITER", "Specify field delimiter (character or TAB)") do |v|
+ options[:col_sep] = v
+ options[:col_sep] = "\t" if options[:col_sep] == "TAB"
+ end
+
+ opts.on("--fields LIST", Array, "Specify fields to select") do |list|
+ options[:fields] = list
+ end
+
+ opts.on("--field FIELD", "Specify field to convert") do |f|
+ options[:field] = f
+ end
+
+ opts.on("--snip DIRECTIVE", "Specify header fields to snip: first N, or by name") do |f|
+ options[:snip] = f.to_i
+ end
+
+ opts.on("--from REGEXP", "Regular expression for gsub conversion") do |s|
+ options[:from] = s
+ end
+
+ opts.on("--to STRING", "Result string for gsub conversion") do |s|
+ options[:to] = s
+ end
+end.parse!
+
+filename = ARGV.shift # use stdin if no filename provided
+
+case command
when 'sniff'
results = Masticate.sniff(filename)
col_sep = results[:col_sep]
col_sep = "TAB" if col_sep == "\t"
$stderr.puts <<-EOT
Processing complete.
Input delimiter: #{col_sep}
Field counts: #{results[:field_counts].inspect}
+ Headers: #{results[:headers].join(',')}
EOT
when 'mend'
- metadata = Masticate.sniff(filename)
- col_sep = metadata[:col_sep]
- col_sep = "TAB" if col_sep == "\t"
- results = Masticate.mend(filename, metadata)
+ results = Masticate.mend(filename, options)
$stderr.puts <<-EOT
Processing complete.
- Input delimiter: #{col_sep}
- Lines in input: #{results[:input_records]}
- Lines in output: #{results[:output_records]}
+ Lines in input: #{results[:input_count]}
+ Lines in output: #{results[:output_count]}
EOT
when 'csvify'
- metadata = Masticate.sniff(filename)
- results = Masticate.csvify(filename, metadata)
+ results = Masticate.csvify(filename, options)
$stderr.puts <<-EOT
Processing complete.
- Input delimiter: #{metadata[:col_sep]}
Lines in input: #{results[:input_count]}
Lines in output: #{results[:output_count]}
EOT
+
+when 'pluck'
+ results = Masticate.pluck(filename, options)
+ $stderr.puts <<-EOT
+Processing complete.
+ Lines in input: #{results[:input_count]}
+ Lines in output: #{results[:output_count]}
+EOT
+
+when 'datify'
+ results = Masticate.datify(filename, options)
+ $stderr.puts <<-EOT
+Processing complete.
+ Lines in input: #{results[:input_count]}
+ Lines in output: #{results[:output_count]}
+EOT
+
+when 'gsub'
+ results = Masticate.gsub(filename, options)
+# $stderr.puts <<-EOT
+# Processing complete.
+# Lines in input: #{results[:input_count]}
+# Lines in output: #{results[:output_count]}
+# EOT
else
raise "unknown command #{command}"
end