class WavefrontCommandBase
A base class which all command classes extend.
Public Instance Methods
Inheriting classes must override this method @return [Array]
# File lib/wavefront-cli/commands/base.rb, line 68 def _commands [] end
Inheriting classes must override this method @return [Array]
# File lib/wavefront-cli/commands/base.rb, line 39 def _options [] end
Anything which takes ACLs provides the same interface @return [Array]
# File lib/wavefront-cli/commands/base.rb, line 58 def acl_commands ["acls #{CMN} <id>", "acl #{CMN} clear <id>", "acl #{CMN} grant (view | modify) on <id> to <name>...", "acl #{CMN} revoke (view | modify) on <id> from <name>..."] end
@param term_width [Integer] force a terminal width. Makes
testing far simpler.
@return [String] the subcommands the command offers.
# File lib/wavefront-cli/commands/base.rb, line 104 def commands(term_width = TW) text_arr = %w[Usage:] _commands.flatten.each do |cmd| text_arr.<< ' ' + "#{CMD} #{word} #{cmd}\n".cmd_fold(term_width) end text_arr.<< " #{CMD} #{word} --help" text_arr.join("\n") end
Many commands have these options @return [Array]
# File lib/wavefront-cli/commands/base.rb, line 31 def common_options ['-E, --endpoint=URI cluster endpoint', '-t, --token=TOKEN Wavefront authentication token'] end
# File lib/wavefront-cli/commands/base.rb, line 10 def description "view and manage #{things}" end
@return [String] a full options string which docopt understands
# File lib/wavefront-cli/commands/base.rb, line 167 def docopt commands + "\n\n" + options + "\n\n" + postscript end
# File lib/wavefront-cli/commands/base.rb, line 132 def global_option_text(width, term_width) text_arr = ['Global options:'] global_options.each { |o| text_arr.<< opt_row(o, width, term_width) } text_arr.<< '' end
All commands have these options @return [Array]
# File lib/wavefront-cli/commands/base.rb, line 17 def global_options ['-c, --config=FILE path to configuration file', '-P, --profile=NAME profile in configuration file', '-D, --debug enable debug mode', '-n, --noop do not perform API calls', '-V, --verbose be verbose', '-f, --format=STRING output format', '-M, --items-only only show items in machine-parseable formats', '-h, --help show this message'] end
Formats an option string.
@param opt_str [String] the option string @param width [Integer] the width of the short + long options
columns. This is used to indent following lines
@param term_width [Integer] the width of the user's terminal
# File lib/wavefront-cli/commands/base.rb, line 145 def opt_row(opt_str, width, term_width = TW) format(" %s %-#{width}s %s", *opt_str.split(/\s+/, 3)).opt_fold(term_width, width + 5) end
@return [Integer] the width of the column containing short and
long options
# File lib/wavefront-cli/commands/base.rb, line 153 def option_column_width (global_options + _options).flatten.map do |o| o.split(/\s+/, 3)[0..1].join(' ').size end.max end
@param term_width [Integer] force a terminal width. Makes
testing far simpler.
@return [String] the options the command understands.
# File lib/wavefront-cli/commands/base.rb, line 119 def options(term_width = TW) width = option_column_width text_arr = if global_options.empty? [] else global_option_text(width, term_width) end text_arr.<< 'Options:' _options.flatten.each { |o| text_arr.<< opt_row(o, width, term_width) } text_arr.join("\n") end
@return [String] which will be printed underneath the options.
# File lib/wavefront-cli/commands/base.rb, line 161 def postscript '' end
@return [String] the name of the SDK class which does the work
for this command.
# File lib/wavefront-cli/commands/base.rb, line 89 def sdk_class word.capitalize end
@return [String] the name of the SDK file which does the work
for this command.
# File lib/wavefront-cli/commands/base.rb, line 96 def sdk_file word end
Anything which takes tags provides the same interface @return [Array]
# File lib/wavefront-cli/commands/base.rb, line 46 def tag_commands ["tags #{CMN} <id>", "tag set #{CMN} <id> <tag>...", "tag clear #{CMN} <id>", "tag add #{CMN} <id> <tag>", "tag delete #{CMN} <id> <tag>", "tag pathsearch #{CMN} [-al] [-o offset] [-L limit] <word>"] end
# File lib/wavefront-cli/commands/base.rb, line 78 def thing word end
# File lib/wavefront-cli/commands/base.rb, line 82 def things thing + 's' end
@return [String] the command keyword
# File lib/wavefront-cli/commands/base.rb, line 74 def word self.class.name.sub(/WavefrontCommand/, '').downcase end