Class: Xmfun::CLI

Inherits:
Object
  • Object
show all
Extended by:
Util::Helper
Defined in:
lib/xmfun/cli.rb

Class Method Summary collapse

Methods included from Util::Helper

download_help, main_help

Class Method Details

.start(argv) ⇒ Object



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

def self.start(argv)
  if argv.empty?
    puts main_help
    exit
  end

  url = ''
  dst = '.'

  download_proc = lambda do |option|
    if option == '-h'
      puts download_help
    else
      unless(url = Xmfun::Util::UrlParser.parse(option))
        puts "\e[31mPlease provide valid URL\e[0m"
        exit
      end
    end
  end

  begin
    Clap.run argv,
      "download"   => download_proc,
      "update"     => lambda { system("gem update xmfun") },
      "help"       => lambda { puts main_help; exit },
      "--help"     => lambda { puts main_help; exit },
      "version"    => lambda { puts Xmfun::VERSION; exit },
      "--version"  => lambda { puts Xmfun::VERSION; exit },

      "-u" => lambda { |xmurl|  url = Xmfun::Util::UrlParser.parse(xm_url) },
      "-v" => lambda { puts Xmfun::VERSION; exit},
      "-h" => lambda { puts main_help; exit},
      "-d" => lambda { |d| dst = d }
  rescue ArgumentError
    puts "\e[31mInvalid Usage\e[0m"
    puts main_help
  end

  Xmfun::Util::Downloader.download(url, dst) unless url.empty?
end