Class: HSQL::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/hsql/command_line.rb

Overview

Stuff that I’d rather not put directly into the executable goes here. This makes it much easier to test.

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Returns a new instance of CommandLine.



11
12
13
# File 'lib/hsql/command_line.rb', line 11

def initialize
  @options = {}
end

Instance Method Details



57
58
59
60
61
62
63
64
65
66
# File 'lib/hsql/command_line.rb', line 57

def banner
  <<-BANNER
Usage: #{File.basename(__FILE__)} file [environment]

Arguments
  file: Any *.sql file. If it has a YAML header (ending in three hyphens) the metadata will be processed;

Options
BANNER
end

#filenameObject



20
21
22
23
# File 'lib/hsql/command_line.rb', line 20

def filename
  option_parser.parse!
  ARGV.first
end

#helpObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hsql/command_line.rb', line 68

def help
  <<-HELP
TEMPLATE

You can use the Mustache syntax (three curly braces) to interpolate any of your
'data' into your SQL. You specify the data in the YAML header like so:

data:
  production:     # whatever you want to be interpolated into your SQL
    name: Alison  # when you pass 'production' as the environment argument
  development:
    name: Kailey
---
SELECT * FROM users WHERE name = '{{{name}}}'


There are some common date values available to you at all times, without having
to be defined in the YAML header:

#{HSQL::Data.for_humans}

For more details, run:
  open https://github.com/JackDanger/hsql
HELP
end

#option_parserObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hsql/command_line.rb', line 25

def option_parser
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = banner
    opts.on('-d DATE',
            '--date DATE',
            '--timestamp DATE', 'The time that the SQL will consider to be "{{{now}}}".') do |option|
      @options[:timestamp] = Time.parse(option) if option
    end
    opts.on('-e ENV',
            '--env ENV',
            'Which key of the YAML header "data:" key you want to interpolate.') do |option|
      @options[:environment] = option
    end
    opts.on('-y', '--yaml', 'Output just the metadata for this file as YAML') do |_option|
      @options[:meta_only] = 'yaml'
    end
    opts.on('-j', '--json', 'Output just the metadata for this file as JSON') do |_option|
      @options[:meta_only] = 'json'
    end
    opts.on('-v', '--verbose', 'Output debug information') do |_option|
      @options[:verbose] = true
    end
    opts.on_tail('-V', '--version', 'Show the current version of HSQL') do |_option|
      puts HSQL::VERSION
      exit 1
    end
    opts.on('-h', '--help', 'Show the full help documentation') do |_option|
      help
    end
  end
end

#optionsObject



15
16
17
18
# File 'lib/hsql/command_line.rb', line 15

def options
  option_parser.parse!
  @options
end