Class: Renamr::Configurator
- Inherits:
-
Object
- Object
- Renamr::Configurator
- Defined in:
- lib/renamr/configurator.rb
Overview
Parses and validates command-line options.
Constant Summary collapse
- DIC =
[ ['-a', '--act', 'Performs actual renaming.', :act], ['-d', '--dir dir', 'Directory to process.', :dir], ['-l', '--lim', 'Limits file name length.', :lim], ['-m', '--mod', 'Prepends file modification time.', :mod], ['-r', '--rec', 'Processes directories recursively.', :rec], ['-s', '--src src', 'String to replace.', :src], ['-t', '--dst dst', 'Replacement string.', :dst], ['-w', '--wid wid', 'Output table width.', :wid] ].freeze
Instance Method Summary collapse
- #act? ⇒ Boolean
- #add(opt) ⇒ Object
- #add_cut(opt) ⇒ Object
- #add_prepend(opt) ⇒ Object
- #add_simple(opt, key) ⇒ Object
- #add_version(opt) ⇒ Object
- #beg ⇒ Object
- #dir ⇒ Object
- #dst ⇒ Object
-
#initialize ⇒ Configurator
constructor
A new instance of Configurator.
- #len ⇒ Object
- #lim? ⇒ Boolean
- #mod? ⇒ Boolean
- #pos ⇒ Object
- #pre ⇒ Object
- #rec? ⇒ Boolean
- #src ⇒ Object
- #validate ⇒ Object
- #wid ⇒ Object
Constructor Details
#initialize ⇒ Configurator
Returns a new instance of Configurator.
60 61 62 63 64 65 66 67 |
# File 'lib/renamr/configurator.rb', line 60 def initialize @options = {} OptionParser.new do |o| o. = "Usage: #{File.basename($PROGRAM_NAME)} [options]." add(o) end.parse! validate end |
Instance Method Details
#act? ⇒ Boolean
80 81 82 |
# File 'lib/renamr/configurator.rb', line 80 def act? @options[:act] end |
#add(opt) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/renamr/configurator.rb', line 50 def add(opt) add_simple(opt, :act) add_cut(opt) %i[dir lim mod].each { |k| add_simple(opt, k) } add_prepend(opt) %i[rec src dst].each { |k| add_simple(opt, k) } add_version(opt) add_simple(opt, :wid) end |
#add_cut(opt) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/renamr/configurator.rb', line 28 def add_cut(opt) msg = 'Removes len characters starting at pos.' opt.on('-c', '--cut pos,len', Array, msg) do |l| @options[:pos] = l[0] @options[:len] = l[1] end end |
#add_prepend(opt) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/renamr/configurator.rb', line 36 def add_prepend(opt) opt.on('-p', '--prepend str,beg', Array, 'Prepends a string.') do |l| @options[:pre] = l[0] @options[:beg] = l[1].nil? ? 0 : l[1].to_i end end |
#add_simple(opt, key) ⇒ Object
23 24 25 26 |
# File 'lib/renamr/configurator.rb', line 23 def add_simple(opt, key) f, p, d = DIC.find { |_, _, _, k| k == key } opt.on(f, p, d) { |i| @options[key] = i } end |
#add_version(opt) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/renamr/configurator.rb', line 43 def add_version(opt) opt.on('-v', '--version', 'Shows the version.') do puts "#{File.basename($PROGRAM_NAME)} #{VERSION} #{DATE}" exit end end |
#beg ⇒ Object
112 113 114 |
# File 'lib/renamr/configurator.rb', line 112 def beg @options[:beg] end |
#dir ⇒ Object
96 97 98 |
# File 'lib/renamr/configurator.rb', line 96 def dir @options[:dir] end |
#dst ⇒ Object
104 105 106 |
# File 'lib/renamr/configurator.rb', line 104 def dst @options[:dst] end |
#len ⇒ Object
120 121 122 |
# File 'lib/renamr/configurator.rb', line 120 def len @options[:len] end |
#lim? ⇒ Boolean
88 89 90 |
# File 'lib/renamr/configurator.rb', line 88 def lim? @options[:lim] end |
#mod? ⇒ Boolean
92 93 94 |
# File 'lib/renamr/configurator.rb', line 92 def mod? @options[:mod] end |
#pos ⇒ Object
116 117 118 |
# File 'lib/renamr/configurator.rb', line 116 def pos @options[:pos] end |
#pre ⇒ Object
108 109 110 |
# File 'lib/renamr/configurator.rb', line 108 def pre @options[:pre] end |
#rec? ⇒ Boolean
84 85 86 |
# File 'lib/renamr/configurator.rb', line 84 def rec? @options[:rec] end |
#src ⇒ Object
100 101 102 |
# File 'lib/renamr/configurator.rb', line 100 def src @options[:src] end |
#validate ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/renamr/configurator.rb', line 69 def validate if dir.nil? @options[:dir] = Dir.pwd else raise "No such directory: #{dir}." unless File.directory?(dir) @options[:dir] = File.(dir) end raise "Table width must be at least 15 characters: #{wid}." if wid < 15 end |
#wid ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/renamr/configurator.rb', line 124 def wid if @options[:wid].nil? # Reads the current terminal width. wid = `tput cols` wid.to_s.empty? ? 79 : wid.to_i else @options[:wid].to_i end end |