Class: Bookscan::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/bookscan/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



8
9
10
11
# File 'lib/bookscan/cli.rb', line 8

def initialize
  @options = Hash.new
  @options[:debug] = false
end

Class Method Details

.run(argv) ⇒ Object



44
45
46
# File 'lib/bookscan/cli.rb', line 44

def run(argv)
  self.new.execute(argv)
end

Instance Method Details

#execute(argv) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bookscan/cli.rb', line 13

def execute(argv)
  begin
    @opt = OptionParser.new
    @opt.on('--version', 'show version') { version;exit }
    @opt.on('--help', 'show this message') { usage;exit }
    @opt.on('--debug', 'debug mode') { @options[:debug] = true }
    cmd_argv = @opt.order!(argv)
    cmd = cmd_argv.shift
    Commands.new(cmd_argv,@options).send(cmd)
  rescue =>e
    puts e
    usage
    raise e if @options[:debug]
  end
end

#usage(e = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/bookscan/cli.rb', line 29

def usage(e=nil)
  puts @opt
  puts "\nCommands:\n"
  COMMANDS.each { |c|
    puts "    " + c
  }
end

#versionObject



37
38
39
40
41
# File 'lib/bookscan/cli.rb', line 37

def version
  File.open(File.dirname(__FILE__) + '/../../VERSION',"r") { |file|
    puts file.gets
  }
end