Class: XMLTV::XmltvOptparser

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object

Return a structure describing the options.



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/xmltv/xmltv.rb', line 107

def self.parse(args)
  options = OpenStruct.new
  options.basedir = "#{ENV['HOME']}/.xmltv"
  options.validate = true
  xfound = nil
  %w{ xmllint tv_sort }.each do |prog|
    xfound = false
    ENV['PATH'].split(':').each do |path|
      xfound = true if test(?x, "#{path}/#{prog}")
    end
    break unless xfound
  end

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{$PROGRAM_NAME} [options]"

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-a", "--list-all",
            "List all available channels") do |av|
      options.available = av
      options.action = true
    end

    opts.on("--add x,y,z", Array, "Add channels to config") do |list|
      options.add = list
      options.action = true
    end
    opts.on("--delete x,y,z", Array, "Delete channels from config") do |list|
      options.del = list
      options.action = true
    end
    opts.on("-l", "--list", "List configured channels") do |list|
      options.list = list
      options.action = true
    end
    opts.on("-c", "--config-file FILENAME", "Configuration file") do |list|
      options.config = list
    end
    opts.on( "--basedir DIRNAME", "Basedir (#{options.basedir})") do |list|
      options.basedir = list
    end
    opts.on( "--no-validate", "Do not validate") do |v|
      options.validate = false
    end
    opts.on( "--mailto USER", "Mail exception") do |user|
      options.mailto = user
    end

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options.verbose = v
    end

    opts.separator ""
    opts.separator "Common options:"

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

    opts.on_tail("--version", "Show version") do
      options.version = true
    end
  end

  opts.parse!(args)
  if ! xfound && options.validate
    options.validate = false
    STDERR.puts "Disabled validation: xmllint and/or tv_sort not found"
  end    
  options
end