Sha256: 32992aa228ba6ea114b251bcd4b5f1a8edc28470ef3e47d2f2a7e642fbf06500

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'json'
require 'thor'
require 'yaml'
require 'ltsv'

module Xxx2json
  class CLI < Thor

    class_option :file, type: :string, desc: 'input file. default is STDIN'
    def initialize(args = [], options = {}, config = {})
      super(args, options, config)
      @global_options = config[:shell].base.options
      if @global_options['file'].nil?
        return unless File.pipe?(STDIN)
        @buffer = STDIN.read
      else
        @buffer = File.read(@global_options['file'])
      end
      @core = Core.new
    end

    desc "csv", "csv to json"
    option :headers, type: :array, default: nil, desc: 'header'
    def csv
      puts_json @core.convert_csv(@buffer, ',', options['headers'])
    end

    desc "space", "space delimiter to json"
    option :headers, type: :array, default: nil, desc: 'header'
    def space
      puts_json @core.convert_csv(@buffer, ' ', options['headers'])
    end

    desc "tsv", "tsv to json"
    option :headers, type: :array, desc: 'headers'
    def tsv
      puts_json @core.convert_csv(@buffer, "\t", options['headers'])
    end

    desc "yaml", "yaml to json"
    def yaml
      puts_json YAML.load(@buffer)
    end

    desc "ltsv", "ltsv to json"
    def ltsv
      puts_json LTSV.parse(@buffer)
    end

    desc "regexp", "say hello to NAME"
    option :format, type: :string, required: true, desc: 'header'
    def regexp
      puts_json @core.convert_regexp(@buffer, options['format'])
    end

    desc "rss", "rss to json"
    def rss
      puts_json @core.convert_rss(@buffer)
    end

    desc "version", "show version"
    def version
      puts VERSION
    end

    private
    def puts_json(data)
      puts JSON.pretty_generate(data)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xxx2json-0.1.0 lib/xxx2json/cli.rb