Class: Nrename::Options

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nrename/options.rb

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



11
12
13
# File 'lib/nrename/options.rb', line 11

def initialize
  self.class.def_delegators :options, *default_options.keys
end

Instance Method Details

#default_optionsObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nrename/options.rb', line 15

def default_options
  {
  :numbers_only => false,
  :dirs         => Set.new,
  :execute      => false,
  :pattern      => /(\d+)/,
  :recursive    => false,
  :rename_dirs  => false,
  :renumber     => false,
  :verbose      => true
  }
end

#extract_dirs(args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/nrename/options.rb', line 107

def extract_dirs(args)
  dirs = Set.new

  args.each do |arg|
    dir = File.expand_path arg
    if File.directory? dir
      dirs << dir
    else
      warn "#{dir} is not a valid directory."
      exit 1
    end
  end

  if options.recursive
    dirs.dup.each do |dir|
      dirs.merge Utils.all_subdirs_of dir
    end
  end

  dirs
end

#optionsObject



28
29
30
# File 'lib/nrename/options.rb', line 28

def options
  @options ||= OpenStruct.new default_options
end

#parse(args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nrename/options.rb', line 88

def parse(args)
  # Display help if called through 'nrename' executable and without arguments:
  if args.empty? && Nrename.executable_name == 'nrename'
    args << '--help'
  end

  parser.parse! args

  if !options.execute && Nrename.executable_name == 'nrename'
    at_exit do
      warn 'No renaming is done. Run with -X option to perform actual changes.'
    end
  end

  options.dirs = extract_dirs args

  self
end

#parserObject



36
37
38
39
40
41
42
43
44
45
46
47
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
78
79
80
81
82
83
84
85
86
# File 'lib/nrename/options.rb', line 36

def parser
  OptionParser.new do |opts|
    opts.banner = "Usage: #{Nrename.executable_name} [OPTINS] DIR..."

    opts.separator ''
    opts.separator 'Options:'

    opts.on '-X', '--execute', "Do actual work" do |x|
      options.execute = x
    end

    opts.on '-R', '--recursive',
    'Process given directories recursively' do |rec|
      options.recursive = rec
    end

    opts.on '-N', '--numbers-only',
    'Leave only numbers in file name' do |n|
      options.numbers_only = n
    end

    opts.on '-D', '--rename-dirs',
    'Rename only directories instead of regular files' do |d|
      options.rename_dirs = d
    end

    opts.on '--renumber',
    'Renumber files from starting from 1 and on' do |renum|
      options.renumber = renum
    end

    opts.on '--regexp REGEXP', Regexp,
    'Use REGEXP to match filenames' do |regexp|
      options.pattern = regexp
    end

    opts.on '-v', '--[no-]verbose', 'Run verbosely' do |v|
      options.verbose = v
    end

    opts.on '-h', '--help', 'Display this message' do
      puts opts
      exit
    end

    opts.on '--version', 'Display version' do
      puts VERSION
      exit
    end
  end
end

#resetObject



32
33
34
# File 'lib/nrename/options.rb', line 32

def reset
  @options = nil
end