Class: CSVDiff::CLI

Inherits:
Object
  • Object
show all
Includes:
ArgParser::DSL
Defined in:
lib/csv-diff-report/cli.rb

Instance Method Summary collapse

Instance Method Details

#process(arguments) ⇒ Object

Process a CSVDiffReport using arguments to determine all options.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/csv-diff-report/cli.rb', line 124

def process(arguments)
    options = {}
    exclude_args = [:from, :to, :delimiter, :ignore_case]
    arguments.each_pair do |arg, val|
        options[arg] = val if val && !exclude_args.include?(arg)
    end
    options[:csv_options] = {:col_sep => arguments.delimiter}
    options[:case_sensitive] = !arguments.ignore_case
    rep = CSVDiff::Report.new
    rep.diff(arguments.from, arguments.to, options)

    output_dir = FileTest.directory?(arguments.from) ?
        arguments.from : File.dirname(arguments.from)
    left_name = File.basename(arguments.from, File.extname(arguments.from))
    right_name = File.basename(arguments.to, File.extname(arguments.to))
    output = arguments.output ||
        "#{output_dir}/Diff_#{left_name}_to_#{right_name}.diff"
    rep.output(output, arguments.format)
end

#runObject

Parses command-line options, and then performs the diff.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/csv-diff-report/cli.rb', line 100

def run
    if arguments = parse_arguments
        begin
            process(arguments)
        rescue RuntimeError => ex
            Console.puts ex.message, :red
            exit 1
        end
    else
        if show_help?
            show_help(nil, Console.width).each do |line|
                Console.puts line, :cyan
            end
        else
            show_usage(nil, Console.width).each do |line|
                Console.puts line, :yellow
            end
        end
        exit 2
    end
end