Method: ImgDl::Cli.parse_to_options

Defined in:
lib/img_dl/cli.rb

.parse_to_options(args) ⇒ Object



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
# File 'lib/img_dl/cli.rb', line 46

def parse_to_options args
  case args.size
  when 0,1
    case args.first
    when '-h'
      puts HELP
    when '-version'
      puts VERSION
    else
      not_match
      exit 1
    end
  else
    save_path = args.pop
    url = args.pop
    options = {}
    options[:recursive] = args.delete '-r'
    if args.size.odd?
      not_match 
      exit 1
    end
    args.each_slice(2) do |opt,arg|
      case opt
      when '-ul'
        options[:url_limit_count] = arg.to_i
      when '-il'
        options[:image_limit_count] = arg.to_i
      when '-ur'
        options[:url_reg] = Regexp.new arg
      when '-ir'
        options[:image_reg] = Regexp.new arg
      when '-pf'
        options[:prefix] = arg
      when '-in'
        options[:interval] = arg.to_i
      else
        puts "option '#{opt}' not support! please check out img_dl -h"
      exit 1
      end
    end
    parser = ImgDl::Parser.new(url,save_path,options)
    Thread.start{parser.start;puts 'All done.';exit}
    parser
  end
end