Module: Imagesorter::OptionParser

Defined in:
lib/imagesorter/option_parser.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  silent:    false,
  test:      false,
  recursive: false,
  copy_mode: :copy,
  threads:   1,
  locale:    'en-us',
  extensions: %w[JPG JPEG MP4 MOV],
  destination_format: '%Y/%m/%d/%{full_name}' # rubocop:disable Style/FormatStringToken
}.freeze

Class Method Summary collapse

Class Method Details

.parse(argv) ⇒ Object



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
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
87
88
89
90
91
92
93
94
# File 'lib/imagesorter/option_parser.rb', line 16

def self.parse(argv)
  options = OpenStruct.new(DEFAULT_OPTIONS)

  # rubocop:disable Metrics/BlockLength
  parser = ::OptionParser.new do |opts|
    opts.banner = 'Usage: imagesorter [options]'

    opts.on('-s', '--source SOURCE_DIR', 'Source directory') do |source|
      options.source = source
    end

    opts.on('-d', '--dest DESTINATION_DIR', 'Destination directory (will be created if it does not exist)') do |dest|
      options.dest = dest
    end

    opts.on('-t', '--threads THREADS', "Number of threads to run the processing in (default is #{options.threads})") do |threads|
      options.threads = threads
    end

    opts.on('-m', '--move', 'Move rather than copy') do
      options.copy_mode = :move
    end

    opts.on('-e', '--extensions EXTENSIONS', Array, "What extensions to include (default is #{options.extensions.join(',')})") do |extensions|
      options.extensions = extensions
    end

    opts.on('-r', '--recursive', "Iterate source recursively (default is #{options.recursive})") do |recursive|
      options.recursive = recursive
    end

    opts.on('-l', '--logfile LOGFILE', 'Write message to given logfile. Default output is STDOUT') do |logfile|
      options.logfile = logfile
    end

    opts.on('--locale LOCALE', "Locale to use (default is #{options.locale})") do |locale|
      options.locale = locale
    end

    opts.on('--destination-format DESTINATION_FORMAT', "Destination format (default is #{options.destination_format})") do |destination_format|
      options.destination_format = destination_format
    end

    opts.on('--[no-]silent', 'Run silently') do |silent|
      options.silent = silent
    end

    opts.on('--[no-]test', 'Do a test-run without touching any files') do |test|
      options.test = test
    end

    opts.separator ''
    opts.separator 'Common options:'
    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit
    end

    opts.on('--verbose', 'Increase log message verbosity') do |verbose|
      options.verbose = verbose
    end

    # Another typical switch to print the version.
    opts.on_tail('-v', '--version', 'Show version') do
      puts Imagesorter::Gem.version
      exit
    end
  end

  parser.parse!(argv)

  validate_mandatory_options!(options)

  options
rescue ::OptionParser::InvalidOption, ::OptionParser::MissingArgument => e
  puts e.to_s
  puts parser
  exit
end

.validate_mandatory_options!(options) ⇒ Object

Raises:

  • (::OptionParser::MissingArgument)


96
97
98
99
100
101
# File 'lib/imagesorter/option_parser.rb', line 96

def self.validate_mandatory_options!(options)
  mandatory = %i[source dest]
  missing = mandatory.select { |param| options[param].nil? }
  return if missing.empty?
  raise ::OptionParser::MissingArgument, missing.join(', ')
end