Class: Athena::CLI

Inherits:
Nuggets::CLI
  • Object
show all
Defined in:
lib/athena/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultsObject



40
41
42
43
44
45
46
47
# File 'lib/athena/cli.rb', line 40

def defaults
  super.merge(
    :config => 'config.yaml',
    :input  => '-',
    :output => '-',
    :target => nil
  )
end

Instance Method Details

#run(arguments) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/athena/cli.rb', line 51

def run(arguments)
  spec = options[:spec] || options[:spec_fallback]
  abort "No input format (spec) specified and none could be inferred." unless spec
  abort "Invalid input format (spec): #{spec}. Use `-L' to get a list of available specs." unless Athena.valid_input_format?(spec)

  format = options[:format] || options[:format_fallback]
  abort "No output format specified and none could be inferred." unless format
  abort "Invalid output format: #{format}. Use `-l' to get a list of available formats." unless Athena.valid_output_format?(format)

  if t = options[:target]
    target_config = config[target = t.to_sym]
  else
    [options[:target_fallback] || 'generic', ".#{spec}", ":#{format}"].inject([]) { |s, t|
      s << (s.last ? s.last + t : t)
    }.reverse.find { |t| target_config = config[target = t.to_sym] }
  end or abort "Config not found for target: #{target}."

  input = options[:input]
  input = arguments.shift unless input != defaults[:input] || arguments.empty?
  input = File.directory?(input) ? Dir.open(input) : open_file_or_std(input)

  quit unless arguments.empty?

  Athena.run(target_config, spec, format, input, open_file_or_std(options[:output], true))
end