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 PNG CR2], destination_format: '%Y/%m/%d/%{full_name}' # rubocop:disable Style/FormatStringToken }.freeze
Class Method Summary collapse
-
.parse(argv) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
- .validate_mandatory_options!(options) ⇒ Object
Class Method Details
.parse(argv) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
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 95 96 97 |
# File 'lib/imagesorter/option_parser.rb', line 18 def self.parse(argv) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize = OpenStruct.new(DEFAULT_OPTIONS) # rubocop:disable Metrics/BlockLength parser = ::OptionParser.new do |opts| opts. = 'Usage: imagesorter [options]' opts.on('-s', '--source SOURCE_DIR', 'Source directory') do |source| .source = source end opts.on('-d', '--dest DESTINATION_DIR', 'Destination directory (will be created if it does not exist)') do |dest| .dest = dest end opts.on('-t', '--threads THREADS', "Number of threads to run the processing in (default is #{options.threads})") do |threads| .threads = threads end opts.on('-m', '--move', 'Move rather than copy') do .copy_mode = :move end opts.on('-e', '--extensions EXTENSIONS', Array, "What extensions to include (default is #{options.extensions.join(',')})") do |extensions| .extensions = extensions end opts.on('-r', '--recursive', "Iterate source recursively (default is #{options.recursive})") do |recursive| .recursive = recursive end opts.on('-l', '--logfile LOGFILE', 'Write message to given logfile. Default output is STDOUT') do |logfile| .logfile = logfile end opts.on('--locale LOCALE', "Locale to use (default is #{options.locale})") do |locale| .locale = locale end opts.on('--destination-format DESTINATION_FORMAT', "Destination format (default is #{options.destination_format})") do |destination_format| .destination_format = destination_format end opts.on('--[no-]silent', 'Run silently') do |silent| .silent = silent end opts.on('--[no-]test', 'Do a test-run without touching any files') do |test| .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| .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 # rubocop:enable Metrics/BlockLength parser.parse!(argv) () rescue ::OptionParser::InvalidOption, ::OptionParser::MissingArgument => e puts e.to_s puts parser exit end |
.validate_mandatory_options!(options) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/imagesorter/option_parser.rb', line 99 def self.() mandatory = i[source dest] missing = mandatory.select { |param| [param].nil? } return if missing.empty? raise ::OptionParser::MissingArgument, missing.join(', ') end |