Class: CommandDispatcher::Dispatcher
- Inherits:
-
Object
- Object
- CommandDispatcher::Dispatcher
- Defined in:
- lib/command-dispatcher.rb
Overview
Constant Summary collapse
- EX_NOCMD =
"No command specified"- EX_INVALCMD =
"is not a valid command"
Instance Method Summary collapse
-
#dispatchCommand(cmd, *arguments) ⇒ Object
————————————————————————- ————————————————————————-.
-
#initialize(rootScriptAbsLocation, rootScriptName) ⇒ Dispatcher
constructor
————————————————————————- ————————————————————————-.
-
#isValid?(command) ⇒ Boolean
————————————————————————- ————————————————————————-.
-
#usage ⇒ Object
————————————————————————- ————————————————————————-.
Constructor Details
#initialize(rootScriptAbsLocation, rootScriptName) ⇒ Dispatcher
11 12 13 14 15 |
# File 'lib/command-dispatcher.rb', line 11 def initialize(rootScriptAbsLocation, rootScriptName) @rootScriptAbsLocation = rootScriptAbsLocation @rootScriptName = rootScriptName @commandPrefix = rootScriptAbsLocation + '/' + rootScriptName + '-'; end |
Instance Method Details
#dispatchCommand(cmd, *arguments) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/command-dispatcher.rb', line 33 def dispatchCommand(cmd, *arguments) raise ArgumentError.new(EX_NOCMD) if cmd == nil raise ArgumentError.new("#{cmd} " + EX_INVALCMD) unless isValid?(cmd) exec(@commandPrefix + cmd, *arguments) end |
#isValid?(command) ⇒ Boolean
43 44 45 46 |
# File 'lib/command-dispatcher.rb', line 43 def isValid?(command) return false if (!File.exists?(@commandPrefix + command)) return true end |
#usage ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/command-dispatcher.rb', line 19 def usage() Dir.glob(@commandPrefix + '*').each do |f| tmp=f.slice(@commandPrefix.size, f.size) invalid=tmp.include? "-" if !invalid help = `#{f} --usage` command = File.basename(f).gsub!( "-", " " ) $stdout.puts command.ljust(25) + " : " + help end end end |