Class: Whitepaper::CLI

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

Overview

The commandline interface to Whitespace.

Constant Summary collapse

<<-USAGE
USAGE

Class Method Summary collapse

Class Method Details

.parse_optionsObject

Parse and respond to the command line options.



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

def parse_options
  options = {}
  @opts = OptionParser.new do |opts|
    opts.banner = BANNER.gsub(/^    /, '')

    opts.separator ''
    opts.separator 'Options:'

    opts.on('-h', '--help', 'Display this help') do
      puts opts
      exit 0
    end

    opts.on('-t', '--by-title KEYWORDS', 'Display the data for the paper with the given KEYWORDS in title') do |title|
      options[:by_title] = title
    end

    opts.on('-d', '--download', 'Downloads a pdf of the paper of the paper found') do
      options[:download] = true
    end

    opts.on('-p', '--pdf', 'Display a link to the pdf of the paper found') do
      options[:print_pdf_url] = true
    end

    opts.on('-n', '--name', 'Display the title of the paper found') do
      options[:print_title] = true
    end

    opts.on('-a', '--authors', 'Display the authors of the paper found') do
      options[:print_authors] = true
    end

  end

  @opts.parse!

  if options[:by_title]
    paper = Whitepaper.find_by_title(options[:by_title])

    if options[:print_title]
      puts paper.title
    end

    if options[:print_authors]
      puts paper.authors
    end

    if options[:print_pdf_url]
      unless paper.pdf_urls.empty?
        puts paper.pdf_urls.first
      end
    end

    unless options[:print_title] or
           options[:print_authors] or
           options[:print_pdf_url]
      puts paper
    end

    if options[:download] and not paper.pdf_urls.empty?
      puts "Downloading: " + paper.pdf_urls.first
      paper.download
    end
  else
    puts @opts
  end
end

.runObject

Executes the command line version of whitespace.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/whitepaper/cli.rb', line 84

def CLI.run
  begin
    parse_options
  rescue OptionParser::InvalidOption => e
    warn e
    exit -1
  end

  # Default
  puts BANNER
  exit 0
end