# frozen_string_literal: true require 'gli' require_relative '../csvpp' require_relative '../csvpp/core_extensions' module CSVPP # CSV++ command line interface. # # @example # exit CSVPP::CLI.run(ARGV) # class CLI using CoreExtensions extend GLI::App CLIENT = FormatsClient.new program_desc 'CSV++ Command Line Interface' desc 'Parse input files' command :parse do |c| c.switch %i[convert-types], default_value: true, negatable: true c.switch %i[open], default_value: true, negatable: true, desc: 'Whether to open the output with the default application' c.flag %i[f format], required: true, desc: 'Format identifier or local file path to a JSON format' c.flag %i[o output], required: true, desc: 'Output file' c.flag %i[s separator], default_value: '|' c.flag %i[open-cmd], default_value: OS.open_cmd c.action do |global_options, options, args| format = if File.exist?(options[:format]) Format.load(options[:format]) else CLIENT.format(options[:format]) end convert_type = options[:'convert-types'] output = options[:output].strip case output when /\.db$/ importer = SqliteImporter.new( format: format, db_path: output ) importer.import(ARGF.read) when /\.json$/ File.open(output, 'w') do |file| file.puts CSVPP.json( input: ARGF.read, format: format, convert_type: convert_type ) end end OS.open(output, open_cmd: options[:'open-cmd']) if options[:open] end end desc 'List formats' command :formats do |c| c.switch %i[web], default: false, desc: 'Whether to view the formats in the web app' c.action do |global_options, options, args| if options[:web] OS.open(CLIENT.base_uri) next end puts CLIENT.formats.map(&:to_s) end end end end