Class: AbsoluteRenamer::GeneralParser

Inherits:
IParser show all
Defined in:
lib/absolute_renamer/external/parsers/core/general/parser.rb

Class Method Summary collapse

Methods inherited from WithChildren

inherited

Methods included from UseConfig

#conf

Class Method Details

.add_options(parser, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/absolute_renamer/external/parsers/core/general/parser.rb', line 4

def self.add_options(parser, options)
    parser.banner << ' [file]...'
    parser.on_tail('-h', '--help', 'Display this help screen') do
        puts parser
        exit 0
    end

    parser.on('-r', '--replace PATTERN,REPLACEMENT', Array,
              'String replacement ex:"pattern,replacement" ') do |data|
        self.add_replacement(data, options)
    end

    parser.on('-e', '--regexp-replace PATTERN,REPLACEMENT', Array,
              'String replacement using regexp') do |data|
        self.add_replacement(data, options, true)
    end

    parser.on('-f', '--format FORMAT',
              'Format string used as model') do |format|
        options[:format] = format
        @format_given = true
    end

    parser.on('-x', '--ext-format FORMAT',
              'Format string used as model for the extension') do |format|
        options[:ext_format] = format
    end

    parser.on('-R', '--recursive',
              'Rename files in subdirectories recursively') do
        options[:rec] = true
    end

    parser.on('--maxdepth N', Integer, 'Maximum recursion depth') do |depth|
        options[:maxdepth] = depth
    end

    parser.on('-m', '--mode MODE', [:rename, :copy, :move, :link],
              'Renaming mode. Can be used with --dest DEST.',
              '  rename: simply rename files',
              '  copy: make a copy of each file in DEST with its new name',
              '  move: move each file in DEST with its new name',
              '  link: create a symbolic link to each file in DEST' <<
              ' with its new name') do |mode|
        options[:mode] = mode
    end

    parser.on('--dest DEST', 'Destination directory' <<
              ' for copy, move and link modes') do |dest|
        options[:dest] = File.expand_path(dest)
    end
    
    parser.on('-d', '--directories', 'Directories handling') do
        options[:dir] = true
    end
end

.add_replacement(data, options, regexp = false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/absolute_renamer/external/parsers/core/general/parser.rb', line 61

def self.add_replacement(data, options, regexp = false)
    pattern,replace = data[0..1]
    replace ||= ''
    pattern = Regexp.new(pattern) if regexp
    moment = @format_given.nil? ? :before : :after
    options[:replacements] ||= {}
    options[:replacements][moment] ||= []
    options[:replacements][moment] << {:type => pattern.class,
                                       :pattern => pattern,
                                       :replace => replace
                                      }
end