Class: NewRelic::Cli::Command
- Inherits:
-
Object
- Object
- NewRelic::Cli::Command
- Defined in:
- lib/new_relic/cli/command.rb
Direct Known Subclasses
Defined Under Namespace
Classes: CommandFailure
Instance Attribute Summary collapse
-
#leftover ⇒ Object
Returns the value of attribute leftover.
Class Method Summary collapse
Instance Method Summary collapse
- #err(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(command_line_args) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(command_line_args) ⇒ Command
Returns a new instance of Command.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/new_relic/cli/command.rb', line 31 def initialize(command_line_args) if Hash === command_line_args # command line args is an options hash command_line_args.each do |key, value| instance_variable_set("@#{key}", value.to_s) if value end else # parse command line args. Throw an exception on a bad arg. @options = do |opts| opts.on('-h', 'Show this help') { raise CommandFailure, opts.to_s } end @leftover = @options.parse(command_line_args) end rescue OptionParser::ParseError => e raise CommandFailure.new(e., @options) end |
Instance Attribute Details
#leftover ⇒ Object
Returns the value of attribute leftover.
13 14 15 |
# File 'lib/new_relic/cli/command.rb', line 13 def leftover @leftover end |
Class Method Details
.inherited(subclass) ⇒ Object
49 50 51 |
# File 'lib/new_relic/cli/command.rb', line 49 def self.inherited(subclass) @commands << subclass end |
.run ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/new_relic/cli/command.rb', line 56 def self.run @command_names = @commands.map { |c| c.command } extra = [] = ARGV. do |opts| script_name = File.basename($0) opts. = "Usage: #{script_name} [ #{@command_names.join(' | ')} ] [options]" opts.separator("use '#{script_name} <command> -h' to see detailed command options") opts end extra = .order! command = extra.shift if command.nil? STDERR.puts elsif !@command_names.include?(command) STDERR.puts "Unrecognized command: #{command}" STDERR.puts else command_class = @commands.find { |c| c.command == command } command_class.new(extra).run end rescue OptionParser::InvalidOption => e raise NewRelic::Cli::Command::CommandFailure, e. end |
Instance Method Details
#err(message) ⇒ Object
27 28 29 |
# File 'lib/new_relic/cli/command.rb', line 27 def err() STDERR.puts end |
#info(message) ⇒ Object
23 24 25 |
# File 'lib/new_relic/cli/command.rb', line 23 def info() STDOUT.puts end |