Sha256: 51fb506b5d8a7ad5d8bd385b3ef81881b9a677b73239d69c018dfbf80d8d9eb0

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

#!/usr/bin/env ruby

require "rubygems"
require 'optparse'
require 'ostruct'
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'csv2json.rb') # this form is important for local development

module CSV2JSONRunner
    
    # command-line parsing
    COMMAND = File.basename($0)
    USAGE = "Usage: #{COMMAND} [INPUT] [OPTIONS]"

    options = OpenStruct.new
    options.output = "-"
    options.separator = ","

    opts = OptionParser.new do |o|
        o.banner = USAGE
        o.separator ""
        o.separator "Specific options:"

        o.on("-s", "--separator SEP", "Set separator character surrounded by single quotes (default is ',')") do |sep|
            options.separator = sep
        end

        o.on("-o", "--output FILE", "Write output to a file") do |fn|
            options.output = fn
        end

        o.on_tail("-h", "--help", "Show this message") do
            puts o
            exit
        end

        o.on_tail("-v", "--version", "Show version") do
            puts CSV2JSON::VERSION
            exit
        end
    end

    begin
        opts.parse!(ARGV)
    rescue
        die "Unable to parse options: #{$!}"
    end

    # initialize output handle
    if options.output == "-"
        OUT = $stdout.clone
    else
        OUT = File.open(options.output, "w")
    end
    
    if ARGV.size > 0
        IN = File.open(ARGV[0], "r")
    else
        IN = StringIO.new($stdin.read) # cannot be just $stdin.clone because FasterCSV is seeking in file :-(
    end
    
    # run the command
    CSV2JSON.parse(IN, OUT, nil, :col_sep => options.separator)
    
    # leave in peace
    OUT.flush
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv2json-0.2.0 bin/csv2json