Class: AdLocalize::OptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_localize/option_handler.rb

Constant Summary collapse

GOOGLE_DRIVE_DOCUMENT_ID =
{ length: 32, regexp: /\A[\w-]+\Z/ }

Class Method Summary collapse

Class Method Details

.parseObject



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
# File 'lib/ad_localize/option_handler.rb', line 6

def parse
  args = { debug: false, only: nil}

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: ruby bin/export [options] file(s)"


    opts.on("-h", "--help", "Prints help") do
      puts opts
      exit
    end
    opts.on("-d", "--debug", "Run in debug mode") do
      args[:debug] = true
      LOGGER.debug!
    end
    opts.on("-k", "--drive-key #{GOOGLE_DRIVE_DOCUMENT_ID.dig(:length)}_characters", String, "Use google drive spreadsheets") do |key|
      is_valid_drive_key = !!(key =~ GOOGLE_DRIVE_DOCUMENT_ID.dig(:regexp)) && (key.size >= GOOGLE_DRIVE_DOCUMENT_ID.dig(:length))
      args[:drive_key] = is_valid_drive_key ? key : nil
    end
    opts.on("-s", "--drive-sheet SHEET_ID", String, "Use a specific sheet id for Google Drive spreadsheets with several sheets") do |value|
      args[:sheet_id] = value
    end
    opts.on("-o", "--only platform1,platform2", Array, "Only generate localisation files for the specified platforms. Supported platforms : #{Constant::SUPPORTED_PLATFORMS.join(', ')}") do |platforms|
      args[:only] = filter_option_args("-o", platforms) { |platform| !!Constant::SUPPORTED_PLATFORMS.index(platform) }
    end
    opts.on("-t", "--target-dir PATH", String, "Path to the target directory") do |output_path|
      pn = Pathname.new(output_path)
      if pn.directory? and pn.readable? and pn.writable?
        args[:output_path] = output_path
      else
        raise ArgumentError.new("Invalid target directory. Check the permissions")
      end
    end
  end

  begin
    opt_parser.parse!
  rescue OptionParser::MissingArgument => e
    LOGGER.log(:error, :red, "Missing argument for option #{e.args.join(',')}")
  rescue ArgumentError => e
    LOGGER.log(:error, :red, e.message)
  end
  args
end