Class: RR::CommandRunner
- Inherits:
-
Object
- Object
- RR::CommandRunner
- Defined in:
- lib/rubyrep/command_runner.rb
Overview
This class implements the functionality to dispatch rubyrep commands.
Class Method Summary collapse
-
.commands ⇒ Object
Returns a hash of all commands registered with #register.
-
.register(commands) ⇒ Object
Registers one or multiple commands.
-
.run(args) ⇒ Object
Dispatches commands as per given command line parameters.
-
.show_version ⇒ Object
Prints the version to stderr.
Class Method Details
.commands ⇒ Object
Returns a hash of all commands registered with #register.
11 12 13 |
# File 'lib/rubyrep/command_runner.rb', line 11 def self.commands @commands ||= {} end |
.register(commands) ⇒ Object
Registers one or multiple commands. commands
is a hash with
-
key: name of the command
-
value: a command hash defining the command
A command hash consists of
-
:
description
: short description of the command -
:
command
: an object / class implementing the hash.Must have a method # runs a command # * +args+: array of command line parameters # note: will not contain the command name itself. def run(args)
29 30 31 |
# File 'lib/rubyrep/command_runner.rb', line 29 def self.register(commands) self.commands.merge!(commands) end |
.run(args) ⇒ Object
Dispatches commands as per given command line parameters.
-
args
: array of command line parameters
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rubyrep/command_runner.rb', line 41 def self.run(args) status = 0 = {} parser = OptionParser.new do |opts| opts. = <<EOS Usage: #{$0} [general options] command [parameters, ...] Asynchronous master-master replication of relational databases. EOS opts.separator "" opts.separator "Available options:" opts.on("--verbose", "Show errors with full stack trace") do [:verbose] = true end opts.on("-v", "--version", "Show version information.") do show_version = nil end opts.on_tail("--help", "Show this message") do $stderr.puts opts $stderr.puts "\nAvailable commands:" commands.sort.each do |command_name, command_hash| $stderr.puts " #{command_name.ljust(15)} #{command_hash[:description]}" end = nil end end begin # extract general options general_args = [] until args.empty? if args[0] =~ /^-/ general_args << args.shift else break end end # parse general options parser.parse!(general_args) # process commands if # this will be +nil+ if the --help or --version are specified if args.empty? $stderr.puts "No command specified.\n\n" run(['--help']) status = 1 else command = args[0] if command == 'help' and args.size == 1 run(['--help']) status = 0 elsif commands.include? command status = commands[command][:command].run(args.slice(1, 1_000_000)) else $stderr.puts "Error: Unknown command specified.\n\n" run(['--help']) status = 1 end end end rescue Exception => e $stderr.puts "Exception caught: #{e}" $stderr.puts e.backtrace if && [:verbose] status = 1 end return status end |