lib/archiver/cli.rb in archiver-1.0.0 vs lib/archiver/cli.rb in archiver-1.0.1

- old
+ new

@@ -10,82 +10,99 @@ class Cli attr_accessor :stdout attr_accessor :stderr attr_accessor :config attr_accessor :parser - + def initialize(argv, stdout, stderr) @stdout = stdout @stderr = stderr - + @config = { - :verbose => false + :verbose => false, + :from => false, + :into => false } - + @parser = OptionParser.new do |opt| - opt.banner = "Usage: archiver [options] <fromdir> <intodir>" + opt.banner = "Usage: archiver [options]" + opt.separator "" + opt.separator "Mandatory options:" + + opt.on("-f", "--from DIR", "From directory name") do |dir| + @config[:from] = dir + end + + opt.on("-i", "--into DIR", "Into directory name") do |dir| + @config[:into] = dir + end + + opt.separator "" + opt.separator "Optional options:" + opt.on("-V", "--verbose", "Verbose output format") do @config[:verbose] = true end opt.on("-h", "--help", "Show this help message") do - puts @parser + stdout.puts @parser exit 0 end - + opt.on("-v", "--version", "Show current version") do - puts "archiver v#{Version::STRING}" + stdout.puts "archiver v#{Version::STRING}" exit 0 end end - unless argv.size == 2 + @parser.parse! + + unless @config[:from] or @config[:into] puts @parser exit 1 end - - @config[:fromdir] = argv.shift - @config[:intodir] = argv.shift - @parser.parse! - checks process end - + def checks - unless File.directory? config[:fromdir] - error "Fromdir does not exist" + unless File.directory? config[:from] + error "Fromdir #{config[:from]} does not exist" end - unless File.directory? config[:intodir] - error "Intodir does not exist" + unless File.directory? config[:into] + error "Intodir #{config[:into]} does not exist" end - config[:fromdir_real] = File.realpath config[:fromdir] - config[:intodir_real] = File.realpath config[:intodir] + config[:fromdir] = File.realpath config[:from] + config[:intodir] = File.realpath config[:into] end - + def process unless config[:verbose] - progress = ProgressBar.new('Archiver', records.count) + progress = ProgressBar.new("Archiver", records.count, stdout) end - + records.each do |record| next unless record.process? - record.move config[:intodir_real] if config[:verbose] - puts "#{record.path} -> #{record.filename}" + from = record.path.gsub(/^#{config[:fromdir]}\//, "") + into = File.join(record.segment, record.filename) + + stdout.puts "#{from} -> #{into}" else progress.inc end + + record.move config[:intodir] end - + if config[:verbose] - puts "Processed #{records.count} records!" + stdout.puts "Processed #{records.count} records!" else progress.finish end rescue SystemExit, Interrupt progress.finish if progress @@ -93,21 +110,21 @@ end def records @records ||= begin rows = [] - Dir.glob(File.join(config[:fromdir_real], '**', '*')).each do |row| + Dir.glob(File.join(config[:fromdir], "**", "*")).each do |row| next if File.directory? row rows << Record.new(row) end - + rows end end - + def error(message) - puts "Error: #{message}" - puts @parser + stderr.puts "Error: #{message}" + stderr.puts @parser exit 1 end end end