Class: Renmov::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/renmov/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV, renamer = BasicRenamer) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
15
16
17
18
# File 'lib/renmov/cli.rb', line 11

def initialize(args = ARGV, renamer = BasicRenamer)
  @executable_name = File.basename($PROGRAM_NAME, '.rb')
  @args      = args
  @options   = { verbose: false, noop: false }
  @optparse  = OptionParser.new
  @filenames = []
  @renamer   = renamer
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/renmov/cli.rb', line 8

def args
  @args
end

#executable_nameObject (readonly)

Returns the value of attribute executable_name.



8
9
10
# File 'lib/renmov/cli.rb', line 8

def executable_name
  @executable_name
end

#filenamesObject

Returns the value of attribute filenames.



9
10
11
# File 'lib/renmov/cli.rb', line 9

def filenames
  @filenames
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/renmov/cli.rb', line 8

def options
  @options
end

#optparseObject (readonly)

Returns the value of attribute optparse.



8
9
10
# File 'lib/renmov/cli.rb', line 8

def optparse
  @optparse
end

#renamerObject

Returns the value of attribute renamer.



9
10
11
# File 'lib/renmov/cli.rb', line 9

def renamer
  @renamer
end

Instance Method Details

#output_error_message(msg) ⇒ Object



79
80
81
82
# File 'lib/renmov/cli.rb', line 79

def output_error_message(msg)
  $stderr.puts "#{executable_name}: #{msg}"
  $stderr.puts optparse
end

#parse_optionsObject



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
# File 'lib/renmov/cli.rb', line 48

def parse_options
  optparse.banner =
    ['Rename video files to a consistent format.', '',
     "Usage: #{executable_name} [options] filename..."].join("\n")

  optparse.on('-v', '--verbose',
              'Output more information') do
    options[:verbose] = true
  end

  optparse.on('-n', '--noop',
              'Output actions without invoking them') do
    options[:noop]    = true
    options[:verbose] = true
  end

  optparse.on_tail('-h', '--help',
                   'Show this message') do
    puts optparse
    exit
  end

  optparse.on_tail('--version',
                   'Show version') do
    puts Renmov::VERSION
    exit
  end

  self.filenames = optparse.parse! args
end

#rename_file(old, new) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/renmov/cli.rb', line 84

def rename_file(old, new)
  unless old == new
    FileUtils.mv(old,
                 new,
                 verbose: options[:verbose],
                 noop:    options[:noop])
  end
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/renmov/cli.rb', line 20

def run
  exit_status = 0

  begin
    parse_options
    raise NoFilenameError if filenames.empty?

    filenames.each do |filename|
      dirname   = "#{File.dirname(filename)}/"
      dirname.gsub!(/\A\.\/\z/, '')
      
      basename  = File.basename(filename)
      renamer_i = renamer.new(basename)
      newname   = "#{dirname}#{renamer_i.rename}"

      rename_file(filename, newname)
    end
  rescue OptionParser::InvalidOption => e
    output_error_message(e)
    exit_status = 1
  rescue NoFilenameError => e
    output_error_message(e)
    exit_status = 1
  end

  exit_status
end