Class: AdHocTemplate::CommandLineInterface
- Inherits:
-
Object
- Object
- AdHocTemplate::CommandLineInterface
- Defined in:
- lib/ad_hoc_template/command_line_interface.rb
Constant Summary collapse
- TAG_RE_TO_TYPE =
{ /\Ad(efault)?/i => :default, /\Ac(urly_brackets)?/i => :curly_brackets, /\As(quare_brackets)?/i => :square_brackets, /\Axml_like1/i => :xml_like1, /\Axml_like2/i => :xml_like2, }
- FORMAT_RE_TO_FORMAT =
{ /\Ad(efault)?/i => :default, /\Ay(a?ml)?/i => :yaml, /\Aj(son)?/i => :json, /\Ac(sv)?/i => :csv, /\At(sv)?/i => :tsv, }
- FILE_EXTENTIONS =
{ /\.ya?ml\Z/i => :yaml, /\.json\Z/i => :json, /\.csv\Z/i => :csv, /\.tsv\Z/i => :tsv, }
Instance Attribute Summary collapse
-
#data_format ⇒ Object
Returns the value of attribute data_format.
-
#output_filename ⇒ Object
Returns the value of attribute output_filename.
-
#record_data ⇒ Object
Returns the value of attribute record_data.
-
#tag_type ⇒ Object
Returns the value of attribute tag_type.
-
#template_data ⇒ Object
Returns the value of attribute template_data.
Instance Method Summary collapse
- #convert ⇒ Object
- #execute ⇒ Object
-
#initialize ⇒ CommandLineInterface
constructor
A new instance of CommandLineInterface.
- #open_output ⇒ Object
- #parse_command_line_options ⇒ Object
- #read_input_files ⇒ Object
- #set_encoding(given_opt) ⇒ Object
Constructor Details
#initialize ⇒ CommandLineInterface
Returns a new instance of CommandLineInterface.
33 34 35 36 37 38 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 33 def initialize @tag_formatter = AdHocTemplate::DefaultTagFormatter.new @output_filename = nil @tag_type = :default @data_format = nil end |
Instance Attribute Details
#data_format ⇒ Object
Returns the value of attribute data_format.
8 9 10 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8 def data_format @data_format end |
#output_filename ⇒ Object
Returns the value of attribute output_filename.
8 9 10 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8 def output_filename @output_filename end |
#record_data ⇒ Object
Returns the value of attribute record_data.
8 9 10 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8 def record_data @record_data end |
#tag_type ⇒ Object
Returns the value of attribute tag_type.
8 9 10 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8 def tag_type @tag_type end |
#template_data ⇒ Object
Returns the value of attribute template_data.
8 9 10 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8 def template_data @template_data end |
Instance Method Details
#convert ⇒ Object
93 94 95 96 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 93 def convert AdHocTemplate.convert(@record_data, @template_data, @tag_type, @data_format, @tag_formatter) end |
#execute ⇒ Object
108 109 110 111 112 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 108 def execute read_input_files open_output {|out| out.print convert } end |
#open_output ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 98 def open_output if @output_filename open(@output_filename, "wb") do |out| yield out end else yield STDOUT end end |
#parse_command_line_options ⇒ Object
46 47 48 49 50 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 76 77 78 79 80 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 46 def OptionParser.new("USAGE: #{File.basename($0)} [OPTION]... TEMPLATE_FILE DATA_FILE") do |opt| opt.on("-E [ex[:in]]", "--encoding [=ex[:in]]", "Specify the default external and internal character encodings (same as the option of MRI") do |given_opt| self.set_encoding(given_opt) end opt.on("-o [output_file]", "--output [=output_file]", "Save the result into the specified file.") do |output_file| @output_filename = File.(output_file) end opt.on("-t [tag_type]", "--tag-type [=tag_type]", "Choose a template tag type: default, curly_brackets or square_brackets") do |given_type| choose_tag_type(given_type) end opt.on("-d [data_format]", "--data-format [=data_format]", "Specify the format of input data: default, yaml, json, csv or tsv") do |data_format| choose_data_format(data_format) end opt.on("-u [tag_config.yaml]","--user-defined-tag [=tag_config.yaml]", "Configure a user-defined tag. The configuration file is in YAML format.") do |tag_config_yaml| register_user_defined_tag_type(tag_config_yaml) end opt.parse! end unless @data_format guessed_format = ARGV.length < 2 ? :default : guess_file_format(ARGV[1]) @data_format = guessed_format || :default end end |
#read_input_files ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 82 def read_input_files template, record = ARGV.map {|arg| File.(arg) if arg } if template @template_data = File.read(template) else STDERR.puts "No template file is given." end @record_data = record ? File.read(record) : ARGF.read end |
#set_encoding(given_opt) ⇒ Object
40 41 42 43 44 |
# File 'lib/ad_hoc_template/command_line_interface.rb', line 40 def set_encoding(given_opt) external, internal = given_opt.split(/:/o, 2) Encoding.default_external = external if external and not external.empty? Encoding.default_internal = internal if internal and not internal.empty? end |