Class: CF::UAA::BaseCli

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

Direct Known Subclasses

Cli

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.global_optionsObject

Returns the value of attribute global_options.



233
234
235
# File 'lib/uaa/cli/base.rb', line 233

def global_options
  @global_options
end

.inputObject (readonly)

Returns the value of attribute input.



232
233
234
# File 'lib/uaa/cli/base.rb', line 232

def input
  @input
end

.option_defsObject (readonly)

Returns the value of attribute option_defs.



232
233
234
# File 'lib/uaa/cli/base.rb', line 232

def option_defs
  @option_defs
end

.outputObject (readonly)

Returns the value of attribute output.



232
233
234
# File 'lib/uaa/cli/base.rb', line 232

def output
  @output
end

.overviewObject

Returns the value of attribute overview.



233
234
235
# File 'lib/uaa/cli/base.rb', line 233

def overview
  @overview
end

.topicsObject

Returns the value of attribute topics.



233
234
235
# File 'lib/uaa/cli/base.rb', line 233

def topics
  @topics
end

Class Method Details

.handle_bad_command(args, msg) ⇒ Object

to be implemented in subclass



237
# File 'lib/uaa/cli/base.rb', line 237

def self.handle_bad_command(args, msg) end

.preprocess_options(args, opts) ⇒ Object

to be implemented in subclass



236
# File 'lib/uaa/cli/base.rb', line 236

def self.preprocess_options(args, opts) end

.run(args = ARGV) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/uaa/cli/base.rb', line 239

def self.run(args = ARGV)
  @input ||= $stdin
  @output ||= $stdout
  @output.string = "" if @output.respond_to?(:string)
  args = Shellwords.split(args) if args.respond_to?(:split)
  @option_defs ||= {}
  orig = args
  @parser = OptionParser.new
  opts = @topics.each_with_object({}) do |tpc, o|
    tpc.option_defs.each do |k, optdef|
      @parser.on(*optdef) do |v|
        o[k] = (o[k] ? Array(o[k]).push(v) : v )
      end
      @option_defs[k] = optdef
    end
  end
  @parser.parse! args
  preprocess_options(args, opts)
  @topics.each do |tpc|
    tpc.commands.each do |k, v|
      next unless args[0..v[:parts].length - 1] == v[:parts]
      args = args[v[:parts].length..-1]
      if v[:argc] == -1
        # variable args, leave args alone
      elsif args.length > v[:argc]
        return handle_bad_command(orig, "Too many command line parameters given.")
      elsif args.length < v[:argc]
        (v[:argc] - args.length).times { args << nil }
      end
      opts.keys.each do |o|
        next if v[:options].include?(o) || @global_options.include?(o)
        return handle_bad_command(orig, "Invalid option: #{o}")
      end
      return tpc.new(self, opts, @input, @output).send(k, *args)
    end
  end
  @output.puts "#{File.basename($0)}: subcommand not found"
rescue Exception => e
  @output.puts "#{File.basename($0)} error", "#{e.class}: #{e.message}", (e.backtrace if opts[:trace])
ensure
  puts @output.string if opts[:trace] && @print_on_trace && @output.respond_to?(:string)
end