class Filegen::Options

Commandline parser

Attributes

params[R]

Public Class Methods

new(argv) click to toggle source

Create commandline parser

@param [Array] argv

The array which contains the commandline arguments
# File lib/filegen/options.rb, line 15
def initialize(argv)
  @params = parse_options(argv)

  validate_params
end

Public Instance Methods

data_sources() click to toggle source

The data sources which can be used

@return [Array]

An array of data sources which can be used
# File lib/filegen/options.rb, line 41
def data_sources
  DataSourceBuilder.new(params).sources
end
destination() click to toggle source

Destination for output

@return [IO]

Returns a file handle for the output
# File lib/filegen/options.rb, line 33
def destination
  $stdout
end
source() click to toggle source

Source template

@return [File]

Returns a file handle for the template
# File lib/filegen/options.rb, line 25
def source
  File.new(params.template)
end

Private Instance Methods

empty?() click to toggle source
# File lib/filegen/options.rb, line 100
def empty?
  params.template.nil?
end
erb_template?() click to toggle source
# File lib/filegen/options.rb, line 108
def erb_template?
  # rubocop:disable CaseEquality
  /.erb$/ === File.basename(params.template)
  # rubocop:enable CaseEquality
end
exists?() click to toggle source
# File lib/filegen/options.rb, line 104
def exists?
  File.exist?(params.template)
end
parse_options(argv) click to toggle source

rubocop:disable MethodLength

# File lib/filegen/options.rb, line 52
def parse_options(argv)
  params = OpenStruct.new
  parser = OptionParser.new

  parser.banner = 'Usage: filegen [options] <template>'

  parser.separator ''
  parser.separator 'Specific options:'

  params.data_sources = [:env]
  params.data_source_builders = {}
  params.data_source_builders[:env]  = DataSources::Environment.new

  parser.on('-y', '--yaml-file FILE', 'YAML-file to look for variables') do |f|
    params.yaml_file = f
    params.data_sources << :yaml
    params.data_source_builders[:yaml] = DataSources::Yaml.new(params.yaml_file)
  end

  parser.on('-d', '--data-sources a,b', Array,  'Order for variable lookup: yaml, env (default: env or env,yaml if yaml-file-option is given)') do |l|
    params.data_sources = l.map(&:to_sym)
  end

  parser.separator ''
  parser.separator 'Common options:'

  parser.on_tail('-h', '--help', 'Show this message') do
    $stderr.puts parser
    exit
  end

  parser.on_tail('-v', '--version', 'Show version') do
    $stderr.puts Filegen::VERSION
    exit
  end

  params.template = parser.parse(argv).first

  params
end
validate_params() click to toggle source
# File lib/filegen/options.rb, line 47
def validate_params
  validate_source
end
validate_source() click to toggle source

rubocop:enable MethodLength

# File lib/filegen/options.rb, line 94
def validate_source
  fail Exceptions::TemplateNameIsMissing, 'Template file name is missing.' if empty?
  fail Exceptions::TemplateDoesNotExist, "File \"#{params.template}\" does not exist" unless exists?
  fail Exceptions::TemplateNameIsInvalid, "File \"#{params.template}\" is not a valid erb template: file ending erb" unless erb_template?
end