Class: Nrename::Options
- Inherits:
-
Object
- Object
- Nrename::Options
- Extended by:
- Forwardable
- Defined in:
- lib/nrename/options.rb
Instance Method Summary collapse
- #default_options ⇒ Object
- #extract_dirs(args) ⇒ Object
-
#initialize ⇒ Options
constructor
A new instance of Options.
- #options ⇒ Object
- #parse(args) ⇒ Object
- #parser ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Options
Returns a new instance of Options.
11 12 13 |
# File 'lib/nrename/options.rb', line 11 def initialize self.class.def_delegators :options, *.keys end |
Instance Method Details
#default_options ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/nrename/options.rb', line 15 def { :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. arg if File.directory? dir dirs << dir else warn "#{dir} is not a valid directory." exit 1 end end if .recursive dirs.dup.each do |dir| dirs.merge Utils.all_subdirs_of dir end end dirs end |
#options ⇒ Object
28 29 30 |
# File 'lib/nrename/options.rb', line 28 def @options ||= OpenStruct.new 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 !.execute && Nrename.executable_name == 'nrename' at_exit do warn 'No renaming is done. Run with -X option to perform actual changes.' end end .dirs = extract_dirs args self end |
#parser ⇒ Object
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. = "Usage: #{Nrename.executable_name} [OPTINS] DIR..." opts.separator '' opts.separator 'Options:' opts.on '-X', '--execute', "Do actual work" do |x| .execute = x end opts.on '-R', '--recursive', 'Process given directories recursively' do |rec| .recursive = rec end opts.on '-N', '--numbers-only', 'Leave only numbers in file name' do |n| .numbers_only = n end opts.on '-D', '--rename-dirs', 'Rename only directories instead of regular files' do |d| .rename_dirs = d end opts.on '--renumber', 'Renumber files from starting from 1 and on' do |renum| .renumber = renum end opts.on '--regexp REGEXP', Regexp, 'Use REGEXP to match filenames' do |regexp| .pattern = regexp end opts.on '-v', '--[no-]verbose', 'Run verbosely' do |v| .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 |
#reset ⇒ Object
32 33 34 |
# File 'lib/nrename/options.rb', line 32 def reset @options = nil end |