Class: Renmov::CLI
- Inherits:
-
Object
- Object
- Renmov::CLI
- Defined in:
- lib/renmov/cli.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#executable_name ⇒ Object
readonly
Returns the value of attribute executable_name.
-
#filenames ⇒ Object
Returns the value of attribute filenames.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#optparse ⇒ Object
readonly
Returns the value of attribute optparse.
-
#renamer ⇒ Object
Returns the value of attribute renamer.
Instance Method Summary collapse
-
#initialize(args = ARGV, renamer = BasicRenamer) ⇒ CLI
constructor
A new instance of CLI.
- #output_error_message(msg) ⇒ Object
- #parse_options ⇒ Object
- #rename_file(old, new) ⇒ Object
- #run ⇒ Object
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
8 9 10 |
# File 'lib/renmov/cli.rb', line 8 def args @args end |
#executable_name ⇒ Object (readonly)
Returns the value of attribute executable_name.
8 9 10 |
# File 'lib/renmov/cli.rb', line 8 def executable_name @executable_name end |
#filenames ⇒ Object
Returns the value of attribute filenames.
9 10 11 |
# File 'lib/renmov/cli.rb', line 9 def filenames @filenames end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/renmov/cli.rb', line 8 def @options end |
#optparse ⇒ Object (readonly)
Returns the value of attribute optparse.
8 9 10 |
# File 'lib/renmov/cli.rb', line 8 def optparse @optparse end |
#renamer ⇒ Object
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 (msg) $stderr.puts "#{executable_name}: #{msg}" $stderr.puts optparse end |
#parse_options ⇒ Object
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 optparse. = ['Rename video files to a consistent format.', '', "Usage: #{executable_name} [options] filename..."].join("\n") optparse.on('-v', '--verbose', 'Output more information') do [:verbose] = true end optparse.on('-n', '--noop', 'Output actions without invoking them') do [:noop] = true [: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: [:verbose], noop: [:noop]) end end |
#run ⇒ Object
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 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 (e) exit_status = 1 rescue NoFilenameError => e (e) exit_status = 1 end exit_status end |