Class: Xplenty::Kensa::Client::OptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/xplenty/kensa/client.rb

Constant Summary collapse

KNOWN_ARGS =

OptionParser errors out on unnamed options so we have to pull out all the –flags and –flag=somethings

%w{file async production without-sso help plan version sso foreman template}

Class Method Summary collapse

Class Method Details

.defaultsObject



266
267
268
269
270
271
272
# File 'lib/xplenty/kensa/client.rb', line 266

def self.defaults
  {
    :filename => 'addon-manifest.json',
    :env      => "test",
    :async    => false,
  }
end

.parse(args) ⇒ Object



262
263
264
# File 'lib/xplenty/kensa/client.rb', line 262

def self.parse(args)
  defaults.merge(self.parse_options(args))
end

.parse_command_line(args) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/xplenty/kensa/client.rb', line 296

def self.parse_command_line(args)
  {}.tap do |options|
    OptionParser.new do |o|
      o.on("-f file", "--file") { |filename| options[:filename] = filename }
      o.on("--async")           { options[:async] = true }
      o.on("--production")      { options[:env] = "production" }
      o.on("--without-sso")     { options[:sso] = false }
      o.on("-h", "--help")      { command = "help" }
      o.on("-p plan", "--plan") { |plan| options[:plan] = plan }
      o.on("-v", "--version")   { options[:command] = "version" }
      o.on("-s sso", "--sso")   { |method| options[:method] = method }
      o.on("--foreman")         { options[:foreman] = true }
      o.on("-t name", "--template") do |template|
        options[:template] = template
      end
      #note: have to add these to KNOWN_ARGS

      begin
        o.parse!(args)
      rescue OptionParser::InvalidOption => e
        raise CommandInvalid, e.message
      end
    end
  end
end

.parse_provision(flags, args) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/xplenty/kensa/client.rb', line 282

def self.parse_provision(flags, args)
  {}.tap do |options|
    flags.each do |arg|
      key, value = arg.split('=')
      unless value
        peek = args[args.index(key) + 1]
        value = peek && !peek.match(/^--/) ? peek : 'true'
      end
      key = key.sub(/^--/,'')
      options[key] = value 
    end
  end
end

.pre_parse(args) ⇒ Object



276
277
278
279
280
# File 'lib/xplenty/kensa/client.rb', line 276

def self.pre_parse(args)
  args.partition do |token| 
    token.match(/^--/) && !token.match(/^--(#{KNOWN_ARGS.join('|')})/)
  end.reverse
end