Method: Rsmart::ETL.parse_csv_command_line_options

Defined in:
lib/rsmart_toolbox/etl.rb

.parse_csv_command_line_options(executable, args, opt = { csv_options: { headers: :first_row, header_converters: :symbol, skip_blanks: true, col_sep: ",", quote_char: '"' } }) ⇒ Hash

Parse common command line options for CSV –> SQL transformations.

Examples:

The most common usage:

opt = Rsmart::ETL.parse_csv_command_line_options (File.basename $0), ARGF.argv

Parameters:

  • executable (String)

    the name of the script from which we are executing. See example.

  • args (Array<String>)

    the command line args.

  • opt (Hash) (defaults to: { csv_options: { headers: :first_row, header_converters: :symbol, skip_blanks: true, col_sep: ",", quote_char: '"' } })

    a customizable set of options

Options Hash (opt):

  • :csv_filename (String)

    the input file from which the CSV will be read. Defaults to the first element of args Array.

  • :sql_filename (String)

    the output file to which the SQL will be written.

  • :csv_options (Hash)

    the options that will be used by the CSV parser.

Returns:

  • (Hash)

    a Hash containing the parsed command line results.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/rsmart_toolbox/etl.rb', line 367

def self.parse_csv_command_line_options(
    executable, args, opt={ csv_options: { headers: :first_row,
                                           header_converters: :symbol,
                                           skip_blanks: true,
                                           col_sep: ",",
                                           quote_char: '"'
                                           }
                            } )
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: #{executable} [options] csv_file"
    opts.on( '-o' ,'--output SQL_FILE_OUTPUT', 'The file the SQL data will be writen to... (defaults to <csv_file>.sql)') do |f|
      opt[:sql_filename] = f
    end
    opts.on( '-s' ,'--separator SEPARATOR_CHARACTER', 'The character that separates each column of the CSV file.') do |s|
      opt[:csv_options][:col_sep] = s
    end
    opts.on( '-q' ,'--quote QUOTE_CHARACTER', 'The character used to quote fields.') do |q|
      opt[:csv_options][:quote_char] = q
    end
    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit 1
    end

    opt[:csv_filename] = args[0] unless opt[:csv_filename]
    if opt[:csv_filename].nil? || opt[:csv_filename].empty?
      puts opts
      exit 1
    end
  end
  optparse.parse!

  # construct a sensible default ouptput filename
  unless opt[:sql_filename]
    file_extension = File.extname opt[:csv_filename]
    dir_name = File.dirname opt[:csv_filename]
    base_name = File.basename opt[:csv_filename], file_extension
    opt[:sql_filename] = "#{dir_name}/#{base_name}.sql"
  end

  return opt
end