Class: CF::UAA::Topic
- Inherits:
-
Object
- Object
- CF::UAA::Topic
- Defined in:
- lib/uaa/cli/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.synonyms ⇒ Object
readonly
Returns the value of attribute synonyms.
Class Method Summary collapse
- .commands ⇒ Object
- .define_option(key, *args) ⇒ Object
- .desc(template, desc, *options, &handler) ⇒ Object
- .option_defs ⇒ Object
- .topic(*args) ⇒ Object
Instance Method Summary collapse
- #add_command(branches, parts, opts = nil) ⇒ Object
- #ask(prompt) ⇒ Object
- #ask_pwd(prompt) ⇒ Object
- #gripe(msg) ⇒ Object
- #help_col_start ⇒ Object
-
#initialize(cli_class, options = {}, input = $stdin, output = $stdout) ⇒ Topic
constructor
A new instance of Topic.
- #opt_help(key, args) ⇒ Object
- #opt_strs(opts) ⇒ Object
- #opts ⇒ Object
- #pp(obj, indent = 0, wrap = terminal_columns, label = nil) ⇒ Object
- #print_tree(branches, indent) ⇒ Object
- #say(msg) ⇒ Object
- #say_cmd_helper(info, topic, suffix = nil) ⇒ Object
- #say_command_help(args) ⇒ Object
- #say_commands ⇒ Object
- #say_definition(indent, term, text = nil, start = help_col_start, wrap = terminal_columns) ⇒ Object
- #say_help(topic = nil) ⇒ Object
- #terminal_columns ⇒ Object
Constructor Details
#initialize(cli_class, options = {}, input = $stdin, output = $stdout) ⇒ Topic
Returns a new instance of Topic.
55 56 57 58 |
# File 'lib/uaa/cli/base.rb', line 55 def initialize(cli_class, = {}, input = $stdin, output = $stdout) @cli_class, @options, @input, @output = cli_class, , input, output @highline = HighLine.new(input, output) end |
Class Attribute Details
.synonyms ⇒ Object (readonly)
Returns the value of attribute synonyms.
24 25 26 |
# File 'lib/uaa/cli/base.rb', line 24 def synonyms @synonyms end |
Class Method Details
.commands ⇒ Object
27 |
# File 'lib/uaa/cli/base.rb', line 27 def self.commands; @commands || {} end |
.define_option(key, *args) ⇒ Object
34 35 36 37 38 |
# File 'lib/uaa/cli/base.rb', line 34 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/uaa/cli/base.rb', line 40 def self.desc(template, desc, *, &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: } end |
.option_defs ⇒ Object
26 |
# File 'lib/uaa/cli/base.rb', line 26 def self.option_defs ; @option_defs || {} end |
.topic(*args) ⇒ Object
28 29 30 31 32 |
# File 'lib/uaa/cli/base.rb', line 28 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
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/uaa/cli/base.rb', line 195 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
60 |
# File 'lib/uaa/cli/base.rb', line 60 def ask(prompt) @highline.ask("#{prompt}: ") end |
#ask_pwd(prompt) ⇒ Object
61 |
# File 'lib/uaa/cli/base.rb', line 61 def ask_pwd(prompt) @highline.ask("#{prompt}: ") { |q| q.echo = '*' } end |
#gripe(msg) ⇒ Object
63 |
# File 'lib/uaa/cli/base.rb', line 63 def gripe(msg) @output.puts(msg) end |
#help_col_start ⇒ Object
72 73 74 75 |
# File 'lib/uaa/cli/base.rb', line 72 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
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/uaa/cli/base.rb', line 126 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
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/uaa/cli/base.rb', line 140 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 ⇒ Object
64 |
# File 'lib/uaa/cli/base.rb', line 64 def opts; @options end |
#pp(obj, indent = 0, wrap = terminal_columns, label = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/uaa/cli/base.rb', line 77 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) ⇒ Object
209 210 211 212 213 214 215 216 |
# File 'lib/uaa/cli/base.rb', line 209 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
62 |
# File 'lib/uaa/cli/base.rb', line 62 def say(msg) @output.puts(msg); msg end |
#say_cmd_helper(info, topic, suffix = nil) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/uaa/cli/base.rb', line 151 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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/uaa/cli/base.rb', line 160 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 ⇒ Object
218 219 220 221 222 223 224 225 |
# File 'lib/uaa/cli/base.rb', line 218 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.) @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
95 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 |
# File 'lib/uaa/cli/base.rb', line 95 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
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/uaa/cli/base.rb', line 175 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. @output.print("\n") return topic ? "help topic" : "help" end @output.print "\nGlobal options:\n" @cli_class..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 ⇒ Object
66 67 68 69 70 |
# File 'lib/uaa/cli/base.rb', line 66 def terminal_columns return @terminal_columns ||= 0 if @terminal_columns || !@output.tty? cols = HighLine::SystemExtensions.terminal_size.first rescue 0 @terminal_columns = !cols || cols < 40 ? 0 : cols end |