Class: DBF::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/dbf/cli.rb

Constant Summary collapse

USAGE =
<<~HELP
  usage: dbf [-h|-s|-a|-c|-r] filename
    -h = print this message
    -v = print the DBF gem version
    -s = print summary information
    -a = create an ActiveRecord::Schema
    -r = create a Sequel migration
    -c = export as CSV
HELP

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout: $stdout, stderr: $stderr) ⇒ CLI

Returns a new instance of CLI.



21
22
23
24
25
# File 'lib/dbf/cli.rb', line 21

def initialize(argv, stdout: $stdout, stderr: $stderr)
  @argv = argv.dup
  @stdout = stdout
  @stderr = stderr
end

Class Method Details

.run(argv, stdout: $stdout, stderr: $stderr) ⇒ Object



17
18
19
# File 'lib/dbf/cli.rb', line 17

def self.run(argv, stdout: $stdout, stderr: $stderr)
  new(argv, stdout: stdout, stderr: stderr).run
end

Instance Method Details

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dbf/cli.rb', line 27

def run
  params = OptionParser.new.getopts(@argv, 'h', 's', 'a', 'c', 'r', 'v')

  if params['v']
    print_version
  elsif params['h']
    print_help
  else
    filename = @argv.shift
    return missing_filename unless filename

    action = %w[a r s c].find { |flag| params[flag] }
    case action
    when 'a' then print_ar_schema(filename)
    when 'r' then print_sequel_schema(filename)
    when 's' then print_summary(filename)
    when 'c' then print_csv(filename)
    end
  end
  0
rescue DBF::FileNotFoundError => e
  @stderr.puts "DBF::FileNotFoundError: #{e.message}"
  1
end