class CF::UAA::Topic

Attributes

synonyms[R]

Public Class Methods

commands() click to toggle source
# File lib/uaa/cli/base.rb, line 28
def self.commands; @commands || {} end
define_option(key, *args) click to toggle source
# File lib/uaa/cli/base.rb, line 35
def self.define_option(key, *args)
  @option_defs ||= {}
  raise "conflicting option definition for #{key}" if @option_defs.key?(key) && @option_defs[key] != args
  @option_defs[key] = args
end
desc(template, desc, *options, &handler) click to toggle source
# File lib/uaa/cli/base.rb, line 41
def self.desc(template, desc, *options, &handler)
  parts, argc = template.split(' '), 0
  cmd = parts.each_with_object([]) { |p, o|
    if p =~ /^\[/
      argc = parts[-1] =~ /\.\.\.\]$/ ? -1 : parts.length - o.length
      break o
    end
    o << p
  }
  cmd_key = cmd.join('_').to_sym
  define_method(cmd_key, handler)
  @commands ||= {}
  @commands[cmd_key] = {parts: cmd, argc: argc, template: template, desc: desc, options: options}
end
new(cli_class, options = {}, input = $stdin, output = $stdout) click to toggle source
# File lib/uaa/cli/base.rb, line 56
def initialize(cli_class, options = {}, input = $stdin, output = $stdout)
  @cli_class, @options, @input, @output = cli_class, options, input, output
  @highline = HighLine.new(input, output)
end
option_defs() click to toggle source
# File lib/uaa/cli/base.rb, line 27
def self.option_defs ; @option_defs || {} end
topic(*args) click to toggle source
# File lib/uaa/cli/base.rb, line 29
def self.topic(*args)
  return @description if args.empty?
  @synonyms = (args[0].split(' ') + args[1..-1]).map(&:downcase)
  @description = args[0]
end

Public Instance Methods

add_command(branches, parts, opts = nil) click to toggle source
# File lib/uaa/cli/base.rb, line 196
def add_command(branches, parts, opts = nil)
  if parts.empty?
    return if opts.nil? || opts.empty?
    return branches << {label: opt_strs(opts)}
  end
  if i = branches.find_index { |b| parts[0] == b[:label] }
    parts.shift
  else
    branches << {label: parts.shift, sub: []}
    i = -1
  end
  add_command(branches[i][:sub], parts, opts)
end
ask(prompt) click to toggle source
# File lib/uaa/cli/base.rb, line 61
def ask(prompt) @highline.ask("#{prompt}:  ") end
ask_pwd(prompt) click to toggle source
# File lib/uaa/cli/base.rb, line 62
def ask_pwd(prompt) @highline.ask("#{prompt}:  ") { |q| q.echo = '*' } end
gripe(msg) click to toggle source
# File lib/uaa/cli/base.rb, line 64
def gripe(msg) @output.puts(msg) end
help_col_start() click to toggle source
# File lib/uaa/cli/base.rb, line 73
def help_col_start
  return @help_col_start ||= 35 if @help_col_start || terminal_columns == 0 || terminal_columns > 80
  @help_col_start = terminal_columns / 2
end
opt_help(key, args) click to toggle source
# File lib/uaa/cli/base.rb, line 127
def opt_help(key, args)
  raise "missing option definition for #{key}" unless args
  long = short = desc = nil
  args.each do |a|
    case a
    when /^-.$/ then short = a
    when /^--.*/ then long = a
    else desc = a
    end
  end
  raise "option definition must include long form (--#{key})" unless long
  [ short ? "#{short} | #{long}" : "#{long}", desc]
end
opt_strs(opts) click to toggle source
# File lib/uaa/cli/base.rb, line 141
def opt_strs(opts)
  opts.each_with_object([]) { |o, a|
    @cli_class.option_defs[o].each { |d|
      case d
      when /^--\[no-\](\S+)/ then a << "--#{$1} --no-#{$1}"
      when /^--(\S+)/ then a << "--#{$1}"
      end
    }
  }.join(' ')
end
opts() click to toggle source
# File lib/uaa/cli/base.rb, line 65
def opts; @options end
pp(obj, indent = 0, wrap = terminal_columns, label = nil) click to toggle source
# File lib/uaa/cli/base.rb, line 78
def pp(obj, indent = 0, wrap = terminal_columns, label = nil)
  case obj
  when Array
    if obj.empty? || !obj[0].is_a?(Hash) && !obj[0].is_a?(Array)
      say_definition(indent, ("#{label}: " if label), Util.strlist(obj), nil, wrap)
    else
      say_definition(indent, "#{label}: ", nil, nil, wrap) if label
      obj.each {|o| pp o, indent, wrap, '-' }
    end
  when Hash
    say_definition(indent, label, nil, nil, wrap) if label
    obj.each {|k, v| pp v, indent + 2, wrap, k.to_s}
  when nil
  else say_definition(indent, ("#{label}: " if label), obj.to_s, nil, wrap)
  end
  obj
end
print_tree(branches, indent) click to toggle source
say(msg) click to toggle source
# File lib/uaa/cli/base.rb, line 63
def say(msg) @output.puts(msg); msg end
say_cmd_helper(info, topic, suffix = nil) click to toggle source
# File lib/uaa/cli/base.rb, line 152
def say_cmd_helper(info, topic, suffix = nil)
  say_definition 2, info[:template], info[:desc]
  info[:options].each do |o|
    odef, desc = opt_help(o, topic.option_defs[o] ? topic.option_defs[o]: @cli_class.option_defs[o])
    say_definition help_col_start, "", desc ? "#{odef}, #{desc}" : odef
  end
  @output.print suffix
end
say_command_help(args) click to toggle source
# File lib/uaa/cli/base.rb, line 161
def say_command_help(args)
  say ""
  @cli_class.topics.each do |tpc|
    tpc.commands.each do |k, v|
      if args[0..v[:parts].length - 1] == v[:parts]
        say_cmd_helper(v, tpc, "\n")
        return "help command"
      end
    end
  end
  args = args.map(&:downcase)
  @cli_class.topics.each { |tpc| return say_help(tpc) unless (args & tpc.synonyms).empty? }
  gripe "No command or topic found to match: #{args.join(' ')}\n"
end
say_commands() click to toggle source
# File lib/uaa/cli/base.rb, line 219
def say_commands
  tree = {label: File.basename($0), sub: []}
  @cli_class.topics.each {|t| t.commands.each {|k, v| add_command(tree[:sub], v[:parts].dup, v[:options])}}
  add_command(tree[:sub], [], @cli_class.global_options)
  @output.puts tree[:label]
  print_tree(tree[:sub], 1)
  "help commands"
end
say_definition(indent, term, text = nil, start = help_col_start, wrap = terminal_columns) click to toggle source
# File lib/uaa/cli/base.rb, line 96
def say_definition(indent, term, text = nil, start = help_col_start, wrap = terminal_columns)
  cur = indent + (term ? term.length : 0)
  indent < 1 ? @output.printf("%s", term) : @output.printf("%*c%s", indent, ' ', term)
  if start.nil?
    start = 2 if (start = indent + 4) > wrap
  else
    start = 2 if start > wrap
    if cur < start
      @output.printf("%*c", start - cur, ' ')
    elsif cur > start
      @output.printf("\n%*c", start, ' ')
    end
    cur = start
  end
  return @output.printf("\n") unless text && !text.empty?
  text = text.dup
  text.each_line do |line|
    width = wrap == 0 ? 4096 : wrap - cur
    line = line.chomp
    while line.length > width
      i = line.rindex(' ', width) || width
      @output.printf("%s\n%*c", line[0..i - 1], start, ' ')
      width = wrap == 0 ? 4096 : wrap - start
      line = line[i..-1].strip
    end
    @output.printf("%s\n", line)
    cur = start
  end
  nil
end
say_help(topic = nil) click to toggle source
# File lib/uaa/cli/base.rb, line 176
def say_help(topic = nil)
  @output.print "\n#{@cli_class.overview}\n" unless topic
  @cli_class.topics.each do |tpc|
    next if topic && topic != tpc
    @output.print "\n#{tpc.topic}\n"
    tpc.commands.each { |k, v| say_cmd_helper v, tpc }
  end
  if topic || !@cli_class.global_options
    @output.print("\n")
    return topic ? "help topic" : "help"
  end
  @output.print "\nGlobal options:\n"
  @cli_class.global_options.each do |o|
    odef, desc = opt_help(o, @cli_class.option_defs[o])
    say_definition 2, odef, desc
  end
  @output.print("\n")
  "help"
end
terminal_columns() click to toggle source
# File lib/uaa/cli/base.rb, line 67
def terminal_columns
  return @terminal_columns ||= 0 if @terminal_columns || !@output.tty?
  cols = IO.console.winsize.last rescue 0 if $stdin.tty?
  @terminal_columns = !cols || cols < 40 ? 0 : cols
end