class Athena::CLI
Public Class Methods
defaults()
click to toggle source
Calls superclass method
# File lib/athena/cli.rb 40 def defaults 41 super.merge( 42 :config => 'config.yaml', 43 :input => '-', 44 :output => '-', 45 :target => nil 46 ) 47 end
Public Instance Methods
run(arguments)
click to toggle source
# File lib/athena/cli.rb 51 def run(arguments) 52 spec = options[:spec] || options[:spec_fallback] 53 abort "No input format (spec) specified and none could be inferred." unless spec 54 abort "Invalid input format (spec): #{spec}. Use `-L' to get a list of available specs." unless Athena.valid_input_format?(spec) 55 56 format = options[:format] || options[:format_fallback] 57 abort "No output format specified and none could be inferred." unless format 58 abort "Invalid output format: #{format}. Use `-l' to get a list of available formats." unless Athena.valid_output_format?(format) 59 60 if t = options[:target] 61 target_config = config[target = t.to_sym] 62 else 63 [options[:target_fallback] || 'generic', ".#{spec}", ":#{format}"].inject([]) { |s, t| 64 s << (s.last ? s.last + t : t) 65 }.reverse.find { |t| target_config = config[target = t.to_sym] } 66 end or abort "Config not found for target: #{target}." 67 68 input = options[:input] 69 input = arguments.shift unless input != defaults[:input] || arguments.empty? 70 input = File.directory?(input) ? Dir.open(input) : open_file_or_std(input) 71 72 quit unless arguments.empty? 73 74 Athena.run(target_config, spec, format, input, open_file_or_std(options[:output], true)) 75 end
Private Instance Methods
merge_config(args = [defaults])
click to toggle source
Calls superclass method
# File lib/athena/cli.rb 79 def merge_config(args = [defaults]) 80 super 81 end
opts(opts)
click to toggle source
# File lib/athena/cli.rb 83 def opts(opts) 84 opts.on('-c', '--config YAML', "Config file [Default: #{defaults[:config]}#{' (currently not present)' unless File.readable?(defaults[:config])}]") { |config| 85 quit "Can't find config file: #{config}" unless File.readable?(config) 86 87 options[:config] = config 88 } 89 90 opts.separator '' 91 92 opts.on('-i', '--input FILE', "Input file [Default: STDIN]") { |input| 93 options[:input] = input 94 95 parts = File.basename(input).split('.') 96 options[:spec_fallback] = parts.last.downcase 97 options[:target_fallback] = parts.size > 1 ? parts[0..-2].join('.') : parts.first 98 } 99 100 opts.on('-s', '--spec SPEC', "Input format (spec) [Default: file extension of <input-file>]") { |spec| 101 options[:spec] = spec.downcase 102 } 103 104 opts.on('-L', '--list-specs', "List available input formats (specs) and exit") { 105 print_formats(:in) 106 } 107 108 opts.separator '' 109 110 opts.on('-o', '--output FILE', "Output file [Default: STDOUT]") { |output| 111 options[:output] = output 112 options[:format_fallback] = output.split('.').last.downcase 113 } 114 115 opts.on('-f', '--format FORMAT', "Output format [Default: file extension of <output-file>]") { |format| 116 options[:format] = format.downcase 117 } 118 119 opts.on('-l', '--list-formats', "List available output formats and exit") { 120 print_formats(:out) 121 } 122 123 opts.separator '' 124 125 opts.on('-t', '--target ID', "Target whose config to use [Default: <input-file> minus file extension,", "plus '.<spec>', plus ':<format>' (reversely in turn)]") { |target| 126 options[:target] = target 127 } 128 end
print_formats(direction)
click to toggle source
# File lib/athena/cli.rb 130 def print_formats(direction) 131 puts "Available #{direction}put formats:" 132 133 Athena.send("#{direction}put_formats").each { |name, klass| 134 line, format = " - #{name}", klass.format 135 line << " (= #{format})" if name != format && Athena.valid_format?(direction, format) 136 137 puts line 138 } 139 140 exit 141 end