# File lib/athena/cli.rb, line 40 def defaults super.merge( :config => 'config.yaml', :input => '-', :output => '-', :target => nil ) end
# 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
# File lib/athena/cli.rb, line 79 def merge_config(args = [defaults]) super end
# File lib/athena/cli.rb, line 83 def opts(opts) opts.on('-c', '--config YAML', "Config file [Default: #{defaults[:config]}#{' (currently not present)' unless File.readable?(defaults[:config])}]") { |config| quit "Can't find config file: #{config}" unless File.readable?(config) options[:config] = config } opts.separator '' opts.on('-i', '--input FILE', "Input file [Default: STDIN]") { |input| options[:input] = input parts = File.basename(input).split('.') options[:spec_fallback] = parts.last.downcase options[:target_fallback] = parts.size > 1 ? parts[0..-2].join('.') : parts.first } opts.on('-s', '--spec SPEC', "Input format (spec) [Default: file extension of <input-file>]") { |spec| options[:spec] = spec.downcase } opts.on('-L', '--list-specs', "List available input formats (specs) and exit") { print_formats(:in) } opts.separator '' opts.on('-o', '--output FILE', "Output file [Default: STDOUT]") { |output| options[:output] = output options[:format_fallback] = output.split('.').last.downcase } opts.on('-f', '--format FORMAT', "Output format [Default: file extension of <output-file>]") { |format| options[:format] = format.downcase } opts.on('-l', '--list-formats', "List available output formats and exit") { print_formats(:out) } opts.separator '' opts.on('-t', '--target ID', "Target whose config to use [Default: <input-file> minus file extension,", "plus '.<spec>', plus ':<format>' (reversely in turn)]") { |target| options[:target] = target } end
# File lib/athena/cli.rb, line 130 def print_formats(direction) puts "Available #{direction}put formats:" Athena.send("#{direction}put_formats").each { |name, klass| line, format = " - #{name}", klass.format line << " (= #{format})" if name != format && Athena.valid_format?(direction, format) puts line } exit end