Class: Zypper::Onlinesearch::OptParseMain

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

Overview

Parsing the input data.

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/zypper/onlinesearch/cli.rb', line 13

def self.parse(args)
  options = Struct.new(:operation, :query, :refresh, :engine, :timeout, :arch,
                       :distributions, :types, :number, :view, :format).new
  options.operation = :search
  options.query = ""
  options.refresh = false
  options.engine = :all # :opensuse, :packman
  options.timeout = 20
  options.arch = :compatible # :all
  options.distributions = :compatible # :all
  options.types = %i[supported community experimental unsupported]
  options.number = 0
  options.view = :table
  options.format = :all # or single format
  # options.preload = nil #used by :links or :page operations

  opt_parser = OptionParser.new do |opt|
    command = ENV["ZYPPER_ONLINESEARCH"] ? "zypper onlinesearch" : "onlinesearch"
    opt.banner = "Usage: #{command} [OPTIONS] [OPERATION]"

    opt.separator ""
    opt.separator "Operations:"

    opt.on("-s", "--search <STRING>", "Search for STRING to the online engines available") do |o|
      options.operation = :search
      options.query = o
    end

    opt.on("-p", "--page <PAGE_NAME>", "Get info about the PAGE_NAME") do |o|
      options.operation = :page
      options.query = o
    end

    opt.on("-l", "--links <PAGE_NAME>,<NTH>", Array,
           "Print all the links of the NTH item in the PAGE_NAME") do |o|
      options.operation = :links
      options.query = o[0]
      options.number = o[1].to_i
      # options.preload = :page
    end

    opt.on("--clean-cache", "Clean the whole cache") do
      options.operation = :cache_clean
    end

    opt.separator ""
    opt.separator "General Options"

    alt = Request::Search.constants.map(&:to_s).map(&:downcase).join(", ")
    opt.on("--engine <ENGINE>",
           "Use only ENGINE to search for (default: #{options.engine}, alternatives: #{alt})") do |o|
      options.engine = o.to_sym
    end

    opt.on("--refresh", "Refresh the cached values.") do
      options.refresh = true
    end

    opt.on("--timeout <SECONDS>", "The SECONDS before an HTTP Timeout Error (Default: #{options.timeout})") do |o|
      options.timeout = o.to_f
    end

    opt.separator ""
    opt.separator "View options:"

    opt.on("--table", "Show the results as a table [DEFAULT]") do
      options.view = :table
    end

    opt.on("--report", "Show the results as report") do
      options.view = :report
    end

    opt.separator ""
    opt.separator '"Page" and "Links" options:'

    opt.on("--all-architectures", "Show all the available architectures") do
      options.arch = :all
    end

    opt.on("--all-distributions", "Show all the available distributions") do
      options.distributions = :all
    end

    opt.on("--no-supported", "Hide supported packages.") do
      options.types.delete :supported
    end

    opt.on("--no-experimental", "Hide experimental packages.") do
      options.types.delete :experimental
    end

    opt.on("--no-community", "Hide community packages.") do
      options.types.delete :community
    end

    opt.on("--no-unsupported", "Hide unsupported packages.") do
      options.types.delete :unsupported
    end

    opt.separator ""
    opt.separator '"Links" options:'

    opt.on("--format FORMAT", "Filter for packages with the specified FORMAT") do |o|
      options.format = o.to_sym
    end

    opt.on("--architecture ARCHITECTURE", "Filter for packages with the specified ARCHITECTURE") do |o|
      options.arch = o.to_sym
    end

    opt.on("--urls", "Show only the urls without headers") do
      options.view = :urls
    end

    unless ENV["ZYPPER_ONLINESEARCH"]
      opt.separator ""
      opt.separator "Other:"

      opt.on_tail("-h", "--help", "Show this message") do
        puts opt
        exit
      end

      opt.on_tail("-v", "--version", "Show version") do
        puts VERSION
        exit
      end
    end
  end

  if ARGV.empty?
    puts opt_parser
    exit
  else
    opt_parser.parse!(args)
  end

  options
end