class RenameFiles::Cli

Attributes

file_names[R]
options[R]

Public Class Methods

new() click to toggle source
# File lib/rename_files/cli.rb, line 41
def initialize
  @options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: #{__FILE__} [files] [options]"

    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on("-x", "--execute", "Actually rename files instead of only showing the commands. Defaults to false (no execution).") do |n|
      @options[:execute] = !!n
    end
    opts.on("-m", "--matches [PATTERN]", "Regular expression pattern to limit which files are renamed.") do |n|
      @options[:pattern] = n.to_s
    end
    opts.on("-d", "--directory [DIR]", "Directory in which to rename all files.") do |n|
      @options[:directory] = n.to_s
    end
    opts.on("-p", "--prefix [STRING]", "String that must be at the start of all file names. It will be prepended if necessary.") do |n|
      @options[:prefix] = n.to_s
    end
    opts.on("-a", "--postfix [STRING]", "String that must be at the end of all file names. It will be appended if necessary") do |d|
      @options[:postfix] = d.to_s
    end
  end.parse!

  @file_names = if File.directory? @options[:directory].to_s
    d = Dir.new(@options[:directory])
    Dir.chdir d.path
    d
  else
    ARGV
  end
end

Public Instance Methods

go() { |f| ... } click to toggle source
# File lib/rename_files/cli.rb, line 77
def go
  if block_given?
    RenameFiles.rename @file_names, pattern: @options[:pattern], prefix: @options[:prefix], postfix: @options[:postfix], execute: @options[:execute] do |f|
      yield f
    end
  else
    RenameFiles.rename @file_names, pattern: @options[:pattern], prefix: @options[:prefix], postfix: @options[:postfix], execute: @options[:execute]
  end
end