Class: CF::UAA::Topic

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

Direct Known Subclasses

CommonCli

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_class, options = {}, input = $stdin, output = $stdout) ⇒ Topic

Returns a new instance of Topic.



56
57
58
59
# 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

Class Attribute Details

.synonymsObject (readonly)

Returns the value of attribute synonyms.



25
26
27
# File 'lib/uaa/cli/base.rb', line 25

def synonyms
  @synonyms
end

Class Method Details

.commandsObject



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

def self.commands; @commands || {} end

.define_option(key, *args) ⇒ Object



35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

.option_defsObject



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

def self.option_defs ; @option_defs || {} end

.topic(*args) ⇒ Object



29
30
31
32
33
# 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

Instance Method Details

#add_command(branches, parts, opts = nil) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
# 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) ⇒ Object



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

def ask(prompt) @highline.ask("#{prompt}:  ") end

#ask_pwd(prompt) ⇒ Object



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

def ask_pwd(prompt) @highline.ask("#{prompt}:  ") { |q| q.echo = '*' } end

#gripe(msg) ⇒ Object



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

def gripe(msg) @output.puts(msg) end

#help_col_startObject



73
74
75
76
# 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) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# 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

#optsObject



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

def opts; @options end

#pp(obj, indent = 0, wrap = terminal_columns, label = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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


210
211
212
213
214
215
216
217
# File 'lib/uaa/cli/base.rb', line 210

def print_tree(branches, indent)
  return unless branches
  branches.each do |b|
    indent.times { @output.print "\t" };
    @output.puts b[:label]
    print_tree b[:sub], indent + 1
  end
end

#say(msg) ⇒ Object



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

def say(msg) @output.puts(msg); msg end

#say_cmd_helper(info, topic, suffix = nil) ⇒ Object



152
153
154
155
156
157
158
159
# 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) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 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_commandsObject



219
220
221
222
223
224
225
226
# 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) ⇒ Object



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
# 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) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 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_columnsObject



67
68
69
70
71
# 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