Class: CsvTool

Inherits:
Object
  • Object
show all
Defined in:
lib/csvutils/commands/cut.rb,
lib/csvutils/commands/head.rb,
lib/csvutils/commands/stat.rb,
lib/csvutils/commands/split.rb,
lib/csvutils/commands/header.rb

Class Method Summary collapse

Class Method Details

.cut(args) ⇒ Object

command line tools



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/csvutils/commands/cut.rb', line 7

def self.cut( args )

  config = { columns: [] }

  parser = OptionParser.new do |opts|
     opts.banner = "Usage: csvcut [OPTS] source [dest]"

     opts.on("-c", "--columns=COLUMNS", "Name of header columns" ) do |columns|
       config[:columns] = columns.split(/[,|;]/)   ## allow differnt separators
     end

     opts.on("-h", "--help", "Prints this help") do
       puts opts
       exit
     end
  end

  parser.parse!( args )

  ## pp config
  ## pp args

  source = args[0]
  dest   = args[1] || source   ## default to same as source (note: overwrites datafile in place!!!)

  unless args[0]
    puts "** error: arg missing - source filepath required - #{args.inspect}"
    exit 1
  end

  columns = config[:columns]

  CsvUtils.cut( source, *columns, output: dest )
end

.head(args) ⇒ Object

command line tools



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/csvutils/commands/head.rb', line 7

def self.head( args )

  config = { n: 4 }

  parser = OptionParser.new do |opts|
     opts.banner = "Usage: csvhead [OPTS] datafile ..."

     opts.on("-n", "--num=NUM", "Number of rows" ) do |num|
       config[:n] = num.to_i
     end

     opts.on("-h", "--help", "Prints this help") do
       puts opts
       exit
     end
  end

  parser.parse!( args )

  ## pp config
  ## pp args

  args.each do |arg|
    path = arg
    n = config[:n]

    puts "== #{File.basename(path)} (#{File.dirname(path)}) =="
    puts
    CsvUtils.head( path, n: n )
    puts
  end # each arg
end

.header(args) ⇒ Object

command line tools



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/csvutils/commands/header.rb', line 7

def self.header( args )

  config = {}

  parser = OptionParser.new do |opts|
     opts.banner = "Usage: csvheader [OPTS] datafile ..."

     opts.on("-h", "--help", "Prints this help") do
       puts opts
       exit
     end
  end

  parser.parse!( args )

  ## pp config
  ## pp args

  args.each do |arg|
    path = arg

    puts "== #{File.basename(path)} (#{File.dirname(path)}) =="
    puts
    CsvUtils.pp_header( CsvUtils.header( path ) )
    puts
  end # each arg
end

.split(args) ⇒ Object

command line tools



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/csvutils/commands/split.rb', line 7

def self.split( args )

  config = { columns: [] }

  parser = OptionParser.new do |opts|
     opts.banner = "Usage: csvsplit [OPTS] datafile ..."

     opts.on("-c", "--columns=COLUMNS", "Name of header columns" ) do |columns|
       config[:columns] = columns.split(/[,|;]/)   ## allow differnt separators
     end

     opts.on("-h", "--help", "Prints this help") do
       puts opts
       exit
     end
  end

  parser.parse!( args )

  ## pp config
  ## pp args

  args.each do |arg|
    path = arg
    columns = config[:columns]

    puts "== #{File.basename(path)} (#{File.dirname(path)}) =="
    puts
    CsvUtils.split( path, *columns )
    puts
  end
end

.stat(args) ⇒ Object

command line tools



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/csvutils/commands/stat.rb', line 7

def self.stat( args )

  config = { columns: [] }

  parser = OptionParser.new do |opts|
     opts.banner = "Usage: csvstat [OPTS] datafile ..."

     opts.on("-c", "--columns=COLUMNS", "Name of header columns" ) do |columns|
       config[:columns] = columns.split(/[,|;]/)   ## allow differnt separators
     end

     opts.on("-h", "--help", "Prints this help") do
       puts opts
       exit
     end
  end

  parser.parse!( args )

  ## pp config
  ## pp args

  args.each do |arg|
    path = arg
    columns = config[:columns]

    puts "== #{File.basename(path)} (#{File.dirname(path)}) =="
    puts
    CsvUtils.stat( path, *columns )
    puts
  end # each arg
end